0

I have an IP-Camera and it streams on every app and on every device except for Python. The RTSP I'm using ('rtsp://admin:pass@...:554/onvif1') is working on: VLC, Onvif Device Manager, YooSee, and SurveillanceStation, but not when I use this code in PythonCharm:

# Pakketten importeren.
import os
import re
import cv2

# Show system-paths ('C:\ffmpeg\bin' is there and working fine in Terminal).
print("\nPYTHONPATH: " + os.getenv('PYTHONPATH'))
print("\nPATH: " + re.sub(";", "\n      ", os.getenv("PATH")))

# Show version CV2.
print("\nCV2.version: " + str(cv2.version))
print("\nCV2.getBuildInformation: " + str(cv2.getBuildInformation()))

# Settings ip-camera (Gembird, ICAM-WRHD-01).
# myLink = "Resources\muis_op_rooftocht.mp4"                        # Video from file in local folder is working.
# myLink = 0                                                        # Video from build-in camera is working.
myLink = "rtsp://user:pass@192.xxx.xxx.xxx:554/onvif1"              # Video from ip-camera is NOT working!
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"  # UDP is recommended, I've tried 'tcp', '0' and 'dummy'.

# Open videostream.
cap = cv2.VideoCapture(myLink, cv2.CAP_FFMPEG)

# Check if videostream is opened.
if not cap.isOpened():
    raise IOError("Kan video uit Webcam niet openen!")

# Play video until 'ESC' is pressed.
while True:
    success, img = cap.read()
    cv2.imshow("Video Hansje de Muis", img)
    k = cv2.waitKey(1)
    if k == 27:
        break

# Empty memory.
cap.release()
cv2.destroyAllWindows()

I get the following error:

Traceback (most recent call last):
  File "C:/Users/Gebruiker/Documents/Projecten/OpenCvPhyton/chapter1.py", line 25, in <module>
    raise IOError("Kan video uit Webcam niet openen!")
OSError: Kan video uit Webcam niet openen!

Process finished with exit code 1

I also have the following output, generated by the program 'chapter1.py':

PYTHONPATH: C:\Users\Gebruiker\Documents\Projecten\OpenCvPhyton
PATH: C:\Users\Gebruiker\Documents\Projecten\OpenCvPhyton\venv\Lib\site-packages\cv2\../../x64/vc14/bin
      C:\Users\Gebruiker\Documents\Projecten\OpenCvPhyton\venv\Scripts
      C:\Windows\system32
      C:\Windows
      C:\Windows\System32\Wbem
      C:\Windows\System32\WindowsPowerShell\v1.0\
      C:\Windows\System32\OpenSSH\
      C:\WINDOWS\system32
      C:\WINDOWS
      C:\WINDOWS\System32\Wbem
      C:\WINDOWS\System32\WindowsPowerShell\v1.0\
      C:\WINDOWS\System32\OpenSSH\
      E:\Programma's\PuTTY\
      C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
      C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
      C:\Program Files\dotnet\
      C:\Users\Gebruiker\AppData\Local\Microsoft\WindowsApps
      E:\Programma's\Microsoft VS Code\bin
      C:\Users\Gebruiker\.dotnet\tools
      C:\ffmpeg\bin
      

CV2.version: <module 'cv2.version' from 'C:\\Users\\Gebruiker\\Documents\\Projecten\\OpenCvPhyton\\venv\\lib\\site-packages\\cv2\\version.py'>

CV2.getBuildInformation: 
General configuration for OpenCV 4.5.5 =====================================
  Version control:               4.5.5

  Platform:
    Timestamp:                   2021-12-28T06:21:01Z
    Host:                        Windows 10.0.17763 AMD64
    CMake:                       3.22.1
    CMake generator:             Visual Studio 14 2015
    CMake build tool:            MSBuild.exe
    MSVC:                        1900
    Configuration:               Debug 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 (16 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 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24245.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 14.0/VC/bin/x86_amd64/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:         YES
    Extra dependencies:          wsock32 comctl32 gdi32 ole32 setupapi ws2_32
    3rdparty dependencies:       libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf zlib 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 python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI:                           WIN32UI
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.1.2-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
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.6/cmake-build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.6/cmake-build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)

  OpenCL:                        YES (NVD3D11)
    Include path:                D:/a/opencv-python/opencv-python/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/hostedtoolcache/windows/Python/3.6.8/x64/python.exe (ver 3.6.8)
    Libraries:                   C:/hostedtoolcache/windows/Python/3.6.8/x64/libs/python36.lib (ver 3.6.8)
    numpy:                       C:/hostedtoolcache/windows/Python/3.6.8/x64/lib/site-packages/numpy/core/include (ver 1.13.3)
    install path:                python/cv2/python-3

  Python (for build):            C:/hostedtoolcache/windows/Python/2.7.18/x64/python.exe

  Java:                          
    ant:                         NO
    JNI:                         C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.312-7/x64/include C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.312-7/x64/include/win32 C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.312-7/x64/include
    Java wrappers:               NO
    Java tests:                  NO    
  Install to:                    D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.6/cmake-install

