swarmsort.track_state module
SwarmSort Track State Management
This module contains the core data structures for managing track states in the SwarmSort tracking system. It includes both active track states and pending detections that are waiting to be confirmed as tracks.
- Classes:
PendingDetection: Detection waiting to become a confirmed track FastTrackState: State representation of an active tracked object
- class swarmsort.track_state.FastTrackState(id, class_id=None, position=<factory>, velocity=<factory>, predicted_position=<factory>, bbox=<factory>, kalman_state=<factory>, last_detection_pos=<factory>, last_detection_frame=0, observation_history=<factory>, observation_frames=<factory>, observation_history_array=<factory>, observation_frames_array=<factory>, kalman_type='simple', velocity_damping=0.95, embedding_frozen=False, last_safe_embedding=None, embedding_history=<factory>, embedding_method='average', embedding_score_history=<factory>, _cached_avg_embedding=None, _cache_valid=False, _cached_representative_embedding=None, _representative_cache_valid=False, avg_embedding=None, embedding_update_count=0, age=0, hits=0, misses=0, confirmed=False, detection_confidence=0.0, confidence_score=0.5, lost_frames=0, recent_match_history=<factory>)[source]
Bases:
objectEnhanced track state with N-embedding history and kalman_type support.
- add_embedding(embedding)[source]
Add new embedding to history with safe normalization.
When the track is frozen (embedding_frozen=True), this method returns immediately without modifying the embedding_history. This protects the appearance model during collisions or crowded areas.
- Parameters:
embedding (
ndarray) – The embedding vector to add (will be L2-normalized)
- add_embedding_fast(embedding, pre_normalized=True)[source]
Fast embedding addition without expensive checks.
- embedding_method: Literal['average', 'best_match', 'weighted_average', 'last', 'median'] = 'average'
- freeze_embeddings()[source]
Freeze embeddings when collision/crowded area detected.
When frozen, add_embedding() will return early without modifying the embedding_history. This protects the track’s appearance model from being corrupted by mixed-object detections during collisions.
The freeze/unfreeze is controlled by core.py’s _update_collision_states() which uses embedding_freeze_density with hysteresis to prevent oscillation.
Note: last_safe_embedding stores a reference embedding from before the collision. This can be useful for debugging or future ReID improvements.
- get_observation_prediction(current_frame, max_history=5)[source]
Get observation-based prediction using recent detection history.
- Return type:
- get_predicted_position(current_frame)[source]
Get predicted position based on kalman_type.
- Return type:
- get_recent_miss_ratio()[source]
Get the ratio of misses in recent frames.
- Return type:
- Returns:
Float in [0, 1] where 0 = all hits, 1 = all misses. Returns 0 if no history yet.
- predict_only(current_frame=None)[source]
Prediction step for UNMATCHED tracks - updates position AND counters.
This is called for tracks that were NOT matched to a detection. For matched tracks, use update_with_detection() instead.
- predict_position(current_frame=None)[source]
Update predicted_position using Kalman filter WITHOUT modifying counters.
This should be called for ALL tracks BEFORE assignment to ensure predicted_position is up-to-date for cost matrix computation.
- set_embedding_params(max_embeddings=5, method='average', score_history_length=5)[source]
Configure embedding storage parameters.
- set_uncertainty_window(window_size)[source]
Set the window size for uncertainty tracking.
- Parameters:
window_size (
int) – Number of recent frames to track (default: 10)
- unfreeze_embeddings()[source]
Unfreeze embeddings when collision area is cleared.
Since add_embedding() returns early when frozen, the embedding_history should still contain only pre-collision embeddings at this point. No restoration is needed - the history is already clean.
Note: We don’t clear last_safe_embedding here in case it’s useful for debugging or comparison purposes.
- update_observation_history(position, frame)[source]
Update observation history for observation-based prediction.
- class swarmsort.track_state.PendingDetection(position, embedding=None, bbox=<factory>, class_id=None, confidence=1.0, first_seen_frame=0, last_seen_frame=0, consecutive_frames=1, total_detections=1, average_position=<factory>)[source]
Bases:
objectRepresents a detection waiting to become a track.