Reputation: 1009
I have written a python script called videoSave.py
to read camera and save video. The code is as below:
import cv2
def saveCam():
video = cv2.VideoCapture(0)
ret, img = video.read()
h, w = img.shape[:2]
videoSaver = cv2.VideoWriter("videoSave_test.avi", cv2.VideoWriter_fourcc(*'DIVX'), 10, (w, h))
key = 0
while key != 27:
ret, img = video.read()
if not ret:
break
cv2.imshow('img', img)
key = cv2.waitKey(1) & 0xFF
videoSaver.write(img)
videoSaver.release()
if __name__ == '__main__':
saveCam()
This script works well when runed directly. It can show the image obtained by the camera and save the content to a video file.
Then I build this script into exe file using command pyinstaller -F videoSave.py
. I can get the exe file. And when excuting it, I can see the image obtained by the camera, but the video file saved by it has zero size!
My environment is:
Python 3.7.4
Windows 7
Pyinstaller 3.5
opencv 4.1.2
Upvotes: 1
Views: 1105
Reputation: 303
You have to pack the opencv_videoio_ffmpeg420_64.dll in the .exe
To do so, use --add-binary
option this way (I think you can use --add-data
option too):
pyinstaller --onefile videoSave.py --add-binary venv\Lib\site-packages\cv2\opencv_videoio_ffmpeg420_64.dll;.
Of course you have to specify your own path to the .dll (The .dll could sometimes have other names).
Note that, at the end, it is ";." on Windows and ":." on most unix systems, as specified here (Section: "What to bundle, where to search")
According to this issue it seems that it is because Pyinstaller didn't pack opencv_ffmpeg340.dll in the .exe, which is neccesary to save videofiles.
Looks like you can manually copy and paste the .dll in the same folder where the .exe lies and the program will read it, that would explain the solution offered by @ToughMind
Upvotes: 1
Reputation: 1009
I have solved this problem by copying opencv_video_ffmpeg412_64.dll from Anaconda3\envs\your_env_name to the dir where exe file lies. But I donot know why.
Upvotes: 1