Can't get RTSP stream with openCV 4.5.2

Hi, i am trying to get rtsp stream with OpenCV and cant do it.

I am using the stream like this: rtsp://admin:123456@xx.xx.x.xxx:7070 and VLC player open up and play the life video stream perfect.

And i am trying to show the stream in OpenCV window, but the frame is empty everytime:

cv::VideoCapture source("rtsp://admin:123456@xx.xx.x.xxx:7070");
 while (cv::waitKey(1) < 1)
    {
        source >> frame;
        if (frame.empty())
        {
            cv::waitKey();
            break;
        }
         cv::namedWindow("output");
        cv::imshow("output", frame); 
    }

My camera: ACTI d64, here is a web site: D64 | ACTi Corporation

Also i try to get an http mjpeg stream, but result is the same (frame is empty), i can run this script in Firefox browser and can see a life videostream: http://xx.xx.x.xxx/cgi-bin/encoder?USER=Admin&PWD=123456&GET_STREAM

Please help me to get it work with OpenCV VideoCapture class

std::cout << cv::getBuildInformation();

that is important information. get the information. post it here.

you need to add error checking in your use of the VideoCapture object. you haven’t checked source.isOpened(). check that once, not repeatedly.

keep the check for frame.empty(), but remove the superfluous waitKey in that branch.

put a waitKey call in the loop itself. since you’re missing that, you will NOT see the GUI show anything but an empty window, if at all.

move the namedWindow outside of the loop. that’s supposed to be called once, to create a window explicitly (but it’s not required at all).

1 Like

Thank you for reply!!! Here is getBuildInformation

[ERROR:0] global D:\build\opencv_next\modules\videoio\src\cap.cpp (162) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(5.0.0-pre) D:\build\opencv_next\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): rtsp://admin:123456@10.36.1.132:7070/ in function ‘cv::icvExtractPattern’

General configuration for OpenCV 5.0.0-pre =====================================
Version control: unknown

Extra modules:
Location (extra): D:/build/opencv_next/extramodules/modules
Version control (extra): unknown

Platform:
Timestamp: 2021-08-24T15:07:18Z
Host: Windows 10.0.19043 AMD64
CMake: 3.21.1
CMake generator: Visual Studio 16 2019
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/MSBuild.exe
MSVC: 1929
Configuration: Debug 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: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe (ver 19.29.30133.0)
C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MT /O2 /Ob2 /DNDEBUG
C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP /MTd /Zi /Ob0 /Od /RTC1
C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe
C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /MT /O2 /Ob2 /DNDEBUG
C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /MTd /Zi /Ob0 /Od /RTC1
Linker flags (Release): /machine:x64 /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
Linker flags (Debug): /machine:x64 /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
ccache: NO
Precompiled headers: NO
Extra dependencies: wsock32 comctl32 gdi32 ole32 setupapi ws2_32 freetype harfbuzz cudart.lib nppc.lib nppial.lib nppicc.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.4/lib/x64
3rdparty dependencies: libprotobuf libclapack ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf zlib quirc

OpenCV modules:
To be built: 3d alphamat aruco barcode bgsegm bioinspired calib ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot quality rapid reg saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto xstereo
Disabled: java_bindings_generator python_bindings_generator python_tests world
Disabled by dependency: mcc python3 rgbd
Unavailable: cvv hdf java julia matlab ovis python2 sfm viz
Applications: perf_tests
Documentation: NO
Non-free algorithms: NO

Windows RT support: NO

GUI:
Win32 UI: YES
VTK support: NO
Built-in Unicode font: YES

Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build-libjpeg-turbo (ver 2.1.0-62)
WEBP: build (ver encoder: 0x020f)
PNG: build (ver 1.6.37)
TIFF: build (ver 42 - 4.2.0)
JPEG 2000: build (ver 2.4.0)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (58.134.100)
avformat: YES (58.76.100)
avutil: YES (56.70.100)
swscale: YES (5.9.100)
avresample: YES (4.0.0)
GStreamer: NO
Media Foundation: YES
DXVA: YES

Parallel framework: Concurrency

Trace: YES (with Intel ITT)

Other third-party libraries:
Lapack: YES (libclapack 3.9.0)
Eigen: YES (ver 3.4.0)
Custom HAL: NO
Protobuf: build (3.5.1)

NVIDIA CUDA: YES (ver 11.4, CUFFT CUBLAS)
NVIDIA GPU arch: 35 37 50 52 60 61 70 75 80 86
NVIDIA PTX archs:

cuDNN: YES (ver 8.2.2)

