Can't get an Opencv, cv2.VideoCapture rtsp steam to work from IP camera

1.8k Views Asked by At

I am trying to read a rtsp stream from my Ip camera using Opencv and running Linux. The camera is a Floureon IPC 360 from China. I am trying to develop some facial recognition code.

I am using the following code:

import numpy as np
import cv2

vcap = cv2.VideoCapture("rtsp://192.168.1.240:554/realmonitor?channel=0")
print(vcap)
while(1):
    ret, frame = vcap.read()
    print (ret,frame)
    cv2.imshow('VIDEO', frame)
    #cv2.imwrite('messigray.png',frame)
    cv2.waitKey(1)

$ python  w.py
<VideoCapture 0x7fc685598230>
(False, None)
Traceback (most recent call last):
  File "w.py", line 9, in <module>
    cv2.imshow('VIDEO', frame)
cv2.error: OpenCV(4.1.0) /io/opencv/modules/highgui/src/window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

cv2.imshow is failing as the frame is 'None' & (ret is False).

In a separate window I can run openRTSP :

./openRTSP  -4 -P 10 -F cam_eight -t -d 8 rtsp://192.168.1.240:554/realmonitor?channel=0

Which creates me a nice mp4 file that I can play: 107625 Sep 12 19:08 cam_eight-00000-00010.mp4

OpenRTSP works with or without the t (tcp). I have also tried supplying the admin:123456 credentials to the cv2.VideoCapture line, which openRTSP doesn't appear to require.

Any ideas why cv2.VideoCapture is apparently failing ?

I have tried variants of the above code, but nothing seems to work. I have enabled ONVIF on the camera

1

There are 1 best solutions below

3
On

According to other answers, it isn't possible to acquire ONVIF streams with OpenCV, since it defaults the stream to use the tcp protocol, while ONVIF relies on udp. You should define the environment variable OPENCV_FFMPEG_CAPTURE_OPTIONS to skip the default setting to tcp, as can be seen in the original source code here:

OPENCV_FFMPEG_CAPTURE_OPTIONS=whatever

If you want to properly configure the capture options, then you should refer to the ffmpeg documentation, which is used internally by OpenCV. As stated in the linked answer, keys and values are separated with ; and pairs are separated via |.