【问题标题】:SyntaxError: invalid syntax while trying to release webcamSyntaxError:尝试释放网络摄像头时语法无效
【发布时间】:2020-03-03 16:13:14
【问题描述】:

我编写了一个代码,计划通过从笔记本电脑的网络摄像头捕获帧来执行面部、眼睛和微笑检测。但是,当我完成后,下面的代码不会运行并返回一个无效的语法错误。 我不知道如何解决这个问题,因为返回错误的行与我在互联网上的任何地方都是一样的。但是我仍然可能在某个地方做错了。(顺便说一下,我是 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

【问题讨论】:

  • print("{} saved!".format(img_name, frame) 缺少结束 )

标签: python opencv detection face webcam-capture


【解决方案1】:

在这一行:

print("{} 已保存!".format(img_name, frame)

(如评论中所述)缺少“)”。

此外,在同一行中,使用“.format()”的语法不正确,因为您在“.format”括号内给出了两个变量,即“img_name”和“frame”但在 print 的“”中只有一个“{}”。所以如果要打印两个变量的值,应该有两个“{}”。请注意,这不会抛出任何错误,而只是指出它。

希望这会有所帮助。

编辑:

检查代码中的缩进。

之后

k = cv2.waitKey(1)

“faces = face_classifier.....”行之前有一个空格。 (我不知道这是在粘贴代码时发生的,还是在您的编辑器中也是这样)

另外,我认为您不需要将此 for 循环嵌套在前一个循环中。它应该在“while循环缩进”内

对于 fx,fy,fw,fh 的微笑: cv2.rectangle(frame, (fx,fy), (fx+fw, fy+fh), (110,100,200), 1.5)

然后将接下来的代码行也放在“while循环缩进”中

所以最后你的代码会是这样的:

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()

【讨论】:

  • 我根据您的指导进行了必要的更改,但它仍然会引发与我在上面提到的错误 2 相同的错误。 (很抱歉多次评论和修改。我试图在这里添加错误。那么 video_capture.release() 的语法有什么问题??还是谢谢你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 2015-07-08
  • 2016-07-17
  • 1970-01-01
相关资源
最近更新 更多