roboflow / supervision

We write your reusable computer vision tools. 💜
https://supervision.roboflow.com
MIT License
23.94k stars 1.79k forks source link

Low FPS on video stream #1250

Closed gmijo47 closed 5 months ago

gmijo47 commented 5 months ago

Search before asking

Question

Hi there, I'm working on some realtime object detecion and my fps is always 0.3 FPS (both on GPU T4, and CPU), i started form time_in_zone example. On start it spikes, for single second GPU-40FPS, CPU 10 FPS, and as time goes it goes down to 0.3FPS

requirments.txt supervision>=0.20.0 ultralytics==8.2.22 opencv-python==4.8.0.76 inference==0.9.17

my app.py

from utils.configHandler import ConfigHandler
from inference import InferencePipeline
from inference.core.interfaces.camera.entities import VideoFrame
from ultralytics import YOLO
from utils.functions import find_in_list

import cv2
import supervision as sv
from typing import List

COLORS = sv.ColorPalette.from_hex(["#E6194B", "#3CB44B", "#FFE119", "#3C76D1"])
COLOR_ANNOTATOR = sv.ColorAnnotator(color=COLORS)
LABEL_ANNOTATOR = sv.LabelAnnotator(
    color=COLORS, text_color=sv.Color.from_hex("#000000")
)

class DataProcessor:
    def __init__(self, classes: List[int], config):
        self.classes = classes
        self.config = config
        self.tracker = sv.ByteTrack(minimum_matching_threshold=0.8)
        self.fps_monitor = sv.FPSMonitor()

    def on_prediction(self, detections: sv.Detections, frame: VideoFrame) -> None:
        self.fps_monitor.tick()
        fps = self.fps_monitor.fps

        detections = detections[find_in_list(detections.class_id, self.classes)]
        detections = self.tracker.update_with_detections(detections)

        annotated_frame = frame.image.copy()
        match fps:
            case fps if fps <= 5.0:
                bg_color = "#D20103"
            case fps if fps <= 12.5:
                bg_color = "#FFDE59"
            case _:
                bg_color = "#7DDA58"
        annotated_frame = sv.draw_text(
            scene=annotated_frame,
            text=f"{fps:.2f}",
            text_anchor=sv.Point(40, 30),
            background_color=sv.Color.from_hex(bg_color),
            text_color=sv.Color.from_hex("#000000"),
        )

        annotated_frame = COLOR_ANNOTATOR.annotate(
            scene=annotated_frame,
            detections=detections,
        )
        labels = [
            f"#{tracker_id}"
            for tracker_id in detections.tracker_id
        ]
        annotated_frame = LABEL_ANNOTATOR.annotate(
            scene=annotated_frame,
            detections=detections,
            labels=labels,
        )              
        print("fps" + str(fps))
        if self.config.debug:
            cv2.imshow("Processed Video", annotated_frame)
            cv2.waitKey(1)

def main() -> None:
    config = ConfigHandler()

    model = YOLO(config.weights)

    def inference_callback(frame: VideoFrame) -> sv.Detections:
        results = model(frame.image, verbose=config.model_verbose, conf=config.confidence, device=config.device)[0]
        return sv.Detections.from_ultralytics(results).with_nms(threshold=config.iou)

    processor = DataProcessor(classes=config.classes, config=config)

    pipeline = InferencePipeline.init_with_custom_logic(
        video_reference=config.rtsp_url,
        on_video_frame=inference_callback,
        on_prediction=processor.on_prediction,
    )

    pipeline.start()

    try:
        pipeline.join()
    except KeyboardInterrupt:
        pipeline.terminate()

if __name__ == "__main__":
    main()

Thanks in advance.

Additional

No response

SkalskiP commented 5 months ago

Hi @gmijo47 👋🏻 Let me convert this question into a discussion first.