资讯详情

python 使用cv2读取mp4视频失败的解决方法

python 使用cv2读取mp44视频失败的解决方案

  • cv读取视频失败描述
    • 相关环境配置
    • 代码部分
    • terminal输出
    • 读取失败分析
  • 失败解决方案
  • 最终解决方案
    • 修改opencv版本
    • 安装相关配置
    • 结果展示
如果仍然使用全网方法cv如果你不能读视频,下面将是你在解决问题之前读过的最后一篇文章!

创作背景:博主今天很生气,也很沮丧,花了一个下午,什么都没做,用cv2读取mp4视频,一直Opened()输出为Flase,一直显示打印出来的视频fps为0!!!为什么会这样???几乎所有的解决方案都在尝试全网cv读完视频失败的方法后,我分析了我配置下读取失败的原因,并给出了这本书csdn下面是最终的解决方案。

cv读取视频失败描述

博主使用的环境是服务器下的环境linux-terminal若使用python opencv-python本文可能对本地电脑没有太大帮助)

相关环境配置

python版本:3.6.9 opencv-python版本: 3.2.0.8(3.3.0.10版以下均可)

代码部分

video_path = "path/to/my/video" img_path = "path/to/my/save" cap = cv2.VideoCapture() cap.open(video_path) fps = cap.get(cv2.CAP_PROP_FPS) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) print(fps, width, height) suc = cap.isOpened() print(suc) frame_count = 0 while suc:     frame_count  = 1     suc, frame = cap.read()     cv2.imwrite(img_path   str(frame_count), frame)     cv2.waitKey(1) cap.release() print('End!') 

terminal输出

0.0 0 0 False End! 

读取失败分析

容易发现,terminal没有输出代码报错位置,说明代码没问题。 此外,代码suc变量print出的结果是False,说明cv没有根据我们给出的视频地址读取相应的视频。 综上所述,我们有以下分析: 1.视频路径错误; 2、opencv看视频问题。 对于1,我们试图更改其他视频,发现我们无法阅读视频;试着更改视频格式(由mp4换成avi),我们有理由相信原来的视频路径没有问题,因为我们发现我们仍然无法阅读视频。opencv的问题了。 对2,根据网上print出当下opencv信息方法:

import cv2 print(cv2.getBuildInformation()) 

我们可以得到以下opencv信息:

  General configuration for OpenCV 3.3.0 =====================================   Version control:               3.3.0    Platform:     Timestamp:                   2017-08-12T18:35:30Z     Host:                        Linux 4.4.0-83-generic x86_64     CMake:                       3.9.0     CMake generator:             Unix Makefiles     CMake build tool:            /usr/bin/gmake     Configuration:               Release    CPU/HW features:     Baseline:                    SSE SSE2 SSE3       requested:                 SSE3     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2       SSE4_1 (2 files):            SSSE3 SSE4_1       SSE4_2 (1 files):            SSSE3 SSE4_1 POPCNT SSE4_2       FP16 (1 files):              SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX       AVX (5 files):               SSSE3 SSE4_1 POPCNT SSE4_2 AVX       AVX2 (7 files):              SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2    C/C  :     Built as dynamic libs?:      NO     C   Compiler:                /opt/rh/devtoolset-2/root/usr/bin/c    (ver 4.8.2)     C   flags (Release):         -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG     C   flags (Debug):           -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG     C Compiler:                  /opt/rh/devtoolset-2/root/usr/bin/cc     C flags (Release):           -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG     C flags Debug):             -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):
    Linker flags (Debug):
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          gthread-2.0 glib-2.0 /lib64/libz.so stdc++ dl m pthread rt
    3rdparty dependencies:       ittnotify libprotobuf libjpeg libwebp libpng libtiff libjasper IlmImf

  OpenCV modules:
    To be built:                 core flann imgproc ml objdetect photo video dnn imgcodecs shape videoio highgui superres features2d calib3d stitching videostab python3
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 ts viz

  GUI:
    QT:                          NO
    GTK+:                        NO
    GThread :                    YES (ver 2.12.3)
    GtkGlExt:                    NO
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        /lib64/libz.so (ver 1.2.3)
    JPEG:                        libjpeg (ver 90)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.24)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      NO
      avcodec:                   NO
      avformat:                  NO
      avutil:                    NO
      swscale:                   NO
      avresample:                NO
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    Aravis SDK:                  NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    NO/NO
    XIMEA:                       NO
    Xine:                        NO
    Intel Media SDK:             NO
    gPhoto2:                     NO

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Use Intel IPP:               NO
    Use Intel IPP IW:            NO
    Use Intel IPP Async:         NO
    Use VA:                      NO
    Use Intel VA-API/OpenCL:     NO
    Use Lapack:                  NO
    Use Eigen:                   NO
    Use Cuda:                    NO
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                /io/opencv/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 NO

  Python 3:
    Interpreter:                 python (ver 3.6.0)
    Libraries:
    numpy:                       /opt/python/cp36-cp36m/lib/python3.6/site-packages/numpy/core/include (ver 1.11.3)
    packages path:               /opt/python/cp36-cp36m/lib/python3.6/site-packages

  Python (for build):            python

  Java:
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

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

  Install path:                  /usr/local

  cvconfig.h is in:              /io/opencv/build
