0

I have the following Python script that raises the error "cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1274: 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'

import cv2

from easyocr import Reader

import argparse

from matplotlib.image import imread

 

def cleanup_text(text):

    return "".join([c if ord(c) < 128 else "" for c in text]).strip()

 

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--image", required=False, help="path to input image to be OCR'd")

ap.add_argument("-l", "--langs", type=str, default="en", help="comma separated list of languages to OCR")

ap.add_argument("-g", "--gpu", type=int, default=1, help="whether or not GPU should be used")

args = vars(ap.parse_args())

 

langs = args["langs"].split(",")

print("[INFO] OCR'ing with the following languages: {}".format(langs))

 

#image = cv2.imread(args["image"])

image = cv2.imread('Capture1.jpg')

 

print("[INFO] OCR'ing input image...")

reader = Reader(langs, gpu=args["gpu"] > 0)

results = reader.readtext(image)

 

for (bbox, text, prob) in results:

    print("[INFO] {}".format(prob, text))

 

    (tl, tr, br, bl) = bbox

    tl = (int(tl[0]), int(tl[1]))

    tr = (int(tr[0]), int(tr[1]))

    br = (int(br[0]), int(br[1]))

    bl = (int(bl[0]), int(bl[1]))

 

    text = cleanup_text(text)

    cv2.rectangle(image, tl, br, (0, 255, 0), 2)

    cv2.putText(image, text, (tl[0], tl[1] - 10),

        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)

   

cv2.imshow("Image", image)

cv2.waitKey(0)

Full traceback:

Traceback (most recent call last):

  File "c:\Users\1\.register\OCRin.py", line 1, in <module>

    import cv2

  File "C:\Users\1\.register\lib\site-packages\cv2\__init__.py", line 181, in <module>

    bootstrap()

  File "C:\Users\1\.register\lib\site-packages\cv2\__init__.py", line 175, in bootstrap

    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):

  File "C:\Users\1\.register\lib\site-packages\cv2\__init__.py", line 28, in __load_extra_py_code_for_module

    py_module = importlib.import_module(module_name)

  File "C:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "C:\Users\1\.register\lib\site-packages\cv2\gapi\__init__.py", line 301, in <module>

    cv.gapi.wip.GStreamerPipeline = cv.gapi_wip_gst_GStreamerPipeline

AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (most likely due to a circular import)
TanukiJoe
  • 11
  • 5
  • Better print the full exception traceback if an exception occurs with "traceback.print_exc()" in the except-block. Show the result as properly formatted text in the question. – Michael Butscher Jan 18 '23 at 09:41
  • Can you elaborate please? – TanukiJoe Jan 18 '23 at 09:54
  • Due to the try-except you catch an exception but only print incomplete information about it. Either remove the try-except construction or use the function I mentioned above. – Michael Butscher Jan 18 '23 at 09:59
  • I rewrote the script but the error remains: – TanukiJoe Jan 18 '23 at 10:02
  • The change was meant to receive more information about the error. Show the full traceback as properly formatted text in the question. – Michael Butscher Jan 18 '23 at 10:06
  • You could try the conversion described at https://stackoverflow.com/a/55081908/987358 or you give the reader the file path instead of the image: `results = reader.readtext(args["image"])` – Michael Butscher Jan 18 '23 at 10:48
  • @MichaelButscher I've tried the conversion described at the url you've suggested but this still raises an error: "The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. I will soon edit and upgrade the question with the new full traceback. – TanukiJoe Jan 18 '23 at 11:30
  • How did you install Opencv? Downloaded as pre-built binary from the site referred by the official website https://docs.opencv.org/4.5.4/d5/de5/tutorial_py_setup_in_windows.html , using pip or somehow else? – Michael Butscher Jan 18 '23 at 20:16
  • @MichaelButscher I pip installed it within a venv virtual environment – TanukiJoe Jan 19 '23 at 07:29
  • This rather old version of Python-OpenCv had no precompiled binary for Python 3.11, therefore it was compiled locally on your system, probably with wrong settings. Simplest solution would be to update the version to the latest one using pip. – Michael Butscher Jan 19 '23 at 09:15
  • @MichaelButscher I updated python-opencv to the latest version and now the script raises the following error: AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (most likely due to a circular import). I'm currently using python 3.10.1 – TanukiJoe Jan 19 '23 at 12:53
  • Show the full traceback of the error as properly formatted text in the question. – Michael Butscher Jan 19 '23 at 21:52
  • @MichaelButscher Full traceback added to the question – TanukiJoe Jan 20 '23 at 07:18
  • Seems to be a known issue depending on the installed version of OpenCV: https://stackoverflow.com/questions/72706073/attributeerror-partially-initialized-module-cv2-has-no-attribute-gapi-wip-gs Unfortunately I can't provide a reliable solution. – Michael Butscher Jan 20 '23 at 15:32

0 Answers0