EasyOCR在Python代码中运行正常,转EXE后报错求助
解决EasyOCR打包EXE后OpenCV报错问题
问题详情
Python环境下以下EasyOCR文字识别代码可正常运行,但通过auto-py-to-exe转换为EXE文件后出现OpenCV相关报错。
运行正常的Python代码
import easyocr cropped_image_path = 'D:\\Desktop\\screens\\screens\\1.png' reader = easyocr.Reader(['en']) easy_results = reader.readtext(cropped_image_path) print(easy_results) print("Extracted text is: ", easy_results[0][1])
EXE运行报错信息
Neither CUDA nor MPS are available - defaulting to CPU. Note: This module is much faster with a GPU.
Traceback (most recent call last): File "easyocr_check.py", line 11, in <module> File "easyocr\\easyocr.py", line 452, in readtext File "easyocr\\easyocr.py", line 321, in detect File "easyocr\\detection.py", line 95, in get_textbox File "easyocr\\detection.py", line 55, in test_net File "easyocr\\craft_utils.py", line 236, in getDetBoxes File "easyocr\\craft_utils.py", line 31, in getDetBoxes_core cv2.error: Unknown C++ exception from OpenCV code [8260] Failed to execute script 'easyocr_check' due to unhandled exception!
使用版本
- Python v3.9
- easyocr v1.7.0
- opencv-python v4.7.0.72
- auto-py-to-exe v2.36.0
Python环境预期输出
[([[23, 5], [59, 5], [59, 19], [23, 19]], '21.23', 0.9985201957308608)] Extracted text is: 21.23
解决方案
降级OpenCV版本
高版本opencv-python打包后易出现兼容性问题,降级到4.5.x稳定版本,执行命令:pip install opencv-python==4.5.5.62卸载原版本后重新安装,再重新打包。
手动指定EasyOCR模型路径
打包时可能未正确包含模型文件,手动下载模型到本地目录,修改代码指定模型存储路径:import easyocr cropped_image_path = 'D:\\Desktop\\screens\\screens\\1.png' # 替换为你本地的模型文件夹路径 reader = easyocr.Reader(['en'], model_storage_directory='./easyocr_models') easy_results = reader.readtext(cropped_image_path) print(easy_results) print("Extracted text is: ", easy_results[0][1])打包时将
easyocr_models文件夹与EXE放在同一目录,或在auto-py-to-exe的Additional Files中添加该文件夹。调整auto-py-to-exe打包参数
- 选择One Directory模式打包(不选One File),依赖文件会全部存放在同一文件夹,减少路径解析问题。
- 在Additional Files中添加EasyOCR默认模型目录(通常为
C:\Users\<你的用户名>\.EasyOCR\model),确保打包后模型文件能被EXE访问。
修正图像路径问题
打包后EXE的工作路径可能与Python环境不同,避免硬编码绝对路径,改为让用户选择图像文件:import easyocr from tkinter import filedialog cropped_image_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg")]) reader = easyocr.Reader(['en']) easy_results = reader.readtext(cropped_image_path) print(easy_results) print("Extracted text is: ", easy_results[0][1])
内容的提问来源于stack exchange,提问作者Test One
相关产品推荐
相关产品推荐

