pyinstaller-hooks-contrib icon indicating copy to clipboard operation
pyinstaller-hooks-contrib copied to clipboard

OpenCV missing data files in /cv2/data directory

Open michaellee1019 opened this issue 1 year ago • 1 comments

Describe the bug It seems that the data files placed into /cv2/data directory are not being included by PyInstaller. The files are defined in https://github.com/opencv/opencv/tree/master/data/haarcascades and currently an --add-data argument is required in order for the executable to run.

I am technically using Deepface on top of OpenCV but feel like its an issue with the cv2 hook given that the files are included at the Open CV level.

Seems like the existing hook could just include cv2/data/ directory https://github.com/pyinstaller/pyinstaller-hooks-contrib/blob/master/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-cv2.py#L51 ?

  • Which hook/library isn't working? cv2
  • Does the error get raised while building or when running? running

To Reproduce

A minimal example file:

from deepface import DeepFace
results = DeepFace.extract_faces(img_path=numpy.array(image.convert('RGB')),enforce_detection=False, detector_backend='ssd')
print(results)

PyInstaller command: Using this command resolves the issue

python3 -m PyInstaller --add-data /root/project/.venv/lib/python3.11/site-packages/cv2/data:cv2/data src/main.py

Error:

ValueError - ('Confirm that opencv is installed on your environment! Expected path ', '/tmp/_MEIIkBAm6/cv2/data/haarcascade_frontalface_default.xml', ' violated.') - file_name='deepface/detectors/OpenCv.py' func_name='__build_cascade' line_num=145

Expected behavior Would like hooks to handle this instead of needing a --add-data argument

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: linux-arm64, linux-amd64, darwin-arm64
  • Python Version: python 3.11
  • Version of pyinstaller-hooks-contrib: 2024.0
  • Version of PyInstaller: 6.3.0

Additional context I can provide a repository with a full working example if required.

michaellee1019 avatar Jan 29 '24 16:01 michaellee1019

Strictly speaking, it is deepface that is trying to load the cascade file, not OpenCV itself: https://github.com/serengil/deepface/blob/35025cdf6e4e2fc0642b890dc7dd589b7cb37205/deepface/detectors/OpenCv.py#L128-L156

And since the .xml files in cv2/data total 9MB, I don't think it is good idea to have them collected by default. If anything, this should be a job for deepface.detector.OpenCv hook (and even that should collect only the two files that it actually uses).

rokm avatar Jan 29 '24 16:01 rokm