This is tested on a Windows 10 and 11 machine with the same results. I've browsed this and other websites and I've seen every answer ;) but not the right one for this problem. What am I missing?

In PyCharm Terminal ffplay -fflags nobuffer -rtsp_transport udp rtsp://user:pass@192.xxx.xxx.xxx:554/onvif1 plays the stream from external camera (with a lot of a small errors). Why doesn't it play with OpenCV?

SMoKuM
  • 19
  • 6
  • Could you try with `rtsp://rtsp.stream/pattern`. Your code work for me with that link. – David Jan 19 '22 at 23:44
  • Yes, your URL also works for me. But my URL still doesn't..... at least not in Python OpenCV. – SMoKuM Jan 20 '22 at 00:03
  • Did your password contain character other than letter like `@` or `:` or `/` ? – David Jan 20 '22 at 01:19
  • No, the password does not contain a special character... – SMoKuM Jan 20 '22 at 08:00
  • My last try will be to disable authentication on your camera. If it work, summit a bug to opencv. – David Jan 20 '22 at 11:28
  • I can't disable authentication on the camera, and thats a good thing. :) Thanks for your help! – SMoKuM Jan 20 '22 at 16:40
  • For testing, you may try using FFmpeg (command line tool) directly instead of OpenCV. Here is an [example](https://stackoverflow.com/a/70207942/4926757). Remove the `'-discard', 'nokey'` argument. The OpenCV issue may be some default argument like TCP / UDP or connection timeout. – Rotem Jan 20 '22 at 23:27
  • I've already tested with FFmpeg and it worked. – SMoKuM Jan 24 '22 at 16:31

1 Answers1

0

I found a 'workaround' to this problem:

# Import packages.
import cv2
import subprocess as sp
import numpy as np

# Settings ip-camera (Gembird, ICAM-WRHD-01).
myCamera_Name = "Gembird"
myCamera_Link = "rtsp://user:pass@192.168.*.*:554/onvif1"
myCamera_Height = 720
myCamera_Width = 1280

FFMPEG_BIN = "ffmpeg"  # Works with Windows when ffmpeg.exe is in the path.
ffmpeg_cmd = [FFMPEG_BIN,
              '-rtsp_transport', 'udp',
              '-max_delay', '30000000',  # 30 seconds
              '-i', myCamera_Link,
              '-f', 'rawvideo',
              '-pix_fmt', 'bgr24',
              '-vcodec', 'rawvideo', '-an', 'pipe:']

# Open sub-process that gets in_stream as input and uses stdout as an output PIPE.
process = sp.Popen(ffmpeg_cmd, stdout=sp.PIPE)

while True:
    raw_frame = process.stdout.read(myCamera_Width * myCamera_Height * 3)

    if len(raw_frame) != (myCamera_Width * myCamera_Height * 3):
        print('Error reading frame!')  # Break the loop in case of an error (too few bytes were read).
        break

    # Transform the byte read into a numpy array, and reshape it to video frame dimensions
    frame = np.frombuffer(raw_frame, np.uint8).reshape((myCamera_Height, myCamera_Width, 3))

    # Show frame for testing
    cv2.imshow('frame', frame)
    
    # Check if user is pressing 'ESC'.
    k = cv2.waitKey(1)
    if k == 27:
        break

# Close process and window.
process.stdout.close()
process.wait()
cv2.destroyAllWindows()

So, I can still use OpenCV but I have to open the stream with:

raw_frame = process.stdout.read(myCamera_Width * myCamera_Height * 3)

# Transform the byte read into a numpy array, and reshape it to video frame dimensions
frame = np.frombuffer(raw_frame, np.uint8).reshape((myCamera_Height, myCamera_Width, 3))

I'm not sure if this is the best way but it works!

SMoKuM
  • 19
  • 6