Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capturing rtsp camera using OpenCV python

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.

like image 450
Nik391 Avatar asked Nov 29 '16 21:11

Nik391


People also ask

How do I stream IP camera in Python?

Python code to read the RTSP stream To retrieve the images, we will use the OpenCV library which allows us to create image processing algorithms. To access the RTSP stream, we just need to specify the URL of the camera. Once the script has started, a window will appear with the video from the IP camera's RTSP stream.

How do I get RTSP stream from my camera?

Before you have your RTSP address entered, you're going to go into the web interface for the camera you're going to be streaming and go to “Set Up”, “Network”, and then “Port.” You need to make sure your RTSP Port is set to 554. This should be the default setting, but it doesn't hurt to check before you get going!

Can you record RTSP?

To record RTSP streams on Windows machines, you must first set up ffmpeg on your machine: Install FFMPEG and unzip it to C:\Program Files\ffmpeg\bin. Create a PATH entry. Open the Windows menu and type "Path".


2 Answers

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)
like image 178
Ryan Avatar answered Oct 21 '22 04:10

Ryan


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
like image 1
Plinio Bueno Andrade Silva Avatar answered Oct 21 '22 03:10

Plinio Bueno Andrade Silva