读取视频中的人脸

代码如下:

import cv2
def face_detect_demo(img):
    #将图片灰度处理
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #加载特征数据
    face_detector = cv2.CascadeClassifier('E:/opencv_package/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
    faces = face_detector.detectMultiScale(gray)
    for x,y,w,h in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
    cv2.imshow('result',img)
#读取视频
cap = cv2.VideoCapture('video.mp4')
while True:
    flag,frame = cap.read()
    if not flag:
        break
    face_detect_demo(frame)
    if ord('q') == cv2.waitKey(10):
        break
cv2.destroyAllWindows()
cap.release()

你可能感兴趣的:(Python,图形识别,机器学习)