1import cv2
2import os
3
4RTSP_URL = 'rtsp://user:pass@192.168.0.189:554/h264Preview_01_main'
5
6os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'
7
8cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)
9
10if not cap.isOpened():
11 print('Cannot open RTSP stream')
12 exit(-1)
13
14while True:
15 _, frame = cap.read()
16 cv2.imshow('RTSP stream', frame)
17
18 if cv2.waitKey(1) == 27:
19 break
20
21cap.release()
22cv2.destroyAllWindows()
23