face_recognition: Freeze your script with Pyinstaller
Hi,
You might want to freeze your script into a standalone executable to run on any system without the need of installing python
or face_recognition
and maybe you want to create a demo for your application and want to give it to someone else without giving your source code. Here is a simple tutorial to do that. I tested this method with python3.6
on Windows 10, but I think you can get it working on Linux with a similar method.
- Make sure you have correctly installed both
face_recognition
anddlib
and you see no error when importing them into your script. - Make sure your script works fine, and all its dependencies are right next to it, and you can run it fine with
python yourscript.py
. - Install Pyinstaller with
pip
:pip install pyinstaller
- Create a new directory and move your python script and all dependencies into it. I call it
myproject
andmyscript.py
- Copy
face_recognition_models
andscipy-extra-dll
from your python installed directory to your project directory. - Create an empty file called
<yourscriptname>.spec
likemyscript.spec
next to your python script. - Use below Pyinstaller spec file sample and edit some parts according to your needs: (I mark it with
<>
tag)
# -*- mode: python -*-
block_cipher = None
face_models = [
('.\\face_recognition_models\\models\\dlib_face_recognition_resnet_model_v1.dat', './face_recognition_models/models'),
('.\\face_recognition_models\\models\\mmod_human_face_detector.dat', './face_recognition_models/models'),
('.\\face_recognition_models\\models\\shape_predictor_5_face_landmarks.dat', './face_recognition_models/models'),
('.\\face_recognition_models\\models\\shape_predictor_68_face_landmarks.dat', './face_recognition_models/models'),
]
a = Analysis(['<your python script name.py>'],
pathex=['<path to working directory>'],
binaries=face_models,
datas=[],
hiddenimports=['scipy._lib.messagestream', 'scipy', 'scipy.signal', 'scipy.signal.bsplines', 'scipy.special', 'scipy.special._ufuncs_cxx',
'scipy.linalg.cython_blas',
'scipy.linalg.cython_lapack',
'scipy.integrate',
'scipy.integrate.quadrature',
'scipy.integrate.odepack',
'scipy.integrate._odepack',
'scipy.integrate.quadpack',
'scipy.integrate._quadpack',
'scipy.integrate._ode',
'scipy.integrate.vode',
'scipy.integrate._dop', 'scipy._lib', 'scipy._build_utils','scipy.__config__',
'scipy.integrate.lsoda', 'scipy.cluster', 'scipy.constants','scipy.fftpack','scipy.interpolate','scipy.io','scipy.linalg','scipy.misc','scipy.ndimage','scipy.odr','scipy.optimize','scipy.setup','scipy.sparse','scipy.spatial','scipy.special','scipy.stats','scipy.version'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
a.datas += Tree('./scipy-extra-dll', prefix=None)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='<your python script name>',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )
- Generate your executable with
python -m pyinstaller myscript.spec
- If you see no error then your executable can be found in
dist
directory. - Enjoy!
Thanks to @ageitgey and @davisking for their awesome work.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 49
- Comments: 56 (1 by maintainers)
@sincerefly Hi, If I remember correctly there are some examples that need
scipy
so if you are usingscipy
with your project you need those files but if you don’t, just useface_models
no need forhidden imports
that is it. And remember you just need to first freeze your script normally and if you got any errors about missing packages, you need to feed them manually in yourspec
file.I found this worked very well when running on Windows desktop, but the application would crash on Windows Server 2012 - 2016 with an unhelpful, vague unable to execute DLL error. I found this issue was caused by using the latest version of dlib linked in the project install guide under issue 175 (currently 19.9.99). However, by using ‘pip install dlib’, which installs 19.9.0 (again, currently as of writing this). The frozen executable is running without errors on both desktop and server. I’ve also been testing freezing this in both 32-bit and 64-bit python and both are working. Thanks
thanks for sharing your knowledge…i can’t find scipy-extra-dll …how i can to find them in python directory??i am in ubuntu 16.04, thank you in advance
Cool, thanks for sharing!
I am having this output after freezing my code with this
.spec
file below. When I run my app on console on Linux I get this:Fascee.spec.txt
Finally I solved my problem. As I said I don’t use sklearn on my code but background sklearn was using. Then I add in hiddenimports=[‘sklearn’,‘sklearn.neighbors._typedefs’,‘sklearn.utils._cython_blas’] in my spec file. and it is okey, it solved 😃
@HAKANMAZI Try to run executable with CMD and you should get the error in the console. Then put the error.
You can search extra-dll using below code on cmd dir extra-dll /s /p I hope you will find it in scipy directory. After create scipy-extra-dll directory. Then all dll files transfer to scipy-extra-dll directory.
try to copy the dependencies that open fail to your output dependencies dir, its helpful for me
@masoudr
I’m not find
scipy-extra-dll
in my site-packages, should I need install scipy?1, when I am not install scipy, I can run face_recognition, Is scipy necessary? 2, I install scipy, and copy scipy dir from site-packages to project dir, with
a.datas += Tree('./scipy-extra-dll', prefix=None)
, I can’t findscipy-extra-dll
, but I findextra-dll
in scipy dir, can I writescipy/extra-dll
instead it or notthank you
i replaced datas=[] with datas=[(‘shape_predictor_68_face_landmarks.dat’,‘./face_recognition_models/models’),(‘shape_predictor_5_face_landmarks.dat’,‘./face_recognition_models/models’),(‘mmod_human_face_detector.dat’,‘./face_recognition_models/models’),(‘dlib_face_recognition_resnet_model_v1.dat’,‘./face_recognition_models/models’)] that works,thanks
Thanks! It works