Created
March 30, 2024 09:58
-
-
Save mirono/db0876079cc3c3f59b8764e1c6c31c59 to your computer and use it in GitHub Desktop.
Capturing an IP web camera RTSP feed with Python in OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
# RTSP URL of the IP camera | |
rtsp_url = "rtsp://ip:8080/h264.sdp" | |
# Create a VideoCapture object | |
cap = cv2.VideoCapture(rtsp_url) | |
# Check if the camera opened successfully | |
if not cap.isOpened(): | |
print("Error: Could not open camera.") | |
exit() | |
# Loop to read frames from the camera | |
while True: | |
# Read a frame from the camera | |
ret, frame = cap.read() | |
# Check if the frame was successfully captured | |
if not ret: | |
print("Error: Could not read frame.") | |
break | |
# Display the frame | |
cv2.imshow("IP Camera Stream", frame) | |
# Check for keypress (press 'q' to exit) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# Release the VideoCapture object and close all OpenCV windows | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment