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

时间:2018-06-20 12:16:58

标签: 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环境也具有ffmpegenter image description here

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

  

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

     

Python 3.5.2(默认值,2017年11月23日,16:37:01)

     

Linux上的[GCC 5.4.0 20160609]

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

2 个答案:

答案 0 :(得分:0)

您正在打印对函数本身的引用,而不是调用它:

print(cap.read)

代替:

print(cap.read())

我想这会奏效,但如果不能(由于某种原因),您可以尝试下一帧以检查是否可以阅读此帧。

答案 1 :(得分:0)

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版本。但是你明白了。