OpenCV videocapture读取摄像头rtsp错误

OpenCV videocapture读取摄像头rtsp错误,opencv,opencv-python,Opencv,Opencv Python,我正在使用opencv python(版本4.4.0.46)python版本3.8.5来捕获我的IP摄像头流,但是,我无法读取视频帧。这是我的密码: 导入cv2 #cap=cv2.视频捕获(“rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov") cap=cv2.视频捕获(“rtsp://admin:xxx@xxx/流媒体/频道/1”) 打印(cap.isOpened()) ret,frame=cap.read() 打

我正在使用opencv python(版本4.4.0.46)python版本3.8.5来捕获我的IP摄像头流,但是,我无法读取视频帧。这是我的密码:

导入cv2
#cap=cv2.视频捕获(“rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov")
cap=cv2.视频捕获(“rtsp://admin:xxx@xxx/流媒体/频道/1”)
打印(cap.isOpened())
ret,frame=cap.read()
打印(ret)
而ret:
ret,frame=cap.read()
打印(ret)
如果cv2.waitKey(1)&0xFF==ord('q'):
打破
第1章释放()
在我的代码中,当我使用IPC流时,我得到“print(cap.isOpened())”为True,而“cap.read()”返回False。然而,当它变为“Bigbuckbuny_115k”流时,一切正常,我可以得到帧图片

有人可能会说你的IPC应该被损坏了。但我尝试过使用ffmpeg来捕获视频,这样可以很容易地获得帧图像。我的代码:

ffmpeg -i "rtsp://admin:xxx@xxx/Streaming/Channels/1" -y -f image2 -r 1/1 img%03d.jpg
使用python来捕获视频,我得到了如上所述的“False”,但没有打印ant错误日志,这使我很难解决问题。因此,我改为使用java,并使用opencv-4.4.0。作为库,我的代码:

VideoCapture VideoCapture=新的VideoCapture();
//将异常模式设置为true,以便在出现错误时可以打印堆栈跟踪
videoCapture.setExceptionMode(true);
布尔值openStatus=videoCapture.open(“rtsp://admin:xxx@xxx/流媒体/频道/1”);
log.info(“打开rtsp状态:{}”,打开状态);
int i=0;
状态=真;
while(openStatus&&status){
Mat Mat=新Mat();
视频捕获读取(mat);
Imgcodecs.imwrite(“../”+i+“.png”,mat);
i++;
}
通过运行这段代码,我得到了异常:org.opencv.core.CvException:cv::exception:opencv(4.4.0)/xxx/opencv-4.4.0/modules/videoio/src/cap.cpp:177:错误:(-2:未指定的错误)无法打开'rtsp://admin:xxx@功能“打开”中的xxx/Streaming/Channels/1。 上面的异常日志无助于解决问题,“-2:未指定错误”是什么意思

忘了提到我的运行时是CentOS 7.5(内核版本3.10.0-862.el7.x86_64)。然后我换了另一台服务器,使用相同的opencv python版本和CentOS版本。奇怪的是,在这个新服务器上,使用相同的opencv代码,我现在可以得到视频帧了

第一个问题是,有没有相关资料可供我参考? 第二个问题是,如何解决此问题。 第三个问题是,为什么它在不同的服务器上表现不同,在我的第一个服务器上是否有任何系统配置错误,比如网络配置



更新日期:2021.01.07

感谢antoine为我提供有关OpenCV后端的信息。我尝试使用下面的代码打印python opencv构建信息

import cv2
print(cv2.getBuildInformation())
在我拥有的两台服务器上(其中一台可以读取视频流,另一台不能),我得到了完全相同的控制台打印。这是印刷品:

General configuration for OpenCV 4.4.0 =====================================
  Version control:               4.4.0-dirty

  Platform:
    Timestamp:                   2020-11-03T00:52:03Z
    Host:                        Linux 4.15.0-1077-gcp x86_64
    CMake:                       3.18.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /bin/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (15 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 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (4 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                /usr/lib/ccache/compilers/c++  (ver 9.3.1)
    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 -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -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 -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -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 -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wno-strict-overflow -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 -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wno-strict-overflow -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):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/root/ffmpeg_build/lib  -Wl,--gc-sections -Wl,--as-needed  
    Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/root/ffmpeg_build/lib  -Wl,--gc-sections -Wl,--as-needed  
    ccache:                      YES
    Precompiled headers:         NO
    Extra dependencies:          ade Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent /lib64/libpng.so /lib64/libz.so dl m pthread rt
    3rdparty dependencies:       ittnotify libprotobuf libjpeg-turbo libwebp libtiff libjasper IlmImf quirc ippiw ippicv

  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 5.15.0)
      QT OpenGL support:         NO
    GTK+:                        NO
    VTK support:                 NO

  Media I/O: 
    ZLib:                        /lib64/libz.so (ver 1.2.7)
    JPEG:                        libjpeg-turbo (ver 2.0.5-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         /lib64/libpng.so (ver 1.5.13)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build Jasper (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.109.100)
      avformat:                  YES (58.61.100)
      avutil:                    YES (56.60.100)
      swscale:                   YES (5.8.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   /tmp/pip-req-build-99ib2vsi/_skbuild/linux-x86_64-3.8/cmake-build/3rdparty/ippicv/ippicv_lnx/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                /tmp/pip-req-build-99ib2vsi/_skbuild/linux-x86_64-3.8/cmake-build/3rdparty/ippicv/ippicv_lnx/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                /tmp/pip-req-build-99ib2vsi/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 /opt/python/cp38-cp38/bin/python (ver 3.8.6)
    Libraries:                   libpython3.8.a (ver 3.8.6)
    numpy:                       /tmp/pip-build-env-qru8fff1/overlay/lib/python3.8/site-packages/numpy/core/include (ver 1.17.3)
    install path:                python

  Python (for build):            /bin/python2.7

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

  Install to:                    /tmp/pip-req-build-99ib2vsi/_skbuild/linux-x86_64-3.8/cmake-install
-----------------------------------------------------------------
在印刷品中,我发现了以下信息:

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (58.109.100)
      avformat:                  YES (58.61.100)
      avutil:                    YES (56.60.100)
      swscale:                   YES (5.8.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)
似乎我的两台服务器都有FFMPEG,没有GStreamer。那么,有没有其他原因可能导致这种差异

顺便说一下,我已经尝试将API首选项传递给构造函数,如下所示:

cap = cv2.VideoCapture("rtsp://admin:xxx@xxx/Streaming/Channels/1", cv2.CAP_FFMPEG)

但是,传递参数并没有解决问题,我也无法读取视频帧。

OpenCV使用不同的后端来读取/写入/编码/解码视频流,具体取决于您的计算机上安装了什么以及您使用OpenCV构建的哪个后端。 这可以解释为什么即使使用相同的OS和OpenCV版本,在不同的服务器上也会得到不同的结果:可以在其中一个服务器上使用GStreamer,而不能在另一个服务器上使用GStreamer

你可以在网上找到一些关于这些后端的信息

IIRC在Linux上,OpenCV使用GStreamer作为默认后端,您使用FFMPEG测试了流。OpenCV也可以使用FFMPEG,尝试将
cv.CAP\FFMPEG
作为API首选项传递给
cv.VideoCapture
构造函数:

cap = cv2.VideoCapture("rtsp://admin:xxx@xxx/Streaming/Channels/1", cv.CAP_FFMPEG)
关于详细性,OpenCV videoio模块并没有那么详细。
但有些后端是,例如,您可以使用
GST\u DEBUG
环境变量增加GStreamer的详细性,请参阅。对于FFMPEG,您可以使用
FFREPORT
环境变量或一些
AV_LOG*
变量,请参阅。

感谢您提供的信息,我已经了解了很多有关OpenCV后端的知识。我试图确定我的操作系统是否有不同的后端,但是,我的两台服务器有相同的OpenCV后端。我会将结果打印添加到问题中。您确定两台服务器上的FFMPEG版本完全相同吗?使用
cv2.getBuildInformation()
显示的版本是用于构建OpenCV的版本,可能与运行时使用的版本不同。如果您自己在一台服务器上而不是在另一台服务器上构建,则可能会发生这种情况。