PaddlePaddle通过pyinstaller打包出错

  • 1 找不到资源问题和matplotlib报错

(1)matplotlib报错,通过 --exclude 屏蔽matplotlib

--exclude matplotlib

或者直接修改spec文件中的     excludes=['matplotlib']

(2)资源找不到,通过打包 --add-binary --add-data 解决

--add-binary --add-data C:\opencode\chk\model;.\model --additional-hooks-dir=.

或者直接修改spec文件中的

binaries=[('E:\\2022\\ImageOCR\\venv\\Lib\\site-packages\\paddle\\libs', './paddle/libs')],
 datas=[('E:\\2022\\ImageOCR\\model', './model')],

  • 2 进程无线启动问题

  • 2.1 分析

经过多次排除法尝试,只要存在以下语句"from paddleocr import PaddleOCR"就会导致进程不停启动 通过命令行运行打包进程“txt.exe", 手动强杀进程(Ctrl+C)发现以下报错:

c:\opencode\ocr\textshot_paddle>C:\opencode\ocr\textshot_paddle\dist\txt\txt.exe
Traceback (most recent call last):
  File "txt.py", line 200, in 
    out, err = import_cv2_proc.communicate()
  File "subprocess.py", line 964, in communicate
  File "subprocess.py", line 1296, in _communicate
  File "threading.py", line 1044, in join
  File "threading.py", line 1060, in _wait_for_tstate_lock
KeyboardInterrupt
[448] Failed to execute script txt

于是查看 paddle\dataset\image.py 代码,发现200行如下

if six.PY3:
    import subprocess
    import sys
    import_cv2_proc = subprocess.Popen(
        [sys.executable, "-c", "import cv2"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    out, err = import_cv2_proc.communicate()
    retcode = import_cv2_proc.poll()
    if retcode != 0:
        cv2 = None
    else:
        import cv2
else:
    try:
        import cv2
    except ImportError:
        cv2 = None
  • 2.3 解决方案

解决方案简单粗暴,修改image.py 39行开始代码,屏蔽subprocess调用

# if six.PY3:
#     import subprocess
#     import sys
#     import_cv2_proc = subprocess.Popen(
#         [sys.executable, "-c", "import cv2"],
#         stdout=subprocess.PIPE,
#         stderr=subprocess.PIPE)
#     out, err = import_cv2_proc.communicate()
#     retcode = import_cv2_proc.poll()
#     if retcode != 0:
#         cv2 = None
#     else:
#         import cv2
# else:
#     try:
#         import cv2
#     except ImportError:
#         cv2 = None
try:
    import cv2
except ImportError:
    cv2 = None

问题解决。

你可能感兴趣的:(python,matplotlib,numpy,paddlepaddle)