opencv (python) 实现人脸检测+拍照

import cv2
 
def make_photo():
    face = cv2.CascadeClassifier("E:/Anaconda3/envs/py36/Library/etc/haarcascades/haarcascade_frontalface_alt2.xml")
    cap = cv2.VideoCapture(0)  # 笔记本自带的摄像头
    while True:
        ret, frame = cap.read()    # 每一帧读取
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 转灰
        faces = face.detectMultiScale(gray)
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)     
        if ret:
            cv2.imshow("capture", frame)  # 弹窗口
            # 按q键关闭摄像头
            if cv2.waitKey(1) & 0xFF == ord('q'):
                file_name = "picture.jpeg"
                cv2.imwrite(file_name, frame)
                break
        else:
            break
            
    cap.release()     # 释放资源
    cv2.destroyAllWindows()
    

你可能感兴趣的:(人脸识别,opencv)