写过关于图像语义检索的文章,详情可以参看:90.人工智能——基于ImageAI实现图像语义的检索。ImageAI是用的是tensorflow框架,后面改用飞桨框架,用PaddleHub来实现建立图像索引。主要是使用训练好的图像分类模型。
关于飞桨框架打包篇可以参看:111.Python——基于pipenv打包PaddlePaddle的GUI项目。
本文在上面的基础上,增加了一个PaddleHub,PaddleHub即开即用。
记录一下这个PaddleHub PyQt5项目打包的问题。打包总是会遇到很多问题,因为折腾了很久,所以在这里记录一下。
版本:
paddlehub 2.2.0
paddlenlp 2.3.4
paddlepaddle 2.3.1
错误信息如下:
Traceback (most recent call last):
File "ImgCls.py", line 7, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "paddlehub\__init__.py", line 31, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "paddlehub\datasets\__init__.py", line 16, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "paddlehub\datasets\chnsenticorp.py", line 19, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "paddlehub\datasets\base_nlp_dataset.py", line 21, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "paddlenlp\__init__.py", line 25, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "paddlenlp\datasets\__init__.py", line 15, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "paddlenlp\datasets\dataset.py", line 27, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "datasets\__init__.py", line 46, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "datasets\inspect.py", line 30, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "datasets\load.py", line 52, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "datasets\packaged_modules\__init__.py", line 29, in <module>
File "inspect.py", line 973, in getsource
File "inspect.py", line 955, in getsourcelines
File "inspect.py", line 786, in findsource
OSError: could not get source code
根据错误提示,网上查找了一通,但没有解决实际问题。分析错误信息中的提示,发现问题是由paddlenlp引起的,但本项目中没有使用到paddlenlp,所以临时解决思路:
1、修改文件:Lib\site-packages\paddlehub\datasets\ __init__.py,屏蔽以下关于paddlenlp的引用三处。
from paddlehub.datasets.canvas import Canvas
#from paddlehub.datasets.chnsenticorp import ChnSentiCorp
from paddlehub.datasets.esc50 import ESC50
from paddlehub.datasets.flowers import Flowers
from paddlehub.datasets.minicoco import MiniCOCO
#from paddlehub.datasets.msra_ner import MSRA_NER
#from paddlehub.datasets.lcqmc import LCQMC
from paddlehub.datasets.base_seg_dataset import SegDataset
from paddlehub.datasets.opticdiscseg import OpticDiscSeg
2、打包的时候排除:paddlenlp
打开.spec文件,设置excludes=['paddlenlp']
或者直接使用排除模块参数: --exclude-module padelenlp
最后顺利打包成功,生成的exe文件约200MB,基本是可以接受的,虽然还是有些大。脱离环境的运行效果。
说明:问题的根源还是没有找到,如果能有解决该问题的方法,希望能分享出来。
,