Skip to main content

GStreamer Test Example

Here's a simple example of using GStreamer with OpenCV in Python:

gstreamer_test.py
import cv2
from datetime import datetime

# Print OpenCV version
print("OpenCV Version:", cv2.__version__)
print(cv2.getBuildInformation())

# You will have to change this pipeline to work with your setup.
pipeline = (
"v4l2src device=/dev/video0 ! "
"image/jpeg, width=1920, height=1080, framerate=30/1 ! "
"jpegdec ! "
"videoconvert ! "
"video/x-raw, format=BGR ! appsink"
)

cap = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)

print("This will capture 20 frames then exit.")
if not cap.isOpened():
print("Error: Unable to open camera")
else:
loop_limit = 20
loop_count = 0
while loop_count < loop_limit:
ret, frame = cap.read()
if not ret:
print("Error: Unable to read frame")
break

# Get current timestamp
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# Retrieve the frame ID from the video capture
frame_id = int(cap.get(cv2.CAP_PROP_POS_FRAMES))

# Print frame ID and timestamp
print(f"Frame ID: {frame_id}, Timestamp: {timestamp}")

cv2.imshow('USB Camera', frame)

cv2.waitKey(10)


loop_count += 1


cap.release()
cv2.destroyAllWindows()