OpenCV is one of the most popular library for computer vision. It uses machine learning algorithms for computer vision.
I Installed OpenCV using conda on my Mac with below command
"conda install -c conda-forge opencv"
I Installed OpenCV using conda on my Mac with below command
"conda install -c conda-forge opencv"
Face detection algorithms work by reading a block of image and matching against thousands of small patterns and features to detect if it is the desired object. OpenCv uses cascades for this. Cascades are trained with a sample views of a particular objects. Read more about cascades here.
OpenCV cascade makes object detection faster because it checks based on stages. If initial tests are positive, it proceeds with further tests. If the input picture has failed for the test, the algorithm will not test it for all the patterns and features. This makes object detection faster and in real time.
Below is a simple example of face detection in action.
1. First import OpenCV and Sys
2. Create the OpenCv cascade and initialise with face cascade.
This loads the face cascade into memory. We will use haarcascade_frontalface_default.xml which comes with OpenCV. It is an xml file which contains data to detect faces.
3. Read the input image and convert it into gray scale
4. Call the function detectMultiScale on face cascade to detect face. Pass the desired parameters like scaleFactor, minNeighbors, minSize etc. This function returns a list of rectangles which it believes is a face. It returns the x,y coordinates of the face along with the width and height.
5. Draw a rectangle around the face using the values returned from detectMultiScale function.
The source code looks like below
In order to test this program. Go to terminal and type
Python DetectFace.py <InputImage.png without the angular brackets>
Make sure that face cascade xml is placed in the same folder as your source .py file. Face cascade xml will be supplied by the OpenCV library. The other option is to fully qualify the path during initialization of the cascade.
The output image I got looks like below
OpenCV is very interesting and promising. I hope to catch up on these in future.


No comments:
Post a Comment