问题描述
我在 macos 上通过 pyinstaller 使用 mediapipe 编译项目时遇到问题
到目前为止我尝试过:
pyinstaller --windowed --noconsole pose_edge.py
pyinstaller --onefile --windowed --noconsole pose_edge.py
pyinstaller --noconsole pose_edge.py
.app 没有打开,如果我尝试 unix exec,我得到
Traceback (most recent call last):
File "pose_edge.py",line 26,in <module>
File "mediapipe/python/solutions/selfie_segmentation.py",line 54,in __init__
File "mediapipe/python/solution_base.py",line 229,in __init__
FileNotFoundError: The path does not exist.
[36342] Failed to execute script pose_edge
我使用 conda,我的环境在 python 3.8、mediapipe 0.8.5 和 OSX 10.15.7 中
提前致谢
解决方法
我也遇到了这个问题,几分钟前才弄明白——到目前为止,我是通过手动方式解决它的,但我确信在 pyinstaller 中使用规范有一种惯用的方法来做到这一点文件和数据导入。对于这个答案,我假设您没有使用 pyinstaller 的 --onefile
选项,而是在单个文件夹中创建二进制文件。
也就是说,答案是 cp -r
安装在虚拟环境中的 mediapipe 中的模块目录(或安装初始 mediapipe 包的任何地方,例如 /virtualenvs/pose_record-2bkqEH7-/lib/python3.9 /site-packages/mediapipe/modules) 进入您的 dist/main/mediapipe
目录。这将使您的捆绑 mediapipe 库能够访问 binarypb 文件,我认为这些文件包含姿势检测算法的图形和权重。
更新:我想出了一个更惯用的 pyinstaller 方法来让它运行。在pyinstaller生成的.spec
文件中,您可以通过以下方式自动添加文件:
在文件顶部的 block_cipher = None
下,添加以下函数:
def get_mediapipe_path():
import mediapipe
mediapipe_path = mediapipe.__path__[0]
return mediapipe_path
然后,在以下几行之后:
pyz = PYZ(a.pure,a.zipped_data,cipher=block_cipher)
添加以下使用 native Tree class to create a TOC for the binary 的行:
mediapipe_tree = Tree(get_mediapipe_path(),prefix='mediapipe',excludes=["*.pyc"])
a.datas += mediapipe_tree
a.binaries = filter(lambda x: 'mediapipe' not in x[0],a.binaries)
添加后,您可以从 CLI 运行编译命令,例如:
pipenv run pyinstaller --debug=all main.spec --windowed --onefile
这使我能够构建一个适用于 mediapipe 的可执行文件。