Friday , June 18 2021
Home / Computer Vision / Face Detection From an Image in Python using OpenCV

Face Detection From an Image in Python using OpenCV

13 Face Detection from Image

Face Detection From an Image in Python using OpenCV

You need to download the trained classifier XML file (haarcascade_frontalface_default.xml), which is available in OpenCv’s GitHub repository. Save it to your working location.

The detection works only on grayscale images. So it is important to convert the color image to grayscale.

Detect faces in the image

  • The detectMultiScale function is a general function that detects objects. Since we are calling it on the face cascade, that’s what it detects.

  • The first option is the grayscale image.

  • The second is the scaleFactor. Since some faces may be closer to the camera, they would appear bigger than the faces in the back. The scale factor compensates for this.

  • The detection algorithm uses a moving window to detect objects. minNeighbors defines how many objects are detected near the current one before it declares the face found.

In [2]:
import cv2

img = cv2.imread('family.jpg')

faces = face_cascade.detectMultiScale(gray_img,scaleFactor=1.1,minNeighbors=5)
print("Total Faces:",len(faces))

print("Face Coordinates:",faces)
Total Faces: 3
Face Coordinates: [[191 337 231 231]
 [399 446 201 201]
 [578 362 246 246]]

Draw Rectangle on Face

In [3]:
for x,y,w,h in faces:
In [4]:

About Machine Learning

Check Also

Access Camera Using OpenCV with Python

11 Open Webcam Access Camera Using OpenCV with Python¶ first, we Import libraries OpenCV. Then …

Leave a Reply

Your email address will not be published. Required fields are marked *