13

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.

Nik391
  • 517
  • 2
  • 7
  • 24
  • [example with rtsp streams](https://github.com/furas/my-python-codes/blob/master/cv2/display-stream/example-main.py) which works for me on Python 2. Try this streams - maybe it is problem with some codecs. – furas Nov 29 '16 at 21:20
  • No unfortunately it didnt worked for me although I am also using python 2 – Nik391 Nov 29 '16 at 21:31
  • 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. http://www.pyimagesearch.com/tag/install/ – furas Nov 30 '16 at 12:58
  • Possible duplicate of [RTSP stream and OpenCV (Python)](https://stackoverflow.com/questions/20891936/rtsp-stream-and-opencv-python) – Ollie Graham Oct 24 '19 at 05:09

2 Answers2

21

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)
SiHa
  • 7,830
  • 13
  • 34
  • 43
Ryan
  • 800
  • 1
  • 13
  • 27
  • is there a library that can view these frames in a browser? (e.g. send it using websocket to the client, then decode it using some JS library?) – ierdna Aug 21 '17 at 14:24
  • There is a [jsmpeg] (https://github.com/phoboslab/jsmpeg) to view the live stream on the browser. But first, you need ffmpeg to send the stream to the relay server. – Ryan Aug 21 '17 at 19:10
  • 1
    for streaming you can easiliy build an mjpeg and stream to browser (https://blog.miguelgrinberg.com/post/video-streaming-with-flask) – kappa Apr 13 '19 at 09:53
  • @ierdna you could also look at using [ImageZMQ](https://github.com/jeffbass/imagezmq) along with the StreamingResponse API from [Starlette.io](https://starlette.io) – Ollie Graham Oct 22 '19 at 02:11
1

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