Casey
Casey

Reputation: 495

Python opencv cv2 cap.read() returns None in PyInstaller on Windows

I have an ethernet camera and the following test code (BasicVideo.py) that works perfectly and does not produce any errors. By "works perfectly" I mean an image window appears and the camera images is displayed:

import cv2

def IpVideo35(mirror=False):    
    cameraAddress = 'http://user:[email protected]:80/video.cgi?.mjpg'
    windowName = "AD Camera - ESC to exit"
    cv2.namedWindow(windowName)    
    cap = cv2.VideoCapture(cameraAddress)
    videoLoopDelayInMs = 50     # 50ms delay for each video loop
    keyboardExitKey = 27        # 27 is the escape key on the keyboard

    while True:
        _, img = cap.read()
        if img is None:
            print('img returned None.')
        else:    
            if mirror: 
                img = cv2.flip(img, 1)
            cv2.imshow(windowName, img)
        if cv2.waitKey(videoLoopDelayInMs) & 0xFF == keyboardExitKey:     # 50ms delay, esc to quit
            break
        if cv2.getWindowProperty(windowName, 0) < 0:
            break  
    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    IpVideo35()

The camera's IP is 192.168.1.8, port is 80, login is 'user' and the password is 'pwd'. When run, this shows the images from the camera on a cv2 windows. All is well.

When I build this into an exe using PyInstaller using the following command:

pyinstaller --distpath F:\dist --workpath F:\build --onefile --noconfirm BasicVideo.py

and then run the resulting executable, the image window appears, but the images is all grey and I get the message "img returned None." repeated many, many times on the command line. Why does _, img = cap.read() return None to img in the PyInstaller version when everything runs so well in Python?

Some details on my setup:

