如何在进行面部检测时释放网络摄像头?

问题描述 投票:0回答:1

我已经编写了一个代码,该代码计划通过从笔记本电脑的网络摄像头捕获帧来执行面部,眼睛和微笑检测。但是,完成后,以下代码将无法运行,并返回无效语法错误。我不知道如何解决此问题,因为该行返回的错误与我在互联网上的所有地方都一样。但是我仍然可能在某处做错了。(顺便说一下,我对Python来说是一个新手!)>

import cv2
import sys
import numpy as np

face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_classifier = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_classifier = cv2.CascadeClassifier('haarcascade_smile.xml')

video_capture = cv2.VideoCapture(0)   

img_counter = 0

while True:
    #Capture frame by frame
    _, frame = video_capture.read()
    im_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    k = cv2.waitKey(1)

    #Detect faces, eyes and smiles in input frame
     faces = face_classifier.detectMultiScale(im_gray, 
                                              scaleFactor = 1.5, 
                                              minNeighbors = 5,
                                              minSize=(30, 30),
                                            flags=cv2.CASCADE_SCALE_IMAGE)

     eyes = eye_classifier.detectMultiScale(im_gray, 
                                            scaleFactor = 1.5,
                                            minNeighbors = 3,
                                            minSize=(10, 10),
                                            maxSize=(15,15),
                                          flags = cv2.CASCADE_SCALE_IMAGE)

     smiles = smile_classifier.detectMultiScale(im_gray,
                                                scaleFactor = 1.5,
                                                minNeighbors = 3,
                                                minSize = (5,5),
                                                maxSize = (10,15),
                                         flags = cv2.CASCADE_SCALE_IMAGE)

    # Draw a rectangle around the faces
    for x, y, w, h in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Draw a rectangle around the eyes
    for ex, ey, ew, eh in eyes:
        cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0,0,255), 1)         

        # Draw a rectangle around the smiles    
        for fx,fy,fw,fh in smiles:
            cv2.rectangle(frame, (fx,fy), (fx+fw, fy+fh), (110,100,200), 1.5)

            # Display the resulting frame
            cv2.imshow('Our Face Detector', frame)

            if k == 27: #ESC Pressed
                break
            elif k == 32: # SPACE pressed
                img_name = "FaceDetect_webcam_{}.png".format(img_counter)
                cv2.imwrite(img_name,frame)
                print("{} saved!".format(img_name, frame)
                img_counter += 1
video_capture.release()
cv2.destroyAllWindows()

这里是错误1:

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/User/Desktop/Top Secret/EE 492/ANACONDA-PYTHON-OPENCV/untitled0.py", line 69
img_counter += 1
          ^
SyntaxError: invalid syntax

这里是错误2:

File "<ipython-input-21-a9c9ddf625b0>", line 64
video_capture.release()
     ^
SyntaxError: invalid syntax

我已经编写了一个代码,该代码计划通过从笔记本电脑的网络摄像头捕获帧来执行面部,眼睛和微笑检测。但是,完成后,以下代码将无法运行,并返回无效的...

python release detection face webcam-capture
1个回答
0
投票

在此行:

© www.soinside.com 2019 - 2024. All rights reserved.