-1

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

pppery
  • 3,731
  • 22
  • 33
  • 46

1 Answers1

0

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 |.

madduci
  • 2,635
  • 1
  • 32
  • 51
  • Hi, Apologies, but I think that I need some clarrification. Are you saying that I need to disable ONVIF on the camera ? Also, why and how do I need to set the OPENCV_FFMPEG_CAPTURE_OPTIONS environment variable ? Would this variable be set from the Linux Bash shell or from within Python ? The documentation says that setting OPENCV_FFMPEG_CAPTURE_OPTIONS disables TCP/IP transport. Why would I need to do this ? Could you be explicit in what I have to do to my camera, the above code and Bash environment please ? – Unreal Admin Sep 12 '19 at 21:28
  • I have modified my python code to include: import os os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = "dummy" but to no avail. – Unreal Admin Sep 12 '19 at 21:33
  • @UnrealAdmin i've added more clarification – madduci Sep 13 '19 at 05:55