mikel-brostrom / boxmot

BoxMOT: pluggable SOTA tracking modules for segmentation, object detection and pose estimation models
GNU Affero General Public License v3.0
6.71k stars 1.71k forks source link

calculate FPS #1579

Closed microchila closed 2 months ago

microchila commented 2 months ago

Search before asking

Question

How to calculate the FPS indicator?Can you give me an example, thank you very much!!!!

mikel-brostrom commented 2 months ago
import cv2
import numpy as np
from pathlib import Path
import time  # Import time module

from boxmot import DeepOCSORT

def fps_calculator(start_time):
    """Calculate the FPS based on the start time."""
    elapsed_time = time.time() - start_time
    if elapsed_time > 0:
        return 1.0 / elapsed_time
    else:
        return float('inf')  # Handle division by zero

tracker = DeepOCSORT(
    model_weights=Path('osnet_x0_25_msmt17.pt'), # which ReID model to use
    device='cuda:0',
    fp16=False,
)

vid = cv2.VideoCapture(0)

while True:
    ret, im = vid.read()

    # Start timer for object detection
    start_detection_time = time.time()

    # Substitute by your object detector, output has to be N X (x, y, x, y, conf, cls)
    dets = np.array([[144, 212, 578, 480, 0.82, 0],
                    [425, 281, 576, 472, 0.56, 65]])

    # End timer for object detection
    detection_fps = fps_calculator(start_detection_time)

    # Start timer for tracking
    start_tracking_time = time.time()

    # Check if there are any detections
    if dets.size > 0:
        tracker.update(dets, im) # --> M X (x, y, x, y, id, conf, cls, ind)
    # If no detections, make prediction ahead
    else:   
        dets = np.empty((0, 6))  # empty N X (x, y, x, y, conf, cls)
        tracker.update(dets, im) # --> M X (x, y, x, y, id, conf, cls, ind)

    # End timer for tracking
    tracking_fps = fps_calculator(start_tracking_time)

    tracker.plot_results(im, show_trajectories=True)

    # Print FPS information
    print(f"Detection FPS: {detection_fps:.2f}")
    print(f"Tracking FPS: {tracking_fps:.2f}")

    # Break on pressing q or space
    cv2.imshow('BoxMOT detection', im)     
    key = cv2.waitKey(1) & 0xFF
    if key == ord(' ') or key == ord('q'):
        break

vid.release()
cv2.destroyAllWindows()
microchila commented 2 months ago

thanks!!