6

ENVIRONMENT

OS- mint Linux, using opencv3.1,using spyder through anaconda

ISSUE

The code mentioned below opens a window of name frame and display the video captured through laptop camera.But when I press 'q', as mentioned in code, it should stop and terminate the window. But,here the window stops to display any further frames captured and do not terminates.then manually I force Quit the process.

What is the problem, why is it not terminating the window?

CODE:-

import cv2

import numpy as np

cap = cv2.VideoCapture(0)

while(True):

        ret, frame = cap.read()

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

cap.release()

cv2.destroyAllWindows()
Xantium
  • 11,201
  • 10
  • 62
  • 89
  • Probably the window is stealing the focus, so when you press "q" is not captured where it should. could you click on the console, so it focus and then press "q" there? –  Feb 19 '18 at 14:36
  • I clicked on it, and pressed ''q''. Just the video stopped and the window is not closing automatically.I manually close it by click the close button then force quit. – Abhijeet tripathy Feb 19 '18 at 14:40

3 Answers3

4

There is an open bug with this issue:

https://github.com/opencv/opencv/issues/7343

There are also similar questions without a good solution:

opencv videocapture hangs/freeze when camera disconnected instead of returning "False"

Try the solutions here: DestroyWindow does not close window on Mac using Python and OpenCV Calling several times waitKey seems to work for many people. You can try without the release() as well

user2518618
  • 1,360
  • 13
  • 32
  • Cool...waitKey(1) is a loop or waitKey(1)m at the fourth time made it...!!So the final code is cap.release() cv2.destroyAllWindows() cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) – Abhijeet tripathy Feb 20 '18 at 03:32
0

I use macOS BigSur and also had this issue, I ended my code with the lines below and had no problem later:

video = cv.VideoCapture("your_video.mp4") # or 0 for your camera
while(video.isOpened()):
    ret, frame = video.read()
    if ret:

        # Your frame manipulations

        if cv.waitKey(1) & 0xFF == ord('q'):
            video.release()
            cv.waitKey(0)
            cv.destroyAllWindows()
            break
    else:
        break
video.release()
cv.waitKey(0)
cv.destroyAllWindows()

This will destroy the freezed window of the video but you have to press 3 times Q. One por stopping the video, one more for closing the window and one last for releasing the kernel if you're running on the Jupiter Notebook. I also recommend to restart and clear all outputs in the kernel tab after applying the code because it could not work if so.

-1

apparently this solves the issue, just include this line on top: import pyautogui

Bob Bobster
  • 95
  • 1
  • 9
  • doesnt work on ventura; please provide an explanation of why importing a library with no other commands will help solve this issue – anonymousaga Jan 08 '23 at 05:31