[英]OpenCV Face Recognition Program Crashing
Here's the code (I deleted everything that wasn't important)这是代码(我删除了所有不重要的内容)
closed = False
faceClassifier = cv2.CascadeClassifier('face.xml')
eyeClassifier = cv2.CascadeClassifier('eye.xml')
bodyClassifier = cv2.CascadeClassifier('body.xml')
cap = cv2.VideoCapture(0)
def drawBox():
while closed == False:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceClassifier.detectMultiScale(img, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eyeClassifier.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
def stopProgram():
closed = True
tkWindow = Tk()
tkWindow.geometry('800x800')
tkWindow.title('Machine Learning Face Detection')
button = Button(tkWindow,
text = 'Stop Program',
command = stopProgram)
button.pack()
button = Button(tkWindow,
text = 'Start Program',
command = drawBox)
button.pack()
tkWindow.mainloop()
cap.release()
cv2.destroyAllWindows()
As soon as I hit the start program error, the camera window opens gray and then it crashes.一旦我遇到启动程序错误,相机窗口就会打开灰色,然后崩溃。 Anyone have any idea why?任何人都知道为什么? I can't figure it out, but it was working before I tried to implement the stop button.我想不通,但在我尝试实现停止按钮之前它就可以工作了。
The first problem is that you didn't mark closed
as a global variable in your functions drawBox
and stopProgram
.第一个问题是您没有在函数drawBox
和stopProgram
closed
标记为全局变量。 Other problem is the while loop in drawBox
, which will cause the program to stop responding, rendering "Stop Program" button unusable.另一个问题是drawBox
的 while 循环,这将导致程序停止响应,使“停止程序”按钮无法使用。 You need to use tkWindow.after
.您需要使用tkWindow.after
。 Here 's an example of displaying video frames from cv2.VideoCapture
in tkWindow
.这是在tkWindow
中显示来自cv2.VideoCapture
视频帧的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.