4

I installed opencv-python on ubuntu wsl, after setting up a venv using virtualenvwrapper (I use wsl in visual studio code). When running this code (which appears in one of the articles of this OCR guide:

import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True)
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("I", image)

with this command on teminal:

python script.py --image temp.png

I get:

qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/ben123/.local/bin/.virtualenvs/ocr_venv/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

The interpreter in vscode is the correct one (the one of the venv), and when I type pip list I get

Package       Version
------------- --------
numpy         1.22.2
opencv-python 4.5.5.62
pip           22.0.3
setuptools    60.6.0
wheel         0.37.1

Would appreciate any help at this point, since I spent so much time and didn't get nowhere.

Things I tried:

  1. following this guide to install it. Gave the same error.
  2. following an older guide from this site, was much more complicated and didn't work as well.
  3. uninstalling opencv-python and installing opencv-python again/ opencv-python-contrib/ opencv-python-headless/ opencv-python-contrib-headless (only one of them at a time)
  4. following this thread because it has similar problem
  5. literally reset my wsl several times just to make sure I don't have multiple pythons/ opencv versions that mess this up.
  6. tried installing (to a wsl venv) opencv directly with the official documentation
  7. Tried to give up on wsl completely and install opencv using anaconda but even that didn't work.
benjamin
  • 304
  • 4
  • 13

5 Answers5

5

I had the same error in a completely different context.

Found that the problem was a PyQt5 installation in my virtual environment. Check if you have a PyQt in the path

/home/ben123/.local/bin/.virtualenvs/ocr_venv/lib/python3.8/site-packages/

if so, remove it

$ pip uninstall <PyQT package installed>

example:

$ pip uninstall PyQt5

Then reinstall opencv-python

$ pip uninstall opencv-python
$ pip install opencv-python

Hope that works!

Shei Pi
  • 51
  • 2
  • 4
  • Couldn't find it in the dir: PIL, pip, Pillow-9.0.1.dist-info, pip-22.0.3.dist-info, Pillow.libs, pip-22.0.3.virtualenv, __pycache__, pkg_resources, _distutils_hack, pycodestyle-2.8.0.dist-info, _virtualenv.pth, pycodestyle.py, _virtualenv.py, pytesseract, autopep8-1.6.0.dist-info, pytesseract-0.3.8.dist-info, autopep8.py, setuptools, cv2, setuptools-60.6.0.dist-info, distutils-precedence.pth, setuptools-60.6.0.virtualenv, numpy, toml, numpy-1.22.2.dist-info, toml-0.10.2.dist-info, numpy.libs, opencv_contrib_python_headless-4.5.5.62.dist-info, opencv_contrib_python_headless.libs and wheel – benjamin Apr 19 '22 at 09:32
  • Is there anything else I can try? – benjamin Apr 19 '22 at 09:33
  • Can you post the content of the "site_packages" dir or the output of "pip freeze"? There must be something there trying to use Qt and crashing. We need to reinstall that. – Shei Pi Apr 19 '22 at 12:35
  • pip freeze: autopep8==1.6.0 numpy==1.22.2 opencv-contrib-python-headless==4.5.5.62 Pillow==9.0.1 pycodestyle==2.8.0 pytesseract==0.3.8 toml==0.10.2 – benjamin Apr 22 '22 at 08:45
5

Uninstalling opencv and installing similar headless version worked for me.

$ pip install opencv-python-headless
2

Just delete cv2.imshow from your code. Your OS is without graphics and can't display image

1

To display graphical information about wsl, you should configure x11 related content. eg: You can use MobaXterm for graphical display.

G.Chenhui
  • 11
  • 2
0

Using a miniconda virtual environment, here is how I did it:

First, I uninstalled opencv:

pip uninstall opencv-python

Then, I checked if I could find PyQt files in this path :

~/miniconda3/lib/python3.10/site-packages/

I found these:

  • PyQt5
  • PyQt5_sip-12.11.0-py3.10-linux-x86_64.egg
  • PyQt5_sip.pth

Which I removed using the rm -r <file name> command (because for some reason I could not do that using pip or conda).

After that, I reinstalled opencv-python:

pip install opencv-python
Droidux
  • 146
  • 2
  • 12