Most of the IP cameras supports Real Time Streaming Protocol (RTSP) to control audio and video streaming. This tutorial provides example how to capture RTSP stream from IP camera using OpenCV and Python. OpenCV provides VideoCapture class which allows to capture video from video files, image sequences, webcams, IP cameras, etc. To capture RTSP stream from IP camera we need to specify RTSP URL as argument.
Please use the following URLs to use your INSTAR camera with third-party software:
- RTSP Stream 1: rtsp:https://user:[email protected]:port/11
- RTSP Stream 2: rtsp:https://user:[email protected]:port/12
- RTSP Stream 3: rtsp:https://user:[email protected]:port/13
User and password are your camera login and the default RTSP port is 554
- e.g.:
rtsp:https://admin:[email protected]:554/12
mkdir ./opencv-rtsp && cd opencv-rtsp
python -m venv .env
source .env/bin/activate
pip install opencv-python
import cv2
import os
RTSP_URL = 'rtsp:https://admin:[email protected]/11'
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)
if not cap.isOpened():
print('Cannot open RTSP stream')
exit(-1)
while True:
success, img = cap.read()
cv2.imshow('RTSP stream', img)
if cv2.waitKey(1) & 0xFF == ord('q'): # Keep running until you press `q`
break