0

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?

Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
luke88
  • 958
  • 19
  • 40
  • WHat is the value of `camera_num`? – E.T. Apr 17 '18 at 15:23
  • @Stiffy2000 almost guaratneed to be 0 – GPPK Apr 17 '18 at 15:28
  • 2
    Are you including FFMPEG DLL or something through your IDE that you need when you run the exectuable? – GPPK Apr 17 '18 at 15:28
  • @Stiffy2000 works with 0 (the integrated webcam) and even with 1 if I attach an external USB camera – luke88 Apr 17 '18 at 15:36
  • @GPPK no, i'm not. I think that PyInstaller does that automatically. I think that without the FFMPEG dll it shouldn't works even with the webcam... – luke88 Apr 17 '18 at 15:39
  • 1
    @luke88 Not necessarily, that's just one of [many different backends](https://docs.opencv.org/3.3.0/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d). – Dan Mašek Apr 17 '18 at 16:53
  • 1
    Also, FFMPEG is not a hard dependency, it's treated more as a plugin and loaded only if it exists when it's needed. As such, automated analysis of the DLL dependencies won't pick it up (you can try it out with the Dependency Walker). You will need to explicitly specify it for inclusion. – Dan Mašek Apr 17 '18 at 17:02

2 Answers2

1

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
luke88
  • 958
  • 19
  • 40
0

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.