Home > front end >  cv2.imshow() doesnt work when easyocr installed
cv2.imshow() doesnt work when easyocr installed

Time:05-24

I installed easyocr in a newly created python environment using pip install easyocr. Then i installed opencv-python.

when i try to execute the code -

import cv2

img = cv2.imread('2.jpg')
cv2.imshow('sd',img)
cv2.waitKey(0)

It's giving error

OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1268: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

CodePudding user response:

Problem:

If you already have an existing OpenCV version in your system/environment; installing easyOCR can alter that. Going through the requirements.txt file of easyOCR, opencv-python-headless gets installed.

The following excerpt is taken from opencv-python-headless documentation:

Packages for server (headless) environments (such as Docker, cloud environments etc.), no GUI library dependencies

These packages are smaller than the two other packages above because they do not contain any GUI functionality (not compiled with Qt / other GUI components). This means that the packages avoid a heavy dependency chain to X11 libraries and you will have for example smaller Docker images as a result. You should always use these packages if you do not use cv2.imshow et al. or you are using some other package (such as PyQt) than OpenCV to create your GUI.

TLDR;

In short, easyocr disables existing GUI capabilities. It has been designed exclusively for containerized applications and/or server deployments.

Solution:

To use easyOCR with OpenCV, you can try either one of the following:

1. Change installation sequence:

All the following can be done using pip:

  • First install easyocr.
  • Uninstall opencv-python-headless.
  • Install opencv-python

2. Use matplotlib

One can still display images using matplotlib:

import cv2
from matplotlib import pyplot as plt
img = cv2.imread('img.jpg',0)
plt.imshow(img)
plt.show()
  • Related