23

I am displaying live video from a camera using OpenCV in Python. This is the code:

capture = cv.CaptureFromCAM(0)
if capture:
    cv.NamedWindow("Live Video", cv.CV_WINDOW_AUTOSIZE)
    frame = cv.QueryFrame(capture)
    if frame:
        cv.ShowImage("Live Video", frame)    
        cv.WaitKey(0)

cv.DestroyWindow("Live Video")

Now, I can only close my video window by pressing "esc", but nothing happens when I click on my window's close "X" button. Is there a way to make that work?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
maupertius
  • 1,518
  • 4
  • 17
  • 30
  • 1
    Possible duplicate of [OpenCV Python: How to detect if a window is closed?](http://stackoverflow.com/questions/35003476/opencv-python-how-to-detect-if-a-window-is-closed) – Simon Hänisch Aug 09 '16 at 09:29

3 Answers3

21

With the cv2 Python module there is a way to do that, I posted the solution here:

https://stackoverflow.com/a/37881722/2897426

This post is just for reference so anyone looking for it can find it

Community
  • 1
  • 1
Simon Hänisch
  • 4,740
  • 2
  • 30
  • 42
14

I had this same issue and I found an easy way to do it:

You can use cv2.getWindowProperty(windowName, cv2.WND_PROP_VISIBLE) to check if the current window is visible, and if it's not you can destroy the window. The method returns a 1 if it is visible and 0 if it is not. Below is an implementation:


while True: 
    _, frame = cap.read()

    cv2.imshow(windowName, frame)
    keyCode = cv2.waitKey(1)

    if cv2.getWindowProperty(windowName, cv2.WND_PROP_VISIBLE) <1:
        break
cv2.destroyAllWindows()

The accepted answer links to a solution that will never work as 0 is included in >=0, and uses the wrong second argument in cv2.getWindowProperty(), while the issues only gets indirectly solved later in the thread. I'm adding this as an answer as I could not find the correct solution when I first visited this thread, and this was exactly what I needed and used.

Carlos Medina
  • 380
  • 2
  • 15
  • This solution/answer is covered by the already existing [answer from @SimonHänisch](https://stackoverflow.com/a/37881827/5698098). Having an example here maybe helpful though. – Ivo Mori Aug 05 '20 at 01:05
  • @IvoMori Not quite. The directly linked answer is using enumeration 0, while I'm using 4 in the second argument. [Link to that here](https://docs.opencv.org/3.4/d7/dfc/group__highgui.html#gaeedf4023e777f896ba6b9ffb156f57b8) He is also using > =0 which catches both 0 and 1. I know that was made aware in the comments, but I still wanted to post a direct solution here. Back to my first sentence, enum 0 doesn't work for me and it didn't work for other people (went through a few blogs online trying to find a solution to OP's problem). – Carlos Medina Aug 05 '20 at 02:14
  • Fair enough, thank you for the clarifications. In that case, you may want to update your answer to incorporate this clarification so that it's clear how your answer is different (and better) than the already existing (and highly rated) one from @SimonHänisch. – Ivo Mori Aug 05 '20 at 02:20
  • Eventually someone posted the example which works for me .. Thanks man – jwpol Oct 29 '20 at 20:29
  • Anyone know of a similar solution for Java? The getWindowsProperty method does not seem to exist anywhere – Peter Kronenberg Apr 26 '21 at 18:53
  • @PeterKronenberg I think you're better off making a separate question about that. – Carlos Medina May 04 '21 at 03:15
4

OpenCV does not have this feature, and only handles key presses by default.

If you want to do this, you must use the handle of the window manager that creates your windows (GTK, QT, ...).

This post describes a similar issue in case you use windows. Let me know if not ;)

César HM
  • 198
  • 2
  • 16
jlengrand
  • 12,152
  • 14
  • 57
  • 87