ROI selection cv2.selectROI not working in Google Colab

I’m learning opencv for object detection and trying to do all the coding using google colab. And so far it has been working well: I can mount the gdrive and upload file and read the video file just fine.

The selecting ROI by passing video frame into cv2.selectROI() crashed the Colab. The runtime log complained about a plugin xcb not initialising. Further reading reveals the plug in require to be run on local machine, so I wonder if there is a way to run cv2.selectROI or equivalent on colab?

that’s expected. you cannot use ANY opencv gui functions (imshow, waitkey, mouse, trackbars) on colab, it is remote server.

1 Like

Sorry I’m very new to all these.
IS there any work around to this? for example in this case I want to label a ROI in a video using openCV. Or my option really is start using pycharm?

no. you misunderstand.

pycharm has NOTHING to do with this.

your mistake is expecting GUI from a server on the internet.

run the stuff locally. this DOES NOT REQUIRE PYCHARM, okay? it just requires python and opencv, on your local machine.

pycharm is not equal to python, nor is it equal to having python on your own machine.

1 Like

my bad, when I say pycharm I mean to run python on my local machine, my IDE is pycharm…you are absolutely right though, run it on my local machine would be the easiest fix.

I read around on StackOverflow, and found out the same problem with cv2.imshow(), and there seems to have a workaround using

from google.colab.patches import cv2_imshow
so I wonder if there is a workaround like that for roi selection? I know this is like taking the long road, but for the sake of learning…

If you really don’t want to run python locally and prefer notebook-like GUI, you can read this article about selecting ROI with jupyter (since Google Colab is a hosted Jupyter Lab instance)

3 Likes

Thank you so much man!
This might just be the thing I’m looking for. I’m a dummy so before I never paid attention to the difference between colab/notebook and a local IDE… Great things to learn.