OpenCL: YES (NVD3D11)
Include path: D:/build/opencv_next/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python 3:
Interpreter: C:/Users/User/AppData/Local/Programs/Python/Python39/python.exe (ver 3.9.6)
Libraries: NO
numpy: C:/Users/User/AppData/Local/Programs/Python/Python39/lib/site-packages/numpy/core/include (ver 1.21.2)
install path: -

Python (for build): C:/Users/User/AppData/Local/Programs/Python/Python39/python.exe

Install to: D:/build/opencv_next/build/install

rewrite the code like this:

cv::VideoCapture source("rtsp://admin:123456@xx.xx.x.xxx:7070");
  if (source.isOpened())
    {

    }
 while (cv::waitKey(1) < 1)
    {
        source >> frame;
        if (frame.empty())
        {
                break;
        }
         
        cv::imshow("output", frame); 
    }
cv::namedWindow("output");

trying to set the break point and go through the if(source.isOpened()) and it never goes inside the if

no gstreamer means you have very little chance of reading the network stream.

try if standalone ffmpeg in a terminal can read your stream. if that succeeds, OpenCV may be able to use ffmpeg. if not, then not.

the block is empty. it does nothing. your compiler might have generated no code for it.

you should write code that only runs the loop if source.isOpened())

and you put the namedWindow call in a useless place. put it before the loop, after the check for isOpened has succeeded

1 Like

If i build the OpenCV from source with GStreamer support, does it mean that this code will be called with GStreamer? or GStreamer is an analog of VideoCapture class?

cv::VideoCapture source("rtsp://admin:123456@xx.xx.x.xxx:7070");

I will also try the standalone ffmpeg

Hi, i tried to use standalone ffmpeg in a terminal and got the stream.

Here is command that i use to get the stream: ffplay.exe rtsp://admin:123456@xx.x.xx.xxx:7070

Here is ffmpeg output

D:\Загрузки\ffmpeg\bin>ffplay.exe rtsp://admin:123456@10.36.1.132:7070
ffplay version 2021-09-11-git-3e127b595a-full_build-www.gyan.dev Copyright (c) 2003-2021 the FFmpeg developers
built with gcc 10.3.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 57. 5.100 / 57. 5.100
libavcodec 59. 7.103 / 59. 7.103
libavformat 59. 5.100 / 59. 5.100
libavdevice 59. 0.101 / 59. 0.101
libavfilter 8. 9.100 / 8. 9.100
libswscale 6. 1.100 / 6. 1.100
libswresample 4. 0.100 / 4. 0.100
libpostproc 56. 0.100 / 56. 0.100
Input #0, rtsp, from ‘rtsp://admin:123456@10.36.1.132:7070’:f=0/0
Metadata:
title : Session streamed by RTP/RTSP server
comment : ACTi.COM Streaming Media v
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 640x480 [SAR 1:1 DAR 4:3], 12.33 tbr, 90k tbn
Stream #0:1: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 640x480 [SAR 1:1 DAR 4:3], 6.08 tbr, 90k tbn
[swscaler @ 00000266d0aa4a00] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0ab1f40] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0ad80c0] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0b02b80] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0b23ec0] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0b500c0] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0b7df00] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0ba5f00] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0bc9e80] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0bd7d00] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0bf4cc0] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0c2ae80] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 00000266d0c3ecc0] deprecated pixel format used, make sure you did set range correctly
6.41 M-V: 0.006 fd= 19 aq= 0KB vq= 370KB sq= 0B f=0/0

Could you please explain how to run OpenCV with that stream? Thank you very much

What happens if you run the below code

cv::VideoCapture source("rtsp://admin:123456@xx.xx.x.xxx:7070");
if (source.isOpened())
  std::cout << "IP cam streaming" << std::endl;
else
  std::cout << "Unable to open IP cam" << std::endl;

Personally I have not had any problems streaming rtsp from ip cam’s unless they require authentication.

1 Like

Unable to open IP cam

Have you tried changing from mjpeg to h264?

1 Like

Yes I have tried to use different streams., mjpeg and h264., result is the same

oookay so ffmpeg can do it… then OpenCV should be able to do it too.

I don’t know if that’ll help but try adding the apiPreference argument:

cv::VideoCapture source("rtsp://admin:123456@xx.xx.x.xxx:7070", cv::CAP_FFMPEG);

I’m worried that OpenCV for some reason might have chosen some other backend that can’t handle the stream, so asking for CAP_FFMPEG explicitly might help.

the only other oddity I see is yuvj420p being reported. I don’t know if that is fine or not, or how to handle it if it is the issue.

2 Likes

Here we go, it does a trick!!! thank you very much for help and spent time!!!