我正在为我的深度学习项目编写代码,并且在运行面部检测时遇到错误。
我正在构建面部识别系统,但出现错误
NameError:未定义名称'x'
在运行代码时,但是我找不到这些问题的根源。
import numpy as np
import cv2
CascadeClassifier = cv2.CascadeClassifier(r'C:\Users\ESEC\Desktop\opencv\aditya\cascades\haarcascades\haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = CascadeClassifier.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
for (x, y, w, h) in faces:
print(x,y,w,h)
roi_gray = gray[y:y+h, x:x+w]
roi_color = gray[y:y+h, x:x+w]
img_item = 'image.png'
cv2.imwrite(img_item, roi_gray)
# Our operations on the frame come here
color = (255,0,0) #BGR 0=255
stroke = 2
end_cord_x = x + w
end_cord_y = y + h
cv2.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
它应该运行平稳并且应该在脸部周围显示矩形,但是出现以下错误。
File "camera-test.py", line 34, in <module>
end_cord_x = x + w
NameError: name 'x' is not defined