Search code examples
pythonface-recognitionopencv

What is the reason for the low video speed in the Face recognition with opencv-python?


This code works corectly , But it's a very slow .

I changed the cv2.waitKey(1) number. But it still didn't change much

import cv2
import numpy as np 

facexml = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")                  
eyexml = cv2.CascadeClassifier("haarcascade_eye.xml")

cap = cv2.VideoCapture("my_video.avi")                                         

while True:
    _,frame = cap.read() 
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)                
    faces = facexml.detectMultiScale(gray)                            
    for (x,y,w,h) in faces:                                       
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)          
        roi_gray = gray[y:y+h, x:x+w]                             
        roi_color = frame[y:y+h, x:x+w]                          

        eyes = eyexml.detectMultiScale(roi_gray)                  
        for (ex,ey,ew,eh) in eyes:                                   
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,0,255),1)   

    cv2.imshow("window",frame)
    if cv2.waitKey(1) & 0XFF == ord("q"):
        break
cap.release()
cv2.destroyAllWindows()                

Solution

  • Simply a Harris cascade classifier is an old and slow algorithm for fast online face recognition in video. Try to read an OpenCV manual on Cascade Classifier and reduce the number of scales by setting equal maxSize and minSize or set larger scaleFactor to decrease total amount of images computed from original image by resizing.