Reputation: 81
I want to stream the video from IP camera by using RTSP. But I am having a problem. I have installed prerequirements and also, my RTSP link works on VlC player. But when I tried it in the editor and run it, it says Camera could not be found.
Here is my code.
import cv2
import numpy as np
cap = cv2.VideoCapture("rtsp://admin:[email protected]:xxx/media/video1/video")
while True:
ret, img = cap.read()
if ret == True:
cv2.imshow('video output', img)
k = cv2.waitKey(10)& 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Upvotes: 4
Views: 16120
Reputation: 132
I was able to solve opening an RTSP stream with OpenCV (built with FFMPEG) in Python by setting the following environment variable:
import os
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
FFMPEG defaults to TCP transport, but some RTSP feeds are UDP, so this sets the correct mode for FFMPEG.
Then use:
cv2.VideoCapture(<stream URI>, cv2.CAP_FFMPEG)
ret, frame = cap.read()
while ret:
cv2.imshow('frame', frame)
# do other processing on frame...
ret, frame = cap.read()
if (cv2.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
as usual.
Upvotes: 3
Reputation: 2337
Do check your installation of opencv has the capability to open videos. for this try
cap=cv2.VideoCapture(r"path/to/video/file")
ret,img=cap.read()
print ret
If ret
is True
then your opencv install has the codecs necessary to handle videos and then confirm that the RTSP address is correct.
If ret
is False
then reinstall opencv using the steps here. I would recommend building opencv from source. But try the pre-built libraries first.
Upvotes: 3