opencv练习_打开视频人脸识别,将图片保存在文件夹内

import cv2

#人脸识别
faceCascade = cv2.CascadeClassifier("./venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml")

#读取视频文件
video = cv2.VideoCapture(’./image/weimi_meinv.mp4’,0)
num = 0 #定义变量
while (video.isOpened()):
retval,image = video.read()
#cv2.namedWindow(‘video’,0)
#cv2.resizeWindow(‘video’,1280,720)

if retval == True:
    #cv2.imshow('video',image)
    faces = faceCascade.detectMultiScale(image, 1.15)
    print(len(faces))
    for (x, y, w, h) in faces:
        if w > 150: #图像宽超过150才保存
            print('xywh', (x, y, w, h))
            cropped = image[y:y + w, x:x + h]  # 裁剪坐标为[y0:y1, x0:x1]
            cv2.imwrite("./image/weimi_meinv/meinv_" + str(num) + '.jpeg', cropped)
            num += 1
else:
    break
key = cv2.waitKey(1)
if key == 27:
    break

video.release()
cv2.destroyAllWindows()

你可能感兴趣的:(python)