After using a mouse callback (python) matplotlib fails to open

asked 2018-01-11 05:28:11 -0500

the_phet gravatar image

I have the following class which I use to read clicks from the user. I have removed some of the documentation and code to keep it simple. The important part here is the function "get_platform_corners" which I think it is what makes the code fail later when I try to load matplotlib. This code in isolation (when not using matplotlib) works fine.

class GridClickData:

        def __init__(self):
                self.drawing = False # True if mouse is pressed
                self.ix, self.iy = -1, -1 # first click, or top left corner
                self.points = [0, 0, 0, 0] # Platform coordinates
                self.finished = False # True when user releases click

        def grid_callback(self, event, x, y, flags, param):
                '''mouse callback function. See OpenCV documentation example'''

                if event == cv2.EVENT_LBUTTONDOWN:
                        self.drawing = True
                        self.points = [x, y, x, y]
                        self.ix, self.iy = x,y

                elif event == cv2.EVENT_MOUSEMOVE:
                        if self.drawing == True:
                                self.points = [self.ix, self.iy, x, y]

                elif event == cv2.EVENT_LBUTTONUP:
                        self.drawing = False
                        self.points = [self.ix, self.iy, x, y]
                        self.finished = True

        def draw_grid(self, frame):
                '''draws a 5x5 grid, just the lines. Used for visualization'''

        def get_platform_corners(self, frame):

                cv2.namedWindow('Choose grid')
                cv2.setMouseCallback('Choose grid', self.grid_callback)
                unmodified = frame.copy()

                # grid_callback sets finished to True once the user selects both corners
                while(self.finished is False):
                        frame = unmodified.copy()
                        self.draw_grid(frame)
                        cv2.imshow('Choose grid', frame)
                        cv2.waitKey(10)

                cv2.destroyWindow('Choose grid')

Later on, in another script I use this code:

click_grid = GridClickData()

 if click_grid.finished is False:
        click_grid.get_platform_corners(frame)

It will fill the click_grid object with some x y coordinates which I will use to draw stuff. This works fine.

The problem is when I try to use matplotlib. just like:

plt.plot(cXs, cYs)

(plt is matplotlib.pyplot)

Will fail and return:

(Choose grid:1956): GLib-GObject-WARNING **: cannot register existing type 'GdkDisplayManager'

(Choose grid:1956): GLib-CRITICAL **: g_once_init_leave: assertion 'result != 0' failed

(Choose grid:1956): GLib-GObject-CRITICAL **: g_object_new_with_properties: assertion 'G_TYPE_IS_OBJECT (object_type)' failed

Because the error comes from "CHoose grid" which is the namedWindow I used to ask the user for input, and where I attached the callback, I suspect this is not freed as it should, it is lock some resource, and then when matplotlib wants to take it, it cannot, because it is already being used.

I have tried to delete the callback after being used, among many other things, but nothing seems to work, and I always get this error when trying to launch matplotlib.

Thanks for reading this

edit retag flag offensive close merge delete

Comments

i don't think, there's an easy solution, here.

(Choose grid:1956): GLib-GObject-WARNING **: cannot register existing type 'GdkDisplayManager'

so, opencv hogs the display manager. and cv2.destroyWindow('Choose grid') will close the window, but not release the manager, so you cannot use matplotlib in the same program.

oh, wait, have you tried: cv2.destroyAllWindows() ?

berak gravatar imageberak ( 2018-01-11 05:36:30 -0500 )edit

@berak before using destroyWindow I was using destroyAllWindows, which also returned the same error.

the_phet gravatar imagethe_phet ( 2018-01-11 05:41:09 -0500 )edit

yea, thought so. not an answer ...

berak gravatar imageberak ( 2018-01-11 05:47:46 -0500 )edit