bplusplus 2.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
bplusplus/tracker.py ADDED
@@ -0,0 +1,261 @@
1
+ import numpy as np
2
+ import uuid
3
+ from scipy.optimize import linear_sum_assignment
4
+ from collections import deque
5
+
6
+ class BoundingBox:
7
+ def __init__(self, x, y, width, height, frame_id, track_id=None):
8
+ self.x = x
9
+ self.y = y
10
+ self.width = width
11
+ self.height = height
12
+ self.area = width * height
13
+ self.frame_id = frame_id
14
+ self.track_id = track_id
15
+
16
+ def center(self):
17
+ return (self.x + self.width/2, self.y + self.height/2)
18
+
19
+ @classmethod
20
+ def from_xyxy(cls, x1, y1, x2, y2, frame_id, track_id=None):
21
+ """Create BoundingBox from x1,y1,x2,y2 coordinates"""
22
+ width = x2 - x1
23
+ height = y2 - y1
24
+ return cls(x1, y1, width, height, frame_id, track_id)
25
+
26
+ class InsectTracker:
27
+ def __init__(self, image_height, image_width, max_frames=30, w_dist=0.7, w_area=0.3, cost_threshold=0.8, track_memory_frames=None, debug=False):
28
+ self.image_height = image_height
29
+ self.image_width = image_width
30
+ self.max_dist = np.sqrt(image_height**2 + image_width**2)
31
+ self.max_frames = max_frames
32
+ self.w_dist = w_dist
33
+ self.w_area = w_area
34
+ self.cost_threshold = cost_threshold
35
+ self.debug = debug
36
+
37
+ # If track_memory_frames not specified, use max_frames (full history window)
38
+ self.track_memory_frames = track_memory_frames if track_memory_frames is not None else max_frames
39
+ if self.debug:
40
+ print(f"DEBUG: Tracker initialized with max_frames={max_frames}, track_memory_frames={self.track_memory_frames}")
41
+
42
+ self.tracking_history = deque(maxlen=max_frames)
43
+ self.current_tracks = []
44
+ self.lost_tracks = {} # track_id -> {box: BoundingBox, frames_lost: int}
45
+
46
+ def _generate_track_id(self):
47
+ """Generate a unique UUID for a new track"""
48
+ return str(uuid.uuid4())
49
+
50
+ def calculate_cost(self, box1, box2):
51
+ """Calculate cost between two bounding boxes as per equation (4)"""
52
+ # Calculate center points
53
+ cx1, cy1 = box1.center()
54
+ cx2, cy2 = box2.center()
55
+
56
+ # Euclidean distance (equation 1)
57
+ dist = np.sqrt((cx2 - cx1)**2 + (cy2 - cy1)**2)
58
+
59
+ # Normalized distance (equation 2 used for normalization)
60
+ norm_dist = dist / self.max_dist
61
+
62
+ # Area cost (equation 3)
63
+ min_area = min(box1.area, box2.area)
64
+ max_area = max(box1.area, box2.area)
65
+ area_cost = min_area / max_area if max_area > 0 else 1.0
66
+
67
+ # Final cost (equation 4)
68
+ cost = (norm_dist * self.w_dist) + ((1 - area_cost) * self.w_area)
69
+
70
+ return cost
71
+
72
+ def build_cost_matrix(self, prev_boxes, curr_boxes):
73
+ """Build cost matrix for Hungarian algorithm"""
74
+ n_prev = len(prev_boxes)
75
+ n_curr = len(curr_boxes)
76
+ n = max(n_prev, n_curr)
77
+
78
+ # Initialize cost matrix with high values
79
+ cost_matrix = np.ones((n, n)) * 999.0
80
+
81
+ # Fill in actual costs
82
+ for i in range(n_prev):
83
+ for j in range(n_curr):
84
+ cost_matrix[i, j] = self.calculate_cost(prev_boxes[i], curr_boxes[j])
85
+
86
+ return cost_matrix, n_prev, n_curr
87
+
88
+ def update(self, new_detections, frame_id):
89
+ """
90
+ Update tracking with new detections from YOLO
91
+
92
+ Args:
93
+ new_detections: List of YOLO detection boxes (x1, y1, x2, y2 format)
94
+ frame_id: Current frame number
95
+
96
+ Returns:
97
+ List of track IDs corresponding to each detection
98
+ """
99
+ # Handle empty detection list (no detections in this frame)
100
+ if not new_detections:
101
+ if self.debug:
102
+ print(f"DEBUG: Frame {frame_id} has no detections")
103
+ # Move all current tracks to lost tracks
104
+ for track in self.current_tracks:
105
+ if track.track_id not in self.lost_tracks:
106
+ self.lost_tracks[track.track_id] = {
107
+ 'box': track,
108
+ 'frames_lost': 1
109
+ }
110
+ if self.debug:
111
+ print(f"DEBUG: Moved track {track.track_id} to lost tracks")
112
+ else:
113
+ self.lost_tracks[track.track_id]['frames_lost'] += 1
114
+
115
+ # Age lost tracks and remove old ones
116
+ self._age_lost_tracks()
117
+
118
+ self.current_tracks = []
119
+ self.tracking_history.append([])
120
+ return []
121
+
122
+ # Convert YOLO detections to BoundingBox objects
123
+ new_boxes = []
124
+ for i, detection in enumerate(new_detections):
125
+ x1, y1, x2, y2 = detection[:4]
126
+ bbox = BoundingBox.from_xyxy(x1, y1, x2, y2, frame_id)
127
+ new_boxes.append(bbox)
128
+
129
+ # If this is the first frame or no existing tracks, assign new track IDs to all boxes
130
+ if not self.current_tracks and not self.lost_tracks:
131
+ track_ids = []
132
+ for box in new_boxes:
133
+ box.track_id = self._generate_track_id()
134
+ track_ids.append(box.track_id)
135
+ if self.debug:
136
+ print(f"DEBUG: FIRST FRAME - Assigned track ID {box.track_id} to new detection")
137
+ self.current_tracks = new_boxes
138
+ self.tracking_history.append(new_boxes)
139
+ return track_ids
140
+
141
+ # Combine current tracks and lost tracks for matching
142
+ all_previous_tracks = self.current_tracks.copy()
143
+ lost_track_list = []
144
+
145
+ for track_id, lost_info in self.lost_tracks.items():
146
+ lost_track_list.append(lost_info['box'])
147
+ lost_track_list[-1].track_id = track_id # Ensure track_id is preserved
148
+
149
+ all_previous_tracks.extend(lost_track_list)
150
+
151
+ if not all_previous_tracks:
152
+ # No previous tracks at all, assign new IDs
153
+ track_ids = []
154
+ for box in new_boxes:
155
+ box.track_id = self._generate_track_id()
156
+ track_ids.append(box.track_id)
157
+ if self.debug:
158
+ print(f"DEBUG: No previous tracks - Assigned track ID {box.track_id} to new detection")
159
+ self.current_tracks = new_boxes
160
+ self.tracking_history.append(new_boxes)
161
+ return track_ids
162
+
163
+ # Build cost matrix including lost tracks
164
+ cost_matrix, n_prev, n_curr = self.build_cost_matrix(all_previous_tracks, new_boxes)
165
+
166
+ # Apply Hungarian algorithm
167
+ row_indices, col_indices = linear_sum_assignment(cost_matrix)
168
+
169
+ # Assign track IDs based on the matching
170
+ assigned_curr_indices = set()
171
+ track_ids = [None] * len(new_boxes)
172
+ recovered_tracks = set() # Track IDs that were recovered from lost tracks
173
+
174
+ if self.debug:
175
+ print(f"DEBUG: Hungarian assignment - rows: {row_indices}, cols: {col_indices}")
176
+ print(f"DEBUG: Cost threshold: {self.cost_threshold}")
177
+ print(f"DEBUG: Current tracks: {len(self.current_tracks)}, Lost tracks: {len(self.lost_tracks)}")
178
+
179
+ for i, j in zip(row_indices, col_indices):
180
+ # Only consider valid assignments (not dummy rows/columns)
181
+ if i < n_prev and j < n_curr:
182
+ cost = cost_matrix[i, j]
183
+ if self.debug:
184
+ print(f"DEBUG: Checking assignment {i}->{j}, cost: {cost:.3f}")
185
+ # Check if cost is below threshold
186
+ if cost < self.cost_threshold:
187
+ # Assign the track ID from previous box to current box
188
+ prev_track_id = all_previous_tracks[i].track_id
189
+ new_boxes[j].track_id = prev_track_id
190
+ track_ids[j] = prev_track_id
191
+ assigned_curr_indices.add(j)
192
+
193
+ # Check if this was a lost track being recovered
194
+ if prev_track_id in self.lost_tracks:
195
+ recovered_tracks.add(prev_track_id)
196
+ if self.debug:
197
+ print(f"DEBUG: RECOVERED lost track ID {prev_track_id} for detection {j} (was lost for {self.lost_tracks[prev_track_id]['frames_lost']} frames)")
198
+ else:
199
+ if self.debug:
200
+ print(f"DEBUG: Continued track ID {prev_track_id} for detection {j}")
201
+ else:
202
+ if self.debug:
203
+ print(f"DEBUG: Cost {cost:.3f} above threshold {self.cost_threshold}, not assigning")
204
+
205
+ # Remove recovered tracks from lost tracks
206
+ for track_id in recovered_tracks:
207
+ del self.lost_tracks[track_id]
208
+
209
+ # Assign new track IDs to unassigned current boxes (new insects)
210
+ for j in range(n_curr):
211
+ if j not in assigned_curr_indices:
212
+ new_boxes[j].track_id = self._generate_track_id()
213
+ track_ids[j] = new_boxes[j].track_id
214
+ if self.debug:
215
+ print(f"DEBUG: Assigned NEW track ID {new_boxes[j].track_id} to detection {j}")
216
+
217
+ # Move unmatched current tracks to lost tracks (tracks that disappeared this frame)
218
+ matched_track_ids = {track_ids[j] for j in assigned_curr_indices if track_ids[j] is not None}
219
+ for track in self.current_tracks:
220
+ if track.track_id not in matched_track_ids and track.track_id not in recovered_tracks:
221
+ if track.track_id not in self.lost_tracks:
222
+ self.lost_tracks[track.track_id] = {
223
+ 'box': track,
224
+ 'frames_lost': 1
225
+ }
226
+ if self.debug:
227
+ print(f"DEBUG: Track {track.track_id} disappeared, moved to lost tracks")
228
+
229
+ # Age lost tracks and remove old ones
230
+ self._age_lost_tracks()
231
+
232
+ # Update current tracks
233
+ self.current_tracks = new_boxes
234
+
235
+ # Add to tracking history
236
+ self.tracking_history.append(new_boxes)
237
+
238
+ return track_ids
239
+
240
+ def _age_lost_tracks(self):
241
+ """Age lost tracks and remove those that have been lost too long"""
242
+ tracks_to_remove = []
243
+ for track_id, lost_info in self.lost_tracks.items():
244
+ lost_info['frames_lost'] += 1
245
+ if lost_info['frames_lost'] > self.track_memory_frames:
246
+ tracks_to_remove.append(track_id)
247
+ if self.debug:
248
+ print(f"DEBUG: Permanently removing track {track_id} (lost for {lost_info['frames_lost']} frames)")
249
+
250
+ for track_id in tracks_to_remove:
251
+ del self.lost_tracks[track_id]
252
+
253
+ def get_tracking_stats(self):
254
+ """Get current tracking statistics for debugging/monitoring"""
255
+ return {
256
+ 'active_tracks': len(self.current_tracks),
257
+ 'lost_tracks': len(self.lost_tracks),
258
+ 'active_track_ids': [track.track_id for track in self.current_tracks],
259
+ 'lost_track_ids': list(self.lost_tracks.keys()),
260
+ 'total_history_frames': len(self.tracking_history)
261
+ }