【问题标题】:auto detect face and take a snapshot with opencv使用opencv自动检测面部并拍摄快照
【发布时间】:2016-07-26 04:38:28
【问题描述】:

我正在与我的大学一起进行人脸识别项目。如果在关闭网络摄像头之前自动检测到面部,我正在尝试拍摄快照并保存它。 我现在拥有的是打开凸轮并等待如果检测到面部,然后按“q”拍摄快照并保存图像。 代码如下:

import numpy as np
import cv2
import time

#import the cascade for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')



def TakeSnapshotAndSave():
    # access the webcam (every webcam has a number, the default is 0)
    cap = cv2.VideoCapture(0)

    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        # to detect faces in video
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)

        for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w]

        x = 0
        y = 20
        text_color = (0,255,0)
        # write on the live stream video
        cv2.putText(frame, "Press q when ready", (x,y), cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=2)


        # if you want to convert it to gray uncomment and display gray not fame
        #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the resulting frame
        cv2.imshow('frame',frame)
        # press the letter "q" to save the picture
        if cv2.waitKey(1) & 0xFF == ord('q'):
            # write the captured image with this name
            cv2.imwrite('try.jpg',frame)
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    TakeSnapshotAndSave()

提前谢谢你

【问题讨论】:

  • 好主意,我要去试试,让你知道它是否有效。谢谢

标签: python opencv face-detection face-recognition


【解决方案1】:

在 for (x,y,w,h) in faces: 循环中执行 imwrite()。如果您使用固定文件名,您最后检测到的人脸将被保存,其余的将被覆盖

【讨论】:

    【解决方案2】:

    我修改了您的代码以保存 10 张图像仅用于测试,如果您想要无限的照片,只需更改 while 条件。因此,在您的代码中,您覆盖了当前图像,因此我更改了字符串参数,以便可以拍摄大量照片。

    import numpy as np
    import cv2
    import time
    
    #import the cascade for face detection
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    
    def TakeSnapshotAndSave():
        # access the webcam (every webcam has a number, the default is 0)
        cap = cv2.VideoCapture(0)
    
        num = 0 
        while num<10:
            # Capture frame-by-frame
            ret, frame = cap.read()
    
            # to detect faces in video
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    
            for (x,y,w,h) in faces:
                cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
                roi_gray = gray[y:y+h, x:x+w]
                roi_color = frame[y:y+h, x:x+w]
    
            x = 0
            y = 20
            text_color = (0,255,0)
    
            cv2.imwrite('opencv'+str(num)+'.jpg',frame)
            num = num+1
    
        # When everything done, release the capture
        cap.release()
        cv2.destroyAllWindows()
    
    
    if __name__ == "__main__":
        TakeSnapshotAndSave()
    

    【讨论】:

    • 你好布鲁诺,我试试你的代码。它报告以下内容:错误:OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty()在函数'cv::cvtColor'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2015-12-12
    • 1970-01-01
    • 2020-09-14
    • 2013-02-14
    • 2014-12-03
    相关资源
    最近更新 更多