-3

I'm trying to detect three types of fruit using a model and opencv for the webcam. Everything works, but when i try to close it down it freezes and i have to use task manager to close it down. This is the code I'm using:

cap = cv2.VideoCapture(0)
while True: 
    ret,img=cap.read()
    cv2.startWindowThread()
    cv2.imshow('Video', img)
    img = cv2.resize(img, (224, 224))
    pred = what_fruit(img)
    print(pred)

    if cv2.waitKey(1) == ord("q"):
            break

Is there a way to use opencv webcam and have it close down correctly in notebook?

nobody
  • 19
  • 2
  • I suggest this read https://stackoverflow.com/questions/27882255/is-it-possible-to-display-an-opencv-video-inside-the-ipython-jupyter-notebook and this https://stackoverflow.com/questions/13734276/python-freezes-after-cv2-destroywindow – iGian Jan 01 '22 at 16:39

1 Answers1

0

It appears you forgot two lines of code after the while loop:

cap.release()
cv2.destroyAllWindows()
TLado
  • 48
  • 4