Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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