pythonopencvvideo-streamingopencv3.0

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:psw@192.168.1.1/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?


Solution

  • 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