Search code examples
pythonqgis

QGIS terminates on importing opencv in python


My QGIS software terminates if I run "import cv2" in python console. The QGIS version is 3.6 Noosa.


Solution

  • I encountered the same issue with Python 3.7.3, QGIS 3.8.2, OpenCV 4.1.0.25, and Ubuntu 19.04. When attempting to import cv2 QGIS freezes, then silently exits. I tested the import with my system level interpreter, (the same one QGIS uses), and it worked fine.

    My solution was to uninstall OpenCV:

    python3 -m pip uninstall opencv-contrib-python

    Then to install headless OpenCV:

    python3 -m pip install opencv-contrib-python-headless

    The cost of this solution is the loss of cv2.imshow() and other GUI functionality. One can simply output to a file, if needed, to inspect results.

    The information here explains a bit more about the headless install and mentions that it is "suitable for more restricted environments."