luke88
luke88

Reputation: 1008

Python OpenCV stream video error in executable

In my python application I'm using OpenCV, among other things, to stream a Video from an IP camera:

cap = cv2.VideoCapture("http://usr:[email protected]/video.cgi")

and everithing works fine.

But i needed to obtain an executable, and so I used PyInstaller. In the resulting .exe the stream doesn't work anymore.

Instead if I change the capture with this:

# works with camera_num = 0 (pc's webcam) and = 1 (external USB webcam)
cap = cv2.VideoCapture(camera_num)

capturing from webcam of my pc, or with and external USB webcam, everything works.

Any suggestions?

Upvotes: 0

Views: 1685

Answers (2)

Umut Kaan Başer
Umut Kaan Başer

Reputation: 1

same problem is faced to me in c++. I installed gstream,ffmpeg and opencv-python firstly after rebuild opencv and i solved this problem. Maybe you can use same method for python.

Upvotes: 0

luke88
luke88

Reputation: 1008

Thanks to @GPPK and @Dan Mašek comments I could solve the problem.

The problem was, like @Dan Mašek said, that "FFMPEG is not a hard dependency".

So a solution is to search the OpenCV's FFMPEG dll. Launch python from the cosnole:

# import OpenCV module
import cv2
# retrieve the pathname of the file from which the module was loaded
cv2.__file__

the output should be something like:

'C:\\Users\\luke\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\cv2\\cv2.cp36-win_amd64.pyd'

go to the cv2 folder, search for opencv_ffmpeg340_64.dll and copy it to the python application's folder.

Now we have to tell PyInstaller to add this dll to the .exe:

pyinstaller -F --add-data "opencv_ffmpeg340_64.dll;." test.py

Upvotes: 1

Related Questions