我有一个以太网摄像头和以下测试代码(BasicVideo.py),它完美运行并且不会产生任何错误。通过"完美地运作"我的意思是出现一个图像窗口,并显示相机图像:
import cv2
def IpVideo35(mirror=False):
cameraAddress = 'http://user:pwd@192.168.1.7: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()
相机的IP为192.168.1.8,端口为80,登录用户为&#39;密码是&#39; pwd&#39;。运行时,会在cv2窗口中显示摄像机的图像。一切都很好。
当我使用PyInstaller使用以下命令将其构建到exe中时:
pyinstaller --distpath F:\dist --workpath F:\build --onefile --noconfirm BasicVideo.py
然后运行生成的可执行文件,图像窗口出现,但图像全部为灰色,我收到消息&#34; img返回无。&#34;在命令行上重复了很多次。当Python中的所有内容运行良好时,为什么_,img = cap.read()在PyInstaller版本中返回None?
我的设置的一些细节:
在PyInstaller构建中,我得到了大量的警告(我不知道如何清除这些),但是cv2窗口的其他测试功能,例如在窗口上绘制线条,更改图像大小或隐藏窗口,告诉我cv2似乎运行正常,除了cap.read()。错误:
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
我不认为这些警告是问题的一部分(但是要理解摆脱它们的指导意见)。当Python中的所有内容运行良好时,为什么_,img = cap.read()在PyInstaller版本中返回None?
我对其他可执行构建器开放,但是py2exe doesn't have a windows precompiled wheel for Python 3.5和I couldn't get cx_Freeze to work。希望PyInstaller可以工作,因为它似乎适用于许多其他软件包:Scipy,Numpy,Socket,......
答案 0 :(得分:2)
我遇到了同样的问题,这对我有用:
在构建exe时,使用--add-binary
将ffmpeg
dll从site-packages
文件夹复制到dist
文件夹。
例如:
pyinstaller program.spec --add-binary <PATH_TO_PYTHON>\Lib\site-packages\cv2\opencv_ffmpeg320_64.dll;.