In the PyInstaller build I get a ton of warnings (I don't know how to clear these), but other test functionality of the cv2 window, such as drawing lines on the window, changing the image size or hiding the window, show me that cv2 seems to be running fine, except for cap.read(). The errors:

missing module named 'win32com.gen_py' - imported by win32com, c:\ad-install\virtualenvs\35itcp_env\lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_win32comgenpy.py
missing module named pyimod03_importers - imported by PyInstaller.loader.pyimod02_archive, c:\ad-install\virtualenvs\35itcp_env\lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py
missing module named _dbm - imported by dbm.ndbm
missing module named gdbm - imported by anydbm, future.moves.dbm.gnu
missing module named _gdbm - imported by dbm.gnu
missing module named dumbdbm - imported by anydbm, future.moves.dbm.dumb
missing module named anydbm - imported by future.moves.dbm
missing module named dbhash - imported by anydbm
missing module named whichdb - imported by future.moves.dbm, anydbm
missing module named multiprocessing.SimpleQueue - imported by multiprocessing, concurrent.futures.process
missing module named multiprocessing.set_start_method - imported by multiprocessing, multiprocessing.spawn
missing module named multiprocessing.get_start_method - imported by multiprocessing, multiprocessing.spawn
missing module named multiprocessing.TimeoutError - imported by multiprocessing, multiprocessing.pool
missing module named multiprocessing.get_context - imported by multiprocessing, multiprocessing.pool, multiprocessing.managers, multiprocessing.sharedctypes
missing module named multiprocessing.BufferTooShort - imported by multiprocessing, multiprocessing.connection
missing module named multiprocessing.AuthenticationError - imported by multiprocessing, multiprocessing.connection
missing module named _scproxy - imported by urllib.request, future.backports.urllib.request
missing module named dummy_thread - imported by future.backports.misc
missing module named thread - imported by future.backports.misc, PyInstaller.loader.pyimod02_archive
missing module named __builtin__ - imported by numpy.core.numerictypes, numpy.core.numeric, scipy._lib.six, pyparsing, future.builtins.misc, future.utils, past.types, past.builtins.noniterators, past.builtins, past.builtins.misc, numpy.distutils.misc_util, numpy.lib.function_base, numpy.lib._iotools, numpy.ma.core, numpy
missing module named future_builtins - imported by future.builtins.misc, numpy.lib.npyio
missing module named UserDict - imported by PyInstaller.compat
missing module named 'PyInstaller.lib.macholib.compat' - imported by PyInstaller.lib.macholib.MachO
missing module named _pkgutil - imported by PyInstaller.lib.modulegraph.modulegraph
missing module named urllib.pathname2url - imported by urllib, PyInstaller.lib.modulegraph.modulegraph
missing module named StringIO - imported by numpy.lib.utils, numpy.testing.utils, scipy._lib.six, six, PyInstaller.lib.modulegraph.util, PyInstaller.lib.modulegraph.zipio, PyInstaller.lib.modulegraph.modulegraph, numpy.lib.format
missing module named pyimod00_crypto_key - imported by PyInstaller.loader.pyimod02_archive
missing module named Crypto - imported by PyInstaller.building.makespec
missing module named _sysconfigdata - imported by sysconfig
missing module named 'com.sun' - imported by appdirs
missing module named com - imported by appdirs
missing module named ordereddict - imported by pyparsing
missing module named six.moves.filter - imported by six.moves, setuptools, pkg_resources, setuptools.ssl_support, setuptools.command.py36compat
missing module named 'six.moves.urllib' - imported by 'six.moves.urllib'
missing module named six.moves.map - imported by six.moves, setuptools, pkg_resources, setuptools.extension, setuptools.dist, setuptools.command.easy_install, setuptools.sandbox, setuptools.package_index, setuptools.ssl_support, setuptools.command.egg_info, setuptools.namespaces
runtime module named six.moves - imported by setuptools, pkg_resources, 'six.moves.urllib', setuptools.extension, setuptools.dist, setuptools.command.easy_install, setuptools.sandbox, setuptools.command.setopt, setuptools.package_index, setuptools.ssl_support, setuptools.command.egg_info, setuptools.command.py36compat, setuptools.namespaces
missing module named resource - imported by posix, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, test.support
missing module named posix - imported by os, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
missing module named _posixsubprocess - imported by subprocess, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, multiprocessing.util
missing module named 'org.python' - imported by pickle, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, xml.sax, setuptools.sandbox
missing module named readline - imported by cmd, code, pdb, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
excluded module named _frozen_importlib - imported by importlib, importlib.abc, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, PyInstaller.loader.pyimod02_archive
missing module named _frozen_importlib_external - imported by importlib._bootstrap, importlib, importlib.abc, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
missing module named _winreg - imported by platform, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, appdirs, numpy.distutils.cpuinfo
missing module named java - imported by platform, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
missing module named 'java.lang' - imported by platform, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, xml.sax._exceptions
missing module named vms_lib - imported by platform, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
missing module named termios - imported by tty, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, getpass
missing module named urllib.splittag - imported by urllib, setuptools.py26compat
missing module named grp - imported by shutil, tarfile, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, pathlib, distutils.archive_util
missing module named org - imported by copy, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
missing module named pwd - imported by posixpath, shutil, tarfile, http.server, webbrowser, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py, pathlib, distutils.util, distutils.archive_util, netrc, getpass
missing module named ce - imported by os, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
missing module named _dummy_threading - imported by dummy_threading, f:\AD-GitRepositories\python\sandbox\opencv\OpenCvSandbox\BasicVideoPackage\BasicVideo.py
missing module named commands - imported by numpy.distutils.cpuinfo
missing module named ConfigParser - imported by numpy.distutils.system_info, numpy.distutils.npy_pkg_config
missing module named wincertstore - imported by setuptools.ssl_support
missing module named 'backports.ssl_match_hostname' - imported by setuptools.ssl_support
missing module named backports - imported by setuptools.ssl_support
missing module named urllib2 - imported by setuptools.package_index, numpy.lib._datasource
missing module named 'numpy_distutils.cpuinfo' - imported by numpy.f2py.diagnose
missing module named 'numpy_distutils.fcompiler' - imported by numpy.f2py.diagnose
missing module named 'numpy_distutils.command' - imported by numpy.f2py.diagnose
missing module named numpy_distutils - imported by numpy.f2py.diagnose
missing module named __svn_version__ - imported by numpy.f2py.__version__
missing module named numarray - imported by numpy.distutils.system_info
missing module named Numeric - imported by numpy.distutils.system_info
missing module named sets - imported by numpy.distutils.misc_util, numpy.distutils.fcompiler, numpy.distutils.command.build_ext
missing module named _curses - imported by curses, curses.has_key
missing module named 'nose.plugins' - imported by numpy.testing.noseclasses, numpy.testing.nosetester
missing module named cffi - imported by scipy._lib._ccallback
missing module named cPickle - imported by numpy.core.numeric, numpy.lib.format, numpy.lib.npyio, numpy.ma.core
missing module named cStringIO - imported by cPickle
missing module named copy_reg - imported by cPickle, cStringIO, numpy.core
missing module named numpy.random.randn - imported by numpy.random, scipy
missing module named numpy.random.rand - imported by numpy.random, scipy
missing module named 'nose.util' - imported by numpy.testing.noseclasses
missing module named nose - imported by numpy.testing.utils, numpy.testing.decorators, numpy.testing.noseclasses
missing module named numpy.core.number - imported by numpy.core, numpy.testing.utils
missing module named numpy.lib.imag - imported by numpy.lib, numpy.testing.utils
missing module named numpy.lib.real - imported by numpy.lib, numpy.testing.utils
missing module named numpy.lib.iscomplexobj - imported by numpy.lib, numpy.testing.utils
missing module named numpy.core.signbit - imported by numpy.core, numpy.testing.utils
missing module named numpy.core.float64 - imported by numpy.core, numpy.testing.utils
missing module named numpy.core.isinf - imported by numpy.core, numpy.testing.utils
missing module named numpy.core.isfinite - imported by numpy.core, numpy.testing.utils, numpy.linalg.linalg
missing module named numpy.core.isnan - imported by numpy.core, numpy.testing.utils
missing module named numpy.core.float32 - imported by numpy.core, numpy.testing.utils
missing module named numpy.lib.i0 - imported by numpy.lib, numpy.dual
missing module named numpy.linalg.inv - imported by numpy.linalg, numpy.matrixlib.defmatrix, numpy.lib.polynomial
missing module named numpy.core.integer - imported by numpy.core, numpy.fft.helper
missing module named numpy.core.sqrt - imported by numpy.core, numpy.fft.fftpack, numpy.linalg.linalg
missing module named numpy.core.conjugate - imported by numpy.core, numpy.fft.fftpack
missing module named numpy.lib.triu - imported by numpy.lib, numpy.linalg.linalg
missing module named numpy.core.intp - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.multiply - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.inexact - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.double - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.add - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.single - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.longdouble - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.maximum - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.complexfloating - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.object_ - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.cdouble - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.csingle - imported by numpy.core, numpy.linalg.linalg
missing module named numpy.core.geterrobj - imported by numpy.core, numpy.linalg.linalg
missing module named urlparse - imported by numpy.lib._datasource
missing module named numpy.recarray - imported by numpy, numpy.ma.mrecords
missing module named numpy.dtype - imported by numpy, numpy.ma.mrecords, numpy.ctypeslib
missing module named numpy.expand_dims - imported by numpy, numpy.ma.core
missing module named numpy.array - imported by numpy, numpy.ma.core, numpy.ma.extras, numpy.ma.mrecords, numpy.ctypeslib
missing module named numpy.ndarray - imported by numpy, numpy.ma.core, numpy.ma.extras, numpy.ma.mrecords, numpy.ctypeslib
missing module named numpy.bool_ - imported by numpy, numpy.ma.core, numpy.ma.mrecords
missing module named numpy.amin - imported by numpy, numpy.ma.core
missing module named numpy.iscomplexobj - imported by numpy, numpy.ma.core
missing module named numpy.amax - imported by numpy, numpy.ma.core
missing module named numpy.histogramdd - imported by numpy, numpy.lib.twodim_base
missing module named numpy.eye - imported by numpy, numpy.core.numeric

I don't think these warnings are part of the problem (but guidance on getting rid of them would be appreciated). Why does _, img = cap.read() return None to img in the PyInstaller version when everything runs so well in Python?

I'm open to other executable builders, but py2exe doesn't have a windows precompiled wheel for Python 3.5 and I couldn't get cx_Freeze to work. Hopefully PyInstaller can be made to work because it seems to work well for many other packages: Scipy, Numpy, Socket, ...

Upvotes: 2

Views: 2705

Answers (1)

Garth5689
Garth5689

Reputation: 622

I just had this same problem and this worked for me:

Use the --add-binary to copy the ffmpeg dll from the site-packages folder to your dist folder when building the exe.

example:
pyinstaller program.spec --add-binary <PATH_TO_PYTHON>\Lib\site-packages\cv2\opencv_ffmpeg320_64.dll;.

Upvotes: 3

Related Questions