Face Recognition with Python and OpenCV

Haar feature-based cascade classifiers object detection method

Face Recognition with Python and OpenCV

Table of contents

No heading

No headings in the article.

Haar feature-based cascade classifiers is a machine learning-based effective object detection method are where a cascade function is trained from a lot of positive and negative images. let's Learn A small program for Face Recognition with Python OpenCV and Haar-cascade.


py -3.10 -m pip install opencv-python --user

after installation, we start the program first import open CV library

import cv2

CascadeClassifier method in cv2 module supports the loading of haar-cascade XML files. haar-cascade XML can be downloaded from here Click to Download

face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

cv2.VideoCapture is used for video capturing from video files, image sequences, or cameras.

cv2.VideoCapture(0)#: Means first camera or webcam.
cv2.VideoCapture(1)#:  Means second camera or webcam.
cv2.VideoCapture("file name.mp4")#: Means video file

Now we need to convert the image to gray to detect the face (object) from image. There are more than 150 color-space conversion methods available in OpenCV. We will use gray color space conversion codes below.

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

detectMultiScale Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.


  • image Matrix of the type CV_8U containing an image where objects are detected.
  • objects Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image.
  • scaleFactor Parameter specifying how much the image size is reduced at each image scale.
  • minNeighbors Parameter specifying how many neighbors each candidate rectangle should have to retain it.
  • flags Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.

  • minSize Minimum possible object size. Objects smaller than that are ignored.

  • maxSize Maximum possible object size. Objects larger than that are ignored. If maxSize == minSize model is evaluated on single scale.

void cv::CascadeClassifier::detectMultiScale    (    InputArray     image,
std::vector< Rect > &     objects,
double     scaleFactor = 1.1,
int     minNeighbors = 3,
int     flags = 0,
Size     minSize = Size(),
Size     maxSize = Size() 

#this will return a list of  x,y,w,h (X,y and width and height value of rectangle around object detected)

cv2.rectangle will draw a rectangle with given coordinates on image

  • image: It is the image on which rectangle is to be drawn.
  • start_point: It is the starting coordinates of rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
  • end_point: It is the ending coordinates of rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).

  • color: It is the color of border line of rectangle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.

  • thickness: It is the thickness of the rectangle border line in px. Thickness of -1 px will fill the rectangle shape by the specified color.

#cv2.rectangle(image, start_point, end_point, color, thickness)
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
x1,y1 ------
|          |
|          |
|          |

video.release() When everything is done, the function will release the video capture.


cv2.destroyAllWindows() This function allows users to destroy all windows at any time. It is similar to destroyWIndow(). destroyWindow() only destroys a specific window and destroyAllWindow() will destroy all windows.


Now let's bring it all together.

#Face Recognition with Python and OpenCV  
import cv2 
face_cascade =cv2.CascadeClassifier("haarcascade_frontalface_default.xml") 
video = cv2.VideoCapture(0)
while True:
    check, frame = video.read() 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)    
    face= face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5)
    for x,y,w,h in face:
        frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3) 

    cv2.imshow("Face Detection", frame)
    key = cv2.waitKey(1)

    if key==ord('q'): 
        break  # exit if button q is press