我更改了cv2.waitKey(1)号。但它仍然没有太大变化
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()
简而言之,Harris级联分类器是一种用于视频中快速在线人脸识别的古老而缓慢的算法。尝试阅读Cascade Classifier上的OpenCV手册,并通过将maxSize
和minSize
设置为相等来减少比例数,或者设置较大的scaleFactor以通过调整大小来减少从原始图像计算出的图像总量。