10
votes

I have a remote camera streaming through rtsp protocol and am trying to access it in OpenCV 2.13.1 using python with following code.

camera = cv2.VideoCapture("rtsp://admin:<port>@<ip>/xyz/video.smp")

However, when I do that I get the following warning

WARNING: Couldn't read movie file rtsp://admin:<port>@<ip>/xyz/video.smp

I have also tried doing this:

camera = cv2.VideoCapture.open("rtsp://admin:<port>@<ip>/xyz/video.smp")

but when I do this I get this error:

`AttributeError: 'builtin_function_or_method' object has no attribute` 'open'

I did searched quite a bit about this problem but couldn't get the idea that works for me. Any help would be appreciated.

2
example with rtsp streams which works for me on Python 2. Try this streams - maybe it is problem with some codecs. - furas
No unfortunately it didnt worked for me although I am also using python 2 - Nik391
so maybe you have problem with some codecs or something else. I have problem with cv2 in Python 3 - it doesn't read stream event from camera :/ I will try to install it from source code maybe it changes something. pyimagesearch.com/tag/install - furas

2 Answers

20
votes

Credit from RTSP stream and OpenCV (Python):

vcap = cv.VideoCapture("rtsp://192.168.1.2:8080/out.h264")
while(1):
    ret, frame = vcap.read()
    cv.imshow('VIDEO', frame)
    cv.waitKey(1)
1
votes

I had the same problem. My python scrip reading rtsp stream worked only some times and in some computers and I don't figured out why. My solution was initializing the stream from the camera and create a new http stream with VLC. Then, my opencv script reads from "http://127.0.0.1" that is the stream created by the VLC. It's not the best solutoin, but worked out for me.

I made a script inside a .bat file to keep simple to initialize the VLC with the correct configurations. The comando line to do it is:

"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" rtsp://10.0.0.1 input_stream --ipv4-timeout=600000 --sout #transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:http{mux=ffmpeg{mux=flv},dst=:8080/} :no-sout-all :sout-keep