pyinstaller打包Mediapipe遇到的Failed to execute script

这篇文章主要是记录一下流水账。

背景

IDE使用的是Pycharm。Anaconda3安装的Python版本是3.8,在此基础上通过Pycharm安装了Opencv4.5.2.32和Mediapipe0.8.4.2.
在Pycharm的环境运行python是正常的,但是使用pyinstaller打包成exe,再运行就有问题了。打包过程:

(venv) E:\work\src\python3_src\FaceMeshProject_38>pyinstaller -F -w FaceMeshBasics.py
98 INFO: PyInstaller: 5.0.dev0
99 INFO: Python: 3.8.8
99 INFO: Platform: Windows-10-10.0.19041-SP0
100 INFO: wrote E:\work\src\python3_src\FaceMeshProject_38\FaceMeshBasics.spec
433 INFO: UPX is available.
441 INFO: Extending PYTHONPATH with paths
['E:\\work\\src\\python3_src\\FaceMeshProject_38',
 'E:\\work\\src\\python3_src\\FaceMeshProject_38']
493 INFO: checking Analysis
635 INFO: checking PYZ
696 INFO: checking PKG
776 INFO: Building because E:\work\src\python3_src\FaceMeshProject_38\build\FaceMeshBasics\FaceMeshBasics.exe.manifest changed
776 INFO: Building PKG (CArchive) PKG-00.pkg
18577 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
18615 INFO: Bootloader e:\work\src\python3_src\facemeshproject_38\venv\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
18615 INFO: checking EXE
18671 INFO: Building because icon changed
18671 INFO: Building EXE from EXE-00.toc
18764 INFO: Copying icons from ['e:\\work\\src\\python3_src\\facemeshproject_38\\venv\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-windowed.ico']
18853 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
18853 INFO: Writing RT_ICON 1 resource with 3752 bytes
18853 INFO: Writing RT_ICON 2 resource with 2216 bytes
18853 INFO: Writing RT_ICON 3 resource with 1384 bytes
18853 INFO: Writing RT_ICON 4 resource with 38188 bytes
18854 INFO: Writing RT_ICON 5 resource with 9640 bytes
18854 INFO: Writing RT_ICON 6 resource with 4264 bytes
18854 INFO: Writing RT_ICON 7 resource with 1128 bytes
18872 INFO: Updating manifest in E:\work\src\python3_src\FaceMeshProject_38\build\FaceMeshBasics\runw.exe.eyk_sfod
18925 INFO: Updating resource type 24 name 1 language 0
18948 INFO: Appending archive to EXE E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics.exe
31855 INFO: Building EXE from EXE-00.toc completed successfully.

后双击运行提示:

在这里插入图片描述
为了得到更多得的错误信息重新打包,这次不使用-w选项,也不使用-F,目的是把所有需要打包的文件分散的放到一个文件夹里面,这样可以看到都有哪些文件被打包进来了,方便查找和对比。打包完之后再运行,得到的结果是:

E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics>FaceMeshBasics.exe
Traceback (most recent call last):
  File "FaceMeshBasics.py", line 21, in <module>
  File "mediapipe\python\solutions\face_mesh.py", line 205, in __init__
  File "mediapipe\python\solution_base.py", line 231, in __init__
FileNotFoundError: The path does not exist.
[944] Failed to execute script FaceMeshBasics
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics>

到这里可以看到有:FileNotFoundError的提示,但是不知道具体是哪个文件没有找到,跟踪到上面提到的mediapipe\python\solution_base.py的231行:

   if binary_graph_path:
      validated_graph.initialize(
          binary_graph_path=os.path.join(root_path, binary_graph_path))
    else:
      validated_graph.initialize(graph_config=graph_config)

上面的文件路径是在工程目录的下的venv\Lib\site-packages\mediapipe\python里面。提示中的231行是validdated_graph.initialize()这一行,找到这个文件看了一下,表示看不懂,先加一行print()打印看看:

    root_path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-3])
    resource_util.set_resource_dir(root_path)
    validated_graph = validated_graph_config.ValidatedGraphConfig()
    binary_graph_path = os.path.join(root_path, binary_graph_path)
    print("test--q:binary_graph_path=%s"%(binary_graph_path))
    if binary_graph_path:
      validated_graph.initialize(
          binary_graph_path=os.path.join(root_path, binary_graph_path))
    else:
      validated_graph.initialize(graph_config=graph_config)

在Pycharm里面运行后的提示:

