OpenCV (Open source computer vision) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel). The library is cross-platform and free for use under the open-source BSD license.
The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc.
In this tutorial we are developing a facial recognition on a photo using Haar Cascades.
So Lets get started
OpenCV comes with a trainer as well as detector. If you want to train your own classifier for any object like car, planes etc. you can use OpenCV to create one. Here we will deal with detection. OpenCV already contains many pre-trained classifiers for face, eyes,smiles, etc. Those XML files are stored in the opencv/data/haarcascades/ folder. Let’s create a face and eye detector with OpenCV.
1. run pip install opencv-python if you need only main modules
2. run pip install opencv-contrib-python if you need both main and contrib modules (check extra modules listing from OpenCV documentation)
3. Create a new file as face.py and open it in your choice of editor.
4. And Type or paste following Code as save it.
FACE DETECTION CODE
img = cv2.imread(image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
Thats it guys try it with various images.
since this tutorial was short with no description on how the commands works. A detailed tutorial might come very soon.
Next time we will return for live facial detection with Webcam.
All required resources available in github below. You can download and simply run too.