Hi, I trained a model by yolo to detecet objects on videos and tracking by deep_sort_realtime.deepsort_tracker. the detection accuracy is very well but id of one object by changing the frames change a lot. how can i solve it by giving a stable id to a object???
I used a lot of ReID models but i have got a lot of error. my code import cv2
import numpy as np
from ultralytics import YOLO
from deep_sort_realtime.deepsort_tracker import DeepSort
# Load the YOLO model by providing the path to the trained model's weights.
model = YOLO('/home/train2/weights/best.pt')
# Initialize the Deep SORT tracker.
deepsort = DeepSort(
max_age=40, # Maximum number of frames a track can be missing before it's deleted.
n_init=2, # Minimum number of consecutive detections to confirm a track.
nn_budget=None, # Number of features to keep for matching (None means unlimited).
)
# Open the video file using OpenCV's VideoCapture function.
video_path = '/home/pythonProject/s3.mp4'
cap = cv2.VideoCapture(video_path)
# Process each frame from the video.
while cap.isOpened():
ret, frame = cap.read() # Read a single frame from the video.
if not ret:
break # Break the loop if no frame is returned (end of video).
# Perform object detection on the current frame using the YOLO model.
results = model(frame, stream=True)
# Initialize a list to store detections in the current frame.
detections = []
for result in results: # Loop over the detected objects in the current frame.
for box in result.boxes.data.tolist(): # Get the bounding box for each detection.
x1, y1, x2, y2, conf = box[:5] # Extract the coordinates and confidence score.
# Filter out detections with a confidence score lower than 0.5.
if conf < 0.5:
continue # Skip low-confidence detections.
# Prepare the bounding box in (x, y, width, height) format for Deep SORT.
bbox = [x1, y1, x2 - x1, y2 - y1]
# Append the detection (bounding box and confidence) to the list of detections.
detections.append((np.array(bbox), conf))
# Update the Deep SORT tracker with the current frame's detections.
outputs = deepsort.update_tracks(detections, frame=frame)
# Process each track output from the tracker.
for track in outputs:
track_id = track.track_id # Get the unique ID for this track (assigned by Deep SORT).
bbox = track.to_ltrb() # Convert the bounding box to (left, top, right, bottom) format.
x1, y1, x2, y2 = map(int, bbox) # Convert the bounding box coordinates to integers.
# Draw a bounding box for each tracked object.
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) # Green color for all tracked objects.
cv2.putText(frame, f'ID: {track_id}', (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, .51, (0, 255, 0), 2)
# Display the processed frame with tracking.
cv2.imshow('YOLO Movement Detection', frame)
# Exit the loop if the 'q' key is pressed.
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object and close all OpenCV windows.
cap.release()
cv2.destroyAllWindows()