E:\work\src\python3_src\FaceMeshProject_38\venv\Scripts\python.exe E:/work/src/python3_src/FaceMeshProject_38/FaceMeshBasics.py
test--:binary_graph_path=E:\work\src\python3_src\FaceMeshProject_38\venv\lib\site-packages\mediapipe/modules/face_landmark/face_landmark_front_cpu.binarypb

打包后,在cmd的命令行中运行提示:

E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics>FaceMeshBasics.exe
test--q:binary_graph_path=E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics\mediapipe/modules/face_landmark/face_landmark_front_cpu.binarypb
Traceback (most recent call last):
  File "FaceMeshBasics.py", line 21, in <module>
  File "mediapipe\python\solutions\face_mesh.py", line 205, in __init__
  File "mediapipe\python\solution_base.py", line 231, in __init__
FileNotFoundError: The path does not exist.
[11952] Failed to execute script FaceMeshBasics
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics>

这时发现2个问题:
1.binary_graph_path的字符串中有"\“也有”/",混合着使用来分割目录,不确定这样行不行;但马上又看到在Pycharm里面打印出来的路径也是这样混合用的,猜测应该是可以的。
2.假设1是没有问题的。通过命令行运行时,在打印出来的那个路径中确实是不存在,缺少的是不是就是那个文件face_landmark_front_cpu.binarypb。而在Pycharm环境下运行时打印的路径是存在的。

先不管1,把缺少的文件在打包的时候加进去。使用pyinstaller添加外部资源有很多方法,可以修改spec文件,也可以在打包的命令选项中添加,下面是使用选项–add-data来添加的,格式很简单,中间使用分号分隔,前面是源目录地址,后面是目的目录地址,也就是把源地址的文件复制到目的地址里面去。

(venv) E:\work\src\python3_src\FaceMeshProject_38>pyinstaller  FaceMeshBasics.py --add-data="venv/Lib/site-packages/mediapipe/modules;mediapipe/modules"
85 INFO: PyInstaller: 5.0.dev0
85 INFO: Python: 3.8.8
85 INFO: Platform: Windows-10-10.0.19041-SP0
86 INFO: wrote E:\work\src\python3_src\FaceMeshProject_38\FaceMeshBasics.spec
132 INFO: UPX is available.
141 INFO: Extending PYTHONPATH with paths
['E:\\work\\src\\python3_src\\FaceMeshProject_38',
 'E:\\work\\src\\python3_src\\FaceMeshProject_38']
152 INFO: checking Analysis
278 INFO: Appending 'datas' from .spec
294 INFO: checking PYZ
337 INFO: checking PKG
339 INFO: Building because E:\work\src\python3_src\FaceMeshProject_38\build\FaceMeshBasics\FaceMeshBasics.exe.manifest changed
339 INFO: Building PKG (CArchive) PKG-00.pkg
364 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
366 INFO: Bootloader e:\work\src\python3_src\facemeshproject_38\venv\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
366 INFO: checking EXE
367 INFO: Building because icon changed
368 INFO: Building EXE from EXE-00.toc
370 INFO: Copying icons from ['e:\\work\\src\\python3_src\\facemeshproject_38\\venv\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
437 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
437 INFO: Writing RT_ICON 1 resource with 3752 bytes
437 INFO: Writing RT_ICON 2 resource with 2216 bytes
437 INFO: Writing RT_ICON 3 resource with 1384 bytes
437 INFO: Writing RT_ICON 4 resource with 37019 bytes
438 INFO: Writing RT_ICON 5 resource with 9640 bytes
438 INFO: Writing RT_ICON 6 resource with 4264 bytes
438 INFO: Writing RT_ICON 7 resource with 1128 bytes
440 INFO: Appending archive to EXE E:\work\src\python3_src\FaceMeshProject_38\build\FaceMeshBasics\FaceMeshBasics.exe
1343 INFO: Building EXE from EXE-00.toc completed successfully.
1348 INFO: checking COLLECT
WARNING: The output directory "E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics" and ALL ITS CONTENTS will be REMOVED! Continue? (y/N)y
On your own risk, you can use the option `--noconfirm` to get rid of this question.
11281 INFO: Removing dir E:\work\src\python3_src\FaceMeshProject_38\dist\FaceMeshBasics
11598 INFO: Building COLLECT COLLECT-00.toc
14336 INFO: Building COLLECT COLLECT-00.toc completed successfully.

这时再在命令行下运行就没有错误提示了,然后再根据情况决定是否使用-F打成一个包,这些已经不重要了。

总结

1.使用命令行方式获取错误提示;
2.添加缺少的文件资源;

评论 14 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页

打赏作者

k7arm

请用银子使劲砸我吧

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值