程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python] using pyinstaller to package pytorch program with interface

編輯:Python

1 opencv Compatibility issues

1.1 The phenomenon

Pack it up as a exe After completion , In execution exe when , Report errors :
ImportError: ERROR: recursion is detected during loading of “cv2” binary extensions. Check OpenCV installation.
As shown in the figure below :

1.2 Causes and Solutions

pyinstaller and cv2 Compatibility issue with version , Uninstall the existing opencv-python, install opencv-python=4.5.3.56

I installed python3.7,pyinstaller5.1,opencv The version is high .

See here :https://blog.csdn.net/weixin_50850903/article/details/125231985

2 lack yolov5 Next two documents general.pyc and coco128.yaml

2.1 The phenomenon

Pack it up as a exe After completion , In execution exe when , Report errors : The lack of general.pyc and coco128.yaml. As shown in the figure below .

2.2 Causes and Solutions

yolov5 These two should be used in , But it doesn't include .
(1)general.pyc For binary , by general.py Compiled format , stay VSCode in , Run project files ,python Run in general.py when , This is automatically generated pyc file , This file is located under the folder .


But what is generated here is general.cpython-37.pyc, No general.pyc, This may also be the reason why we can't find it .
Rename it to general.pyc that will do .
And put him in utils below .
(2)coco128.yaml File in text format . There are in the project , stay data\ below . Just use it directly .

resolvent , stay spec Just include these two files in .
spec The definition is as follows :

a = Analysis(['D:XXX\\detect.py'],
pathex=[],
binaries=[(r'D:\XXX\XX\utils\general.pyc', r'.\utils' ) # Copy , Note that the destination folder of the copy must be consistent with the path missing above .
],
datas=[(r'D:\XXX\data\coco128.yaml', r'.\data' )], # Copy , Note that the destination folder of the copy must be consistent with the path missing above .
hiddenimports=[],
hookspath=[],
hooksconfig={
},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)

3 lack yolov5 Next two documents general.pyc and coco128.yaml

3.1 The phenomenon

function exe when , Can run , But there was an alarm :
WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp_MEI132522\torch_C.cp37-win_amd64.pyd.
When this is run from the command line , The hint is nothing . Because I made an interface program , Every time it starts , Pop up prompt , As shown in the figure below , It's uncomfortable .

3.2 Causes and solutions

I don't know pytorch Its own problems . This is to say , There is one more file in the text file , There shouldn't have been , But with .
resolvent : stay spec In file , Add a search , From binary referenced files , Get rid of this . as follows :

a = Analysis(['D:\XXX\detect.py'],
pathex=[],
binaries=[(r'D:XXX\utils\general.pyc', r'.\utils' )
],
datas=[(r'D:\XXX\data\coco128.yaml', r'.\data' )],
hiddenimports=[],
hookspath=[],
hooksconfig={
},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
for d in a.datas: # Go through it like this , Delete what should not be included .spec Files are actually executable python Code 
if '_C.cp37-win_amd64.pyd' in d[0]:
a.datas.remove(d)
break

Additional explanation :spec Files are actually executable python Code !!!

4 With interface python How does the program remove the command line interface

4.1 The phenomenon

Click packaged exe when , The command line interface appears first , And there has always been , Then there is the interface . At this time, there is a very uncomfortable .

4.2 resolvent

stay spec In file , The configuration is as follows , The command line interface will not pop up :

exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='XXX',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=".",
console=False, # This definition is False, The command window will not pop up 
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )

5 With interface python Program startup interface

5.1 The phenomenon

pack exe Program , This pytorch In the framework of , I packed all the related packages , Yes 3.9 individual G size , start-up exe, Unzip it until the interface pops up , It's been a long time , There is no command line prompt , Users don't know the situation .
So ,pyinstaller It has the function of flashing screen , Just load a prompt window first , Can display progress .
ps: This interface also uses tinker Library implementation of .
Method , stay spec Add to file splash object , Define it :

a = Analysis(['D:\CNG_Work\LostDistrictIdentify\\detect.py'],
pathex=[],
binaries=[(r'D:\CNG_Work\LostDistrictIdentify\utils\general.pyc', r'.\utils' )
],
datas=[(r'D:\CNG_Work\LostDistrictIdentify\data\coco128.yaml', r'.\data' )],
hiddenimports=[],
hookspath=[],
hooksconfig={
},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
for d in a.datas:
if '_C.cp37-win_amd64.pyd' in d[0]:
a.datas.remove(d)
break
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
splash = Splash(r'D:XXXX\Assets\CompanyLogo.gif', # Only support PNG Format . In other formats , If installed PIL Will automatically convert 
# If the maximum size is set max_img_size, And installed PIL, When the size is large , Then the conversion and scaling will be performed automatically 
binaries=a.binaries, # there a That's what we defined earlier , This points to his binary file ,
datas=a.datas
text_pos= (1240,405) # Text tip , Prompt the binary file name loaded above #None No display , Or the location of the display (10, 50),
text_default = 'Initializing' # Default prompt 
text_size=12, # font size 
text_color='black'
exe = EXE(pyz,
a.scripts,
splash, # <-- both, splash target, quote splash object 
splash.binaries, # <-- and splash binaries, Displays the contents of the loaded file 
a.binaries,
a.zipfiles,
a.datas,
[],
name='XXX',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=".",
console=False,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )

More about splash Introduce , See also pyinstaller The official introduction .https://pyinstaller.org/en/stable/usage.html?highlight=Splash#splash-screen-experimental
There are several chapters , You can search , Take a look at all .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved