24

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?

1

3 Answers 3

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

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

Comments

15

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.

6 Comments

This solution/answer is covered by the already existing answer from @SimonHänisch. Having an example here maybe helpful though.
@IvoMori Not quite. The directly linked answer is using enumeration 0, while I'm using 4 in the second argument. Link to that here 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).
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.
Eventually someone posted the example which works for me .. Thanks man
Anyone know of a similar solution for Java? The getWindowsProperty method does not seem to exist anywhere
|
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 ;)

2 Comments

Thanks, I haven't noticed that post. Yes, I'm using Window so it should be easy.
This should be a feature in opencv what else would that button be used for?

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.