-----------------------------------------------------------------

容易发现,video下方基本都是,最终我们确定是opencv的问题。

失败解决方案

1、网上说没有安装ffmpeg库,我们尝试安装此库,发现并不能解决问题。 2、没有安装opencv-contrib-python-headless,我们尝试安装此库,发现并不能解决问题。 3、opencv库里面dll文件异常,我们尝试修改dll文件,发现并不能解决问题。

最终解决方案

修改opencv版本

我们尝试提高opencv版本,将原来的3.2.0.8版本更新到版本及以上,令我们出乎意料的是这个方法将上述遇到的问题解决了,下面将分享我们解决问题的过程和方法。

安装相关配置

我们尝试根据如下指令提高opencv的版本:

pip install opencv-python==4.1.1.26

之后运行代码,发现出现如下报错:

ImportError: libSM.so.6: cannot open shared object file: No such file or directory

依次运行如下命令,即可解决此报错:

apt-get update (选择)
apt-get install sudo (选择)
sudo apt-get install libsm6
sudo apt-get install libxrender1
sudo apt-get install libxext-dev

结果展示

再次查看opencv信息:

 General configuration for OpenCV 4.1.1 =====================================
  Version control:               4.1.1

  Platform:
    Timestamp:                   2019-09-02T14:45:10Z
    Host:                        Linux 4.15.0-1028-gcp x86_64
    CMake:                       3.9.0
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (13 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (27 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      NO
    C++ Compiler:                /usr/lib/ccache/compilers/c++  (ver 4.8.2)
    C++ flags (Release):         -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/lib/ccache/compilers/cc
    C flags (Release):           -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -L/root/ffmpeg_build/lib  -Wl,--gc-sections
    Linker flags (Debug):        -L/root/ffmpeg_build/lib  -Wl,--gc-sections
    ccache:                      YES
    Precompiled headers:         NO
    Extra dependencies:          ade /opt/Qt4.8.7/lib/libQtGui.so /opt/Qt4.8.7/lib/libQtTest.so /opt/Qt4.8.7/lib/libQtCore.so /lib64/libz.so dl m pthread rt
    3rdparty dependencies:       ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quirc

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java js python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:
    QT:                          YES (ver 4.8.7 EDITION = OpenSource)
      QT OpenGL support:         NO
    GTK+:                        NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        /lib64/libz.so (ver 1.2.3)
    JPEG:                        libjpeg-turbo (ver 2.0.2-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (58.55.100)
      avformat:                  YES (58.31.101)
      avutil:                    YES (56.33.100)
      swscale:                   YES (5.6.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                /io/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 /opt/python/cp36-cp36m/bin/python (ver 3.6.9)
    Libraries:                   libpython3.6m.a (ver 3.6.9)
    numpy:                       /opt/python/cp36-cp36m/lib/python3.6/site-packages/numpy/core/include (ver 1.11.3)
    install path:                python

  Python (for build):            /opt/python/cp36-cp36m/bin/python

  Java:
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /io/_skbuild/linux-x86_64-3.6/cmake-install
-----------------------------------------------------------------

显然,video下方已经由NO变成

再次运行代码,terminal输出结果:

59.94005994005994 1920 1080
True
End!

fps、视频长宽均正常输出,且视频正常打开,问题解决!

希望此分享能帮助遇到同样问题的您!

标签: itt连接器kpse08f24

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台