Detection and tracking in real-time using the Python API

I wish to use my webcam in real-time for detection and tracking based on this example : https://github.com/hailo-ai/Hailo-Application-Code-Examples/blob/main/runtime/python/detection_with_tracker/detection_with_tracker.py
, Considering that “supervision” does not allow it, can you please suggest a solution?

To modify your detection script for webcam use:

  1. Replace cv2.VideoCapture("video.mp4") with cv2.VideoCapture(0) for live webcam input.
  2. Ensure the loop processes frames in real-time.

Here’s a modified structure:

import cv2

cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("Error: Could not open webcam.")
    exit()

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

# Initialize YOLOX model and tracker here

while True:
    ret, frame = cap.read()
    if not ret:
        print("Error: Failed to capture image")
        break
    
    detections = yolox_inference(frame)  # Replace with actual YOLOX inference
    tracked_objects = tracker.update(detections)  # Update tracker
    
    for obj in tracked_objects:
        bbox = obj.bbox
        id = obj.id
        cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
        cv2.putText(frame, f'ID: {id}', (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
    
    cv2.imshow("Webcam Detection with Tracker", frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

This structure should work with the existing detection_with_tracker.py script.

Let me know if you need help integrating specific parts of your code!

Can you explain more.hiw initialisée the model yolo, i’m sorry for this question, because i’m a beginner :pray:

Hey @mAty1901

This is a way to do it :

from hailo_platform import (HEF, VDevice,
                            FormatType, HailoSchedulingAlgorithm)
from hailo_platform import InferVStreams, InputVStreamParams, OutputVStreamParams, ConfigureParams, HailoStreamInterface

# Load the pre-compiled Hailo model (replace with your path)
model_path = "/path/to/yolox.hef"

# Configure the Hailo device
device = configure.VDevice()
hef = configure.HEF(model_path)

# Load the inference pipeline
network_group = device.configure(hef)

# Initialize the inference stream
inference_vstreams = InferVStreams(network_group)

# Function for YOLOX inference
def yolox_inference(frame):
    # Preprocess the frame as needed for YOLOX model (resize, normalization, etc.)
    input_data = preprocess(frame)

    # Run inference
    result = inference_vstreams.infer(input_data)
    
    # Post-process the results (detection bounding boxes)
    detections = postprocess(result)
    return detections

I would also recommend looking at Hailo-Application-Code-Examples/runtime/python/utils.py at main · hailo-ai/Hailo-Application-Code-Examples · GitHub

But i cantine use hailo sdk client in raspberry

1 Like

You’re right. I’ve updated the imports, and they’re now working correctly.

1 Like

thanks for your reply omria,
i tested this code, but i have the following error message :slight_smile:

Blockquote

inference_vstreams = InferVStreams(network_group)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: InferVStreams.init() missing 2 required positional arguments: ‘input_vstreams_params’ and ‘output_vstreams_params’

Blockquote


I modified the “inference_vstreams” :

Blockquote
inference_vstreams = InferVStreams(network_group, input_vstreams_params, output_vstreams_params)

Blockquote

and I got this error message " ‘InferVStreams’ object has no attribute ‘_infer_pipeline’