python - openCV函数 '.read()'在远程环境中返回(False,None)

标签 python linux opencv ffmpeg

我正在 远程 Linux 环境中的 docker 中运行视频处理程序。我在使用以下代码时遇到了一些问题:

import os
import cv2
os.chdir("/home/test/wyq/object_detection/")
print(os.path.exists("test.mp4"))  # see if the file exists
print(cv2.__version__) #check cv's version
cap = cv2.VideoCapture("test.mp4") 
print(cap.read())

结果是:

True
3.3.0
(False, None)

这意味着 mp4 文件肯定存在并且 openCV 的版本没问题。但它就是无法读取它。 Linux环境也有ffmpeg: enter image description here

以下是有关操作系统和 python 的一些信息:

Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-127-generic x86_64)

Python 3.5.2 (default, Nov 23 2017, 16:37:01)

[GCC 5.4.0 20160609] on linux

我想问题可能出在 ffmpeg 上?也许 opencv 无法使用现有的 ffmpeg?但我不知道如何测试和解决它。有人可以给我一些建议吗?感谢您的关注。

最佳答案

OpenCV 支持 several video backends ,但这取决于它是如何编译的,哪些是实际支持的。它不使用命令行 ffmpeg,而是直接与 libav* 库链接(如果在构建时配置为这样做的话)。在您的远程服务器上试试这个:

$ python -c "import cv2; print(cv2.getBuildInformation())"

General configuration for OpenCV 2.4.8 =====================================
  Version control:               unknown

  Platform:
    Host:                        Linux 3.2.0-58-generic x86_64
    CMake:                       2.8.12.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /usr/bin/x86_64-linux-gnu-g++  (ver 4.8.2)
    C++ flags (Release):         -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2    -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security  -DNDEBUG
    C++ flags (Debug):           -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2    -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/x86_64-linux-gnu-gcc
    C flags (Release):           -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2    -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security  -DNDEBUG
    C flags (Debug):             -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2    -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
    Linker flags (Debug):        -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
    Precompiled headers:         YES

  OpenCV modules:
    To be built:                 core flann imgproc highgui features2d calib3d ml objdetect video contrib legacy photo gpu java ocl python stitching superres ts videostab
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 androidcamera dynamicuda

  GUI:
    QT:                          NO
    GTK+ 2.x:                    YES (ver 2.24.22)
    GThread :                    YES (ver 2.39.90)
    GtkGlExt:                    YES (ver 1.2.0)
    OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libSM.so /usr/lib/x86_64-linux-gnu/libICE.so /usr/lib/x86_64-linux-gnu/libX11.so /usr/lib/x86_64-linux-gnu/libXext.so)

  Media I/O:
    ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
    JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
    PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.50)
    TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.3)
    JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
    OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 1.6.1)

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  YES (ver 2.2.1)
    FFMPEG:                      YES
      codec:                     YES (ver 54.35.0)
      format:                    YES (ver 54.20.3)
      util:                      YES (ver 52.3.0)
      swscale:                   YES (ver 2.1.1)
      gentoo-style:              YES
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    Using libv4l (ver 1.0.1)
    XIMEA:                       NO
    Xine:                        NO

  Other third-party libraries:
    Use IPP:                     NO
    Use Eigen:                   YES (ver 3.2.0)
    Use TBB:                     YES (ver 4.2 interface 7000)
    Use OpenMP:                  NO
    Use GCD                      NO
    Use Concurrency              NO
    Use C=:                      NO
    Use Cuda:                    NO
    Use OpenCL:                  YES

  OpenCL:
    Version:                     dynamic
    Use AMD FFT:                 NO
    Use AMD BLAS:                NO

  Python:
    Interpreter:                 /usr/bin/python2 (ver 2.7.6)
    Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.0.dev-95f7a46)
    packages path:               lib/python2.7/dist-packages

  Java:
    ant:                         /usr/bin/ant (ver 1.9.3)
    JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include
    Java tests:                  NO

  Documentation:
    Build Documentation:         YES
    Sphinx:                      /usr/bin/sphinx-build (ver 1.2.1)
    PdfLaTeX compiler:           /usr/bin/pdflatex

  Tests and samples:
    Tests:                       NO
    Performance tests:           YES
    C/C++ Examples:              YES

  Install path:                  /usr

  cvconfig.h is in:              /build/buildd/opencv-2.4.8+dfsg1/obj-x86_64-linux-gnu
-----------------------------------------------------------------

当然你的输出会有所不同,你必须检查正确的 python 版本。但你明白了。

关于python - openCV函数 '.read()'在远程环境中返回(False,None),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50948054/

相关文章:

linux - 如何将用户名附加到文件名中

java - 用于 Java 的 OpenCV : HoughCircles finding all the wrong circles

python - 加速 "closest"字符串匹配算法

python - 创建多个 numpy 数组

python - 如何删除列表中元素的最后 2 个字符?

python - 使用 Python setuptools.command.sdist 导入错误

python - 如何在 python 中检查命令行参数是否为文件?

linux - 在 Internet 存档上重定向后获取 URL

python - OpenCV 的 `getTextSize` 和 `putText` 返回错误的大小和像素较低的字母

opencv - 如何打印轮廓opencv内的所有坐标