@@ -20,60 +20,57 @@ class KSPTracker(BaseOfflineTracker):
2020 def __init__ (
2121 self ,
2222 path_overlap_penalty : Optional [int ] = 40 ,
23- iou_weight : Optional [int ] = 0.9 ,
24- dist_weight : Optional [int ] = 0.1 ,
25- size_weight : Optional [int ] = 0.1 ,
26- conf_weight : Optional [int ] = 0.1 ,
23+ iou_weight : Optional [float ] = 0.9 ,
24+ dist_weight : Optional [float ] = 0.1 ,
25+ size_weight : Optional [float ] = 0.1 ,
26+ conf_weight : Optional [float ] = 0.1 ,
2727 entry_exit_regions : Optional [List [Tuple [int , int , int , int ]]] = None ,
2828 use_border : Optional [bool ] = True ,
2929 borders : Optional [Set [str ]] = None ,
3030 border_margin : Optional [int ] = 40 ,
3131 frame_size : Optional [Tuple [int , int ]] = (1920 , 1080 ),
3232 ) -> None :
3333 """
34- Initialize the KSPTracker and its solver.
34+ Initialize the KSPTracker and its underlying solver with region and cost configuration .
3535
3636 Args:
37- path_overlap_penalty (Optional[int]): Penalty for reusing the same edge
38- (detection pairing) in multiple tracks. Increasing this value encourages
39- the tracker to produce more distinct, non-overlapping tracks by
40- discouraging shared detections between tracks.
41-
42- iou_weight (Optional[int]): Weight for the Intersection-over-Union (IoU)
43- penalty in the edge cost. Higher values make the tracker favor linking
44- detections with greater spatial overlap, which helps maintain track
45- continuity for objects that move smoothly.
46-
47- dist_weight (Optional[int]): Weight for the Euclidean distance between
48- detection centers in the edge cost. Increasing this value penalizes
49- large jumps between detections in consecutive frames, promoting
50- smoother, more physically plausible tracks.
51-
52- size_weight (Optional[int]): Weight for the size difference penalty in the
53- edge cost. Higher values penalize linking detections with significantly
54- different bounding box areas, which helps prevent identity switches when
55- object size changes abruptly.
56-
57- conf_weight (Optional[int]): Weight for the confidence penalty in the edge
58- cost. Higher values penalize edges between detections with lower
59- confidence scores, making the tracker prefer more reliable detections
60- and reducing the impact of false positives.
61-
62- entry_exit_regions (Optional[List[Tuple[int, int, int, int]]]): List of rectangular entry/exit regions.
63- use_border (Optional[bool]): Enable/disable border-based entry/exit.
64- borders (Optional[Set[str]]): Set of borders to use. {"left", "right", "top", "bottom"}
65- border_margin (Optional[int]): Border thickness in pixels.
66- frame_size (Optional[Tuple[int, int]]): Size of the image (width, height).
37+ path_overlap_penalty (Optional[int]): Penalty for reusing the same edge (detection pairing) in multiple tracks.
38+ Higher values encourage the tracker to produce more distinct, non-overlapping tracks by discouraging shared
39+ detections between tracks. Default is 40.
40+ iou_weight (Optional[float]): Weight for the Intersection-over-Union (IoU) penalty in the edge cost.
41+ Higher values make the tracker favor linking detections with greater spatial overlap, which helps maintain
42+ track continuity for objects that move smoothly. Default is 0.9.
43+ dist_weight (Optional[float]): Weight for the Euclidean distance between detection centers in the edge cost.
44+ Increasing this value penalizes large jumps between detections in consecutive frames, promoting smoother,
45+ more physically plausible tracks. Default is 0.1.
46+ size_weight (Optional[float]): Weight for the size difference penalty in the edge cost.
47+ Higher values penalize linking detections with significantly different bounding box areas, which helps
48+ prevent identity switches when object size changes abruptly. Default is 0.1.
49+ conf_weight (Optional[float]): Weight for the confidence penalty in the edge cost.
50+ Higher values penalize edges between detections with lower confidence scores, making the tracker prefer
51+ more reliable detections and reducing the impact of false positives. Default is 0.1.
52+ entry_exit_regions (Optional[List[Tuple[int, int, int, int]]]): List of rectangular entry/exit regions,
53+ each defined as (x1, y1, x2, y2) in pixel coordinates. These regions are used to determine when objects
54+ enter or exit the scene. Default is an empty list.
55+ use_border (Optional[bool]): Whether to enable border-based entry/exit logic. If True, objects entering or
56+ exiting through the image borders (as defined by `borders` and `border_margin`) are considered for
57+ entry/exit events. Default is True.
58+ borders (Optional[Set[str]]): Set of border sides to use for entry/exit logic. Valid values are any subset
59+ of {"left", "right", "top", "bottom"}. Default is all four borders.
60+ border_margin (Optional[int]): Thickness of the border region (in pixels) used for entry/exit detection.
61+ Default is 40.
62+ frame_size (Optional[Tuple[int, int]]): Size of the image frames as (width, height). Used to determine
63+ border region extents. Default is (1920, 1080).
6764 """
68- self .entry_exit_regions = (
65+ self .entry_exit_regions : List [ Tuple [ int , int , int , int ]] = (
6966 entry_exit_regions if entry_exit_regions is not None else []
7067 )
71- self .use_border = use_border
72- self .borders = (
68+ self .use_border : bool = use_border if use_border is not None else True
69+ self .borders : Set [ str ] = (
7370 borders if borders is not None else {"left" , "right" , "top" , "bottom" }
7471 )
75- self .border_margin = border_margin
76- self .frame_size = frame_size
72+ self .border_margin : int = border_margin if border_margin is not None else 40
73+ self .frame_size : Tuple [ int , int ] = frame_size if frame_size is not None else ( 1920 , 1080 )
7774 self ._solver = KSPSolver (
7875 path_overlap_penalty = path_overlap_penalty ,
7976 iou_weight = iou_weight ,
@@ -113,7 +110,10 @@ def _update(self, detections: sv.Detections) -> sv.Detections:
113110
114111 def set_entry_exit_regions (self , regions : List [Tuple [int , int , int , int ]]) -> None :
115112 """
116- Set rectangular entry/exit zones (x1, y1, x2, y2).
113+ Set rectangular entry/exit zones (x1, y1, x2, y2) and update both the tracker and solver.
114+
115+ Args:
116+ regions (List[Tuple[int, int, int, int]]): List of rectangular regions for entry/exit logic.
117117 """
118118 self .entry_exit_regions = regions
119119 self ._solver .set_entry_exit_regions (regions )
@@ -125,12 +125,19 @@ def set_border_entry_exit(
125125 margin : Optional [int ] = 40 ,
126126 frame_size : Optional [Tuple [int , int ]] = (1920 , 1080 ),
127127 ) -> None :
128- self .use_border = use_border
129- self .borders = (
130- borders if borders is not None else {"left" , "right" , "top" , "bottom" }
131- )
132- self .border_margin = margin
133- self .frame_size = frame_size
128+ """
129+ Configure border-based entry/exit zones and update both the tracker and solver.
130+
131+ Args:
132+ use_border (Optional[bool]): Enable/disable border-based entry/exit.
133+ borders (Optional[Set[str]]): Set of borders to use. {"left", "right", "top", "bottom"}
134+ margin (Optional[int]): Border thickness in pixels.
135+ frame_size (Optional[Tuple[int, int]]): Size of the image (width, height).
136+ """
137+ self .use_border = use_border if use_border is not None else True
138+ self .borders = borders if borders is not None else {"left" , "right" , "top" , "bottom" }
139+ self .border_margin = margin if margin is not None else 40
140+ self .frame_size = frame_size if frame_size is not None else (1920 , 1080 )
134141 self ._solver .set_border_entry_exit (
135142 use_border = self .use_border ,
136143 borders = self .borders ,
0 commit comments