You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.11使用EasyOCR时出现OpenCV相关错误的解决方法

问题

运行以下Python OCR脚本时遇到两类错误:

  1. cv2.error:提示cvShowImage函数未实现,需重建带Windows/GTK+ 2.x或Cocoa支持的OpenCV库
  2. AttributeError:导入cv2时提示模块因循环导入未完全初始化,具体错误为AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'

脚本代码

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)

完整回溯信息

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)

解决方案

1. 解决cv2循环导入的AttributeError

该错误多由OpenCV与Python版本不兼容、安装包损坏或环境冲突导致,解决步骤:

  • 卸载当前OpenCV相关包:
    pip uninstall opencv-python opencv-contrib-python -y
    
  • 安装与Python版本匹配的稳定版OpenCV,以Python3.10为例:
    pip install opencv-python==4.5.5.62 opencv-contrib-python==4.5.5.62
    
    其他Python版本可选择对应兼容版本(如Python3.9可选4.5.4.60)
  • 若仍有问题,检查虚拟环境是否干净,避免跨环境包冲突。

2. 解决cvShowImage未实现的错误

该错误因OpenCV库缺失GUI支持导致,两种解决方式:

  • 使用带GUI的预编译包:上述步骤安装的官方opencv-python包默认包含Windows GUI支持,版本兼容即可解决。
  • 替换GUI显示方式:若不想依赖OpenCV的GUI,可改用matplotlib显示图像,修改脚本末尾代码:
    # 替换原有的cv2.imshow和cv2.waitKey
    import matplotlib.pyplot as plt
    # OpenCV读取的是BGR格式,转成RGB后显示
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
    

内容的提问来源于stack exchange,提问作者TanukiJoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 12:30:54