我在Manjaro上使用vs代码ann在这个问题中得到了很多解释,“ VS- CODE errors on manjaro, auto quit, can't open folder
之后我有以下代码
import cv2
from random import randrange
#load data
trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#Choose image
webcam = cv2.VideoCapture(0)
while True:
successful_frame_read, frame = webcam.read()
#convert to greyscale
greyscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#detect faces
face_coordinates = trained_face_data.detectMultiScale(greyscaled_img)
#Draw a rectangle around the Face
for (x, y, w, h) in face_coordinates:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0, 10)
#Display the image with the faces spotted
cv2.imshow('Face detector', frame)
key = cv2.waitKey(1)
#stop if Q is pressed
if key==81 or key==113:
break
webcam.release()
print("code completed")
在一开始,我在语法上出现错误
File "Face_detector.py", line 24
cv2.imshow('Face detector', frame)
^
SyntaxError: invalid syntax
我注释掉了这一行,但是在下一行中出现了相同的错误
File "Face_detector.py", line 25
key = cv2.waitKey(1)
^
SyntaxError: invalid syntax
所以我继续评论每一行,直到最后 现在我出现了EOF错误
File "Face_detector.py", line 33
^
SyntaxError: unexpected EOF while parsing
我尝试从终端运行脚本,但仍然有完全相同的错误
Here is an image of running the uncommented code from the terminal
Here is an image of running the final commented out code from the terminal
答案 0 :(得分:0)
您在此行缺少括号:
#Draw a rectangle around the Face
for (x, y, w, h) in face_coordinates:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0, 10) # <-- Missing parentheses here