python3.6+OpenCV3.4.2-Camera实时捕获人脸

 1 import cv2 as cv
 2 
 3 
 4 def catch_face_video(img):
 5     gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
 6     path_haar='C:/Users/77007/Desktop/python/pythonProject1/opencv/build/etc/haarcascades/'
 7     face_haar='haarcascade_frontalface_default.xml'
 8     classifier=cv.CascadeClassifier(path_haar+face_haar)
 9     faces = classifier.detectMultiScale(gray)
10     for x,y,w,h in faces:
11         cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=1)
12     cv.imshow('rec·',img)
13 
14 
15 def catch_video():
16         capture=cv.VideoCapture(0)
17         # 调用摄像头,只有一个的话默认为0,也可以是视频文件/path
18         while True:
19             flag,frame=capture.read()
20             if not flag:
21                 break
22             catch_face_video(frame)
23             if 27==cv.waitKey(10):
24                 break
25             # 等待10ms显示图像,若过程中按'Esc'(对应27)退出
26         cv.destroyAllWindows()
27         capture.release()
28 
29 
30 if __name__ == '__main__':
31     catch_video()

 

posted @ 2021-03-13 15:33  墨鳌  阅读(118)  评论(0编辑  收藏  举报