File size: 1,591 Bytes
f5a7e32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
detector.py
YOLOv8 person detection wrapper.
Returns raw lists so other modules stay decoupled from supervision version.
"""
from __future__ import annotations
import numpy as np


class PersonDetector:
    """
    Wraps YOLOv8. Returns detections as a plain dict so the rest of the
    pipeline never touches supervision directly from here.
    """

    def __init__(
        self,
        model_path: str = "yolov8n.pt",
        conf: float = 0.30,
        iou: float  = 0.50,
        device: str = "cpu",
    ) -> None:
        from ultralytics import YOLO
        print(f"[Detector] loading {model_path} on {device}")
        self.model  = YOLO(model_path)
        self.conf   = conf
        self.iou    = iou
        self.device = device

    def detect(self, frame: np.ndarray) -> list[dict]:
        """
        Run YOLOv8 on one BGR frame.

        Returns:
            list of {"xyxy": [x1,y1,x2,y2], "conf": float}
        """
        results = self.model(
            frame,
            conf=0.1,         # Pass low conf detections to ByteTrack
            iou=self.iou,
            classes=[0],      # person only
            imgsz=480,        # Faster inference
            verbose=False,
            device=self.device,
        )[0]

        out = []
        boxes = results.boxes
        if boxes is None or len(boxes) == 0:
            return out

        for box in boxes:
            xyxy = box.xyxy[0].cpu().numpy().tolist()   # [x1,y1,x2,y2]
            conf = float(box.conf[0].cpu().numpy())
            out.append({"xyxy": xyxy, "conf": conf})

        return out