Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyPi version 4.3.0.36 sometimes crashes on cv2.setMouseCallback #356

Closed
anqixu opened this issue Jul 7, 2020 · 5 comments
Closed

PyPi version 4.3.0.36 sometimes crashes on cv2.setMouseCallback #356

anqixu opened this issue Jul 7, 2020 · 5 comments

Comments

@anqixu
Copy link

anqixu commented Jul 7, 2020

opencv/opencv#17767

Expected behaviour

  1. create named window
  2. set mouse callback
  3. program keep running

Actual behaviour

On 4.3.0.36 version, sometimes after step 2, OpenCV crashes:

cv2.error: OpenCV(4.3.0) /io/opencv/modules/highgui/src/window_QT.cpp:717: error: (-27:Null pointer) NULL window handler in function 'cvSetMouseCallback'.

Note that this symptom does not occur on version 4.2.0.34.

Steps to reproduce

  • example code
# Code from official docs: https://docs.opencv.org/master/db/d5b/tutorial_py_mouse_handling.html
import numpy as np
import cv2 as cv
# mouse callback function
def draw_circle(event,x,y,flags,param):
    if event == cv.EVENT_LBUTTONDBLCLK:
        cv.circle(img,(x,y),100,(255,0,0),-1)
# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):
    cv.imshow('image',img)
    if cv.waitKey(20) & 0xFF == 27:
        break
cv.destroyAllWindows()
  • operating system: Ubuntu 18.04
  • architecture (e.g. x86): x64
  • opencv-python version: 4.3.0.36
@skvark
Copy link
Member

skvark commented Jul 7, 2020

The base manylinux image was changed between those versions. Qt version is the same 4.8.7.

@skvark
Copy link
Member

skvark commented Jul 7, 2020

I think this is some kind of race condition in OpenCV code or elsewhere. OpenCV (or Qt) seems to open randomly some other window or something before the actual named window appears. Very odd behavior... I have no time to deep dive into the issue, so here is a short workaround which seems to work reliably and should get you going:

import numpy as np
import cv2

window_name = "test"

def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),100,(255,0,0),-1)

img = np.zeros((512,512,3), np.uint8)

window_open = False
# just try as long as a matching window is found
while not window_open:
    try:
        cv2.namedWindow(window_name)
        cv2.setMouseCallback(window_name, draw_circle)
        window_open = True
    except:
        # destroy any "erroneous" windows OpenCV might have created
        cv2.destroyAllWindows()
    	
while(1):
    cv2.imshow(window_name, img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
        
cv2.destroyAllWindows()

Other alternative is to use some actual Qt wrapper (i.e. PyQt) / GUI toolkit to create UIs. OpenCV's GUI functionality is quite limited and imho useful only for debugging purposes.

@anqixu
Copy link
Author

anqixu commented Jul 7, 2020

I agree on try-except work-around, as an alternative to using another version, like opencv-python==4.2.0.34.

As for the practicality of HighGUI, note that it's being built upon by some larger-scale projects in the community. Notably, I encountered this github issue originally when trying to make GibsonV2 work. I personally would also use PyQt instead, but I can understand the developers' wish to go fast on their main contributions (a robot simulation environment) and delegate responsibility to other libraries.

@skvark
Copy link
Member

skvark commented Jul 7, 2020

This issue might get solved if I manage to update the Linux wheels to Qt5. As there are workarounds, I'm not planning to debug this further unless someone points me directly to the root cause. I'll keep the issue open so I remember to test it in the future in new versions.

@skvark
Copy link
Member

skvark commented Jul 28, 2020

Closing, please see #362

@skvark skvark closed this as completed Jul 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants