0

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.

1 Answer 1

0

The first problem is that you didn't mark closed as a global variable in your functions drawBox and stopProgram. Other problem is the while loop in drawBox, which will cause the program to stop responding, rendering "Stop Program" button unusable. You need to use tkWindow.after. Here's an example of displaying video frames from cv2.VideoCapture in tkWindow.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.