人脸检测之——学习
此代码根据haa特征检测人脸
import cv2
def detect(img, cascade):
rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=3,flags=cv2.CASCADE_DO_CANNY_PRUNING, minSize=(3, 3))
if len(rects) == 0:
return []
rects[:,2:] += rects[:,:2]
return rects
video_capture = cv2.VideoCapture(0)
print(video_capture.isOpened())
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while True:
ret, frame = video_capture.read()
if ret:
t1 = cv2.getTickCount()
faces = detect(frame,face_cascade)
t2 = cv2.getTickCount()
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (w, h), (255, 255, 255), 2)
cv2.imshow('imshow', frame)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()