The following tutorial will introduce you with the concept of object detection in python using OpenCV and how you can use if for the applications like face and eye recognition.
OpenCVOpen source computer vision library is an open source computer vision and machine learning library. It was built with a vision to provide basic infrastructure to the computer vision application.
Haar Cascade AlgorithmIt is a machine learning algorithm used to identify objects in image or video based on the concepts of features proposed by Paul Viola and Michael Jones in 2001.
The algorithm contains four stages:
- Haar Feature Selection
- Creating Intergal Images
- Adaboost Training
- Cascading Classifiers
Before jumping into the code you have to install OpenCV into your Odinub. You can check out the steps from here.
Step 1: Create a new Python file using the following command:gedit filename.py
Step 2: Now before starting the code import the modules of OpenCV as following:- The following command will enable the code to do all the scientific computing. It will enable the code to carry out different operations:
import numpy as np
- The following module will make available all the functionalities of the OpenCV library.
import cv2
Step 3: The cascade classifiers- The cascade classifiers are the trained.xml files for detecting the face and eyes.
face_cascade=cv2.CascadeClassifer('/root/opencv/data/haarcascades/haarcasscade_frontalface_default.xml')eye_cascade=cv2.CascadeClassifier('root/opencv/data/haarcascades/haarcascade_eye.xml')
Step 4: The imread() functionThe imread() function is used to read the image captured by passing the path of the image as the input parameter in form of string.
img=cv2.imread(‘/root/Desktop/baby.jpg’).
Step 5: The cvtColor() functionIt converts the imge from one color space to another.
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Step 6: The imshow() functionIt is used to display the image on the window. Here the first command is the string which will assign the name to the window. The second argument is the image that is to be displayed into the window.
cv2.imshow(‘img’,img)
Step 7: The waitKey() functionIt will wait generate delay for the specified milliseconds.
cv2.waitKey()
Step 8: The destroyAllWindows() functionThis function will destroy all the previously created windows.
cv2.destroyAllWindows
Step 9: Simply run your code with the help of following commandpython filename.py
The following is the output of the code detecting the face and eyes of an already captured image of a baby.
The OpenCV contains more than 2500 optimized algorithms which includes both classic and start of the art computer vision and machine learning algorithms. Thus with OpenCV you can create a number of such identifiers, will share more projects on OpenCV for more stay tuned!
Comments
Please log in or sign up to comment.