Processing live video stream from opencv on raspberry pi 5

@Szymon_Parol
Can you please try this code and let me know if it works?

import degirum as dg
import cv2
from picamera2 import Picamera2
import numpy as np

# Define a frame generator: a function that yields frames from the Picamera2
def frame_generator():
    picam2 = Picamera2()

    # Configure the camera (optional: set the resolution or other settings)
    picam2.configure(picam2.create_preview_configuration())

    # Start the camera
    picam2.start()

    try:
        while True:
            # Capture a frame as a numpy array
            frame = picam2.capture_array()

            # Yield the frame
            yield frame
    finally:
        picam2.stop()  # Stop the camera when the generator is closed

# Define model parameters (replace these with your own values)
face_det_model_name = "scrfd_500m--640x640_quant_hailort_hailo8l_1"
inference_host_address = "@local"
zoo_url = "/home/pi/degirum/scrfd_500m--640x640_quant_hailort_hailo8l_1"
token = ''
# Load the object detection AI model from the model zoo
model = dg.load_model(
    model_name=face_det_model_name,
    inference_host_address=inference_host_address,
    zoo_url=zoo_url,
    token=token,
    overlay_color=(0, 255, 0)  # Green color for bounding boxes
)
# Process the video stream by AI model using model.predict_batch():
for result in model.predict_batch(frame_generator()):
    # Display the frame with AI annotations in a window named 'AI Inference'
    cv2.imshow("AI Inference", result.image_overlay)

    # Process GUI events and break the loop if 'q' key was pressed
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

# Destroy any remaining OpenCV windows after the loop finishes
cv2.destroyAllWindows()
3 Likes