deepinvalue / yolov9-supervision-tracking-counting

Object detection, tracking, and counting with YOLOv9 and Supervision.
20 stars 0 forks source link

NOT WORKING ON WEBCAM. #2

Closed MuhammadBilal848 closed 7 months ago

MuhammadBilal848 commented 7 months ago

I want to see live inference of yolov9 with tracking but When I set,

SOURCE_VIDEO_PATH = 1
TARGET_VIDEO_PATH = "output.mp4"

and ran the Detection and Tracking cell. It does not work and does not show live screen.

MuhammadBilal848 commented 7 months ago

I added another function in the code for live inference. Repo: https://github.com/MuhammadBilal848/YOLOv9-With-Object-Tracking-Live-Infernce-Supported-

Function

def process_live_video(model, config=dict(conf=0.1, iou=0.45, classes=None,), counting_zone=None, show_labels=False,source = 0):
    model, video_info = setup_model_and_video_info(model, config, source_path=source)  # Use source_path=0 for the default webcam
    byte_tracker = create_byte_tracker(video_info)
    annotators_list, trace_annotator, label_annotator = setup_annotators()
    polygon_zone, polygon_zone_annotator = setup_counting_zone(counting_zone, video_info) if counting_zone else (None, None)
    cap = cv2.VideoCapture(source)

    def callback(frame: np.ndarray, index: int) -> np.ndarray:
        ret, frame = cap.read()
        if not ret:
            return frame 

        frame_rgb = frame[..., ::-1]  # Convert BGR to RGB
        results = model(frame_rgb, size=1440, augment=False)
        detections = ExtendedDetections.from_yolov9(results)
        return annotate_frame(frame, index, video_info, detections, byte_tracker, counting_zone, polygon_zone, polygon_zone_annotator, trace_annotator, annotators_list, label_annotator, show_labels, model)

    try:
        while True:
            ret, frame = cap.read()
            if not ret:
                break  
            annotated_frame = callback(frame, 0)  # Index is not used for live video
            cv2.imshow('Live Inference', annotated_frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

    finally:
        cap.release()
        cv2.destroyAllWindows()

Usage

yolov9_config=dict(conf=0.3, iou=0.45)
process_live_video(model, config=yolov9_config, counting_zone=None, show_labels=True,source=1)