Without going in much definition let's jump to our implementation.
openCV stands for Open source computer vision. it is a library of programming functions mainly aimed at real-time computer vision.
For making task easier they have pretrained model name haarcascade
which provides good accuracy.
- install opencv at first.
pip install opencv-python
- Import libraries
- Attach frontal-face and apply basic logic.
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Read the input image img = cv2.imread('your-image') # convert it into gray code from BGR gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # apply a scale of 1.1 and 4 faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Make rectangle around face of green colour and thickness of 3. for (x, y , w ,h) in faces: cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255 , 0), 3) # Display the output cv2.imshow('img', img) cv2.waitKey()
- And here you are done.... Output image
It is also possible to detect multiple faces in realtime in videos.
That will be a part of my next blog.
(You can ask doubt in comment section.)
Thanks for reading.