我试图在openCV中围绕面部创建一个矩形,但得到NameError
。
我试图找到根本原因,但是没有成功。
import numpy as np
import cv2
CascadeClassifier = cv2.CascadeClassifier('/Users/adityaacharya/Desktop/openCV/aditya/cascades/haarcascades/haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture('http://root:admin@123@192.168.1.114/mjpg/video.mjpg')
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]#(ycord_start, ycord_end)
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)#BGE 0-255
stroke = 2
height = x + w
width = y + h
cv2.rectangle(frame, (x, y), (height, width))
# 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 33, in <module> height = x + w
NameError: name 'x' is not defined