tool

sam3 tracking tool.

Classes

OSNetEmbedder

Minimal wrapper over torchreid.utils.FeatureExtractor for a single OSNet model.

Sam3TrackingTool

class OSNetEmbedder(model_name='osnet_x1_0', model_path=None, device='cpu')[source]

Bases: object

Minimal wrapper over torchreid.utils.FeatureExtractor for a single OSNet model.

embedder = OSNetEmbedder(device=”cpu”) # or “cuda” vec = embedder.embed(frame_bgr, bbox_xyxy, poly=poly) # None if the crop is too poor to trust

Parameters:
  • model_name (str)

  • model_path (str | None)

  • device (str)

embed(frame_bgr, bbox_xyxy, poly=None)[source]

L2-normalized embedding (1D np.ndarray) of the person in bbox_xyxy (and optionally masked by poly) inside frame_bgr, or None if the crop is too small/degenerate to trust.

Return type:

ndarray | None

Parameters:
  • frame_bgr (ndarray)

  • bbox_xyxy (ndarray)

  • poly (ndarray | None)

class Sam3TrackingTool(device='cpu', model_path='facebook/sam3', api_token=None, max_num_objects=None, overwrite=False, verbose=True)[source]

Bases: TrackingTool

Parameters:
  • device (str)

  • model_path (str)

  • api_token (str)

  • max_num_objects (int | None)

  • overwrite (bool)

  • verbose (bool | int)

embedding_similarity(a, b)[source]

0..1 similarity between two L2-normalized embeddings, via cosine similarity rescaled from [-1, 1] to [0, 1] (same 0..1 convention as the module’s other signals, not cosine’s native [-1, 1] convention). None if either embedding is missing.

Return type:

float | None

Parameters:
  • a (ndarray | None)

  • b (ndarray | None)

infer(video_path, mask_dir, text_prompt='people', chunk_size=300, iou_threshold=0.3, id_correction=True, merged_mask_dir=None, merge_report_path=None, id_correction_kwargs=None)[source]

Perform text-based segmentation and tracking from a video file.

Parameters:
  • video_path (Union[str, Path]) – Path to the input video.

  • mask_dir (Union[str, Path]) – Path to the output mask directory.

  • text_prompt (str) – Text description of objects to track.

  • chunk_size (int) – Number of frames to process at once.

  • iou_threshold (float) – IoU threshold for stitching chunks together.

  • id_correction (bool) – Whether to merge the ids of a same object that tracking fragmented, into a second mask directory.

  • merged_mask_dir (Union[str, Path, None]) – Output directory of the id-corrected masks, defaults to <mask_dir>_merged next to mask_dir.

  • merge_report_path (Union[str, Path, None]) – Path of the JSON report of the merge decisions, defaults to <mask_dir>_merge_report.json next to mask_dir.

  • id_correction_kwargs (Optional[Dict[str, Any]]) – Optional overrides of the masks_id_correction parameters (thresholds, use_osnet, …).

masks_id_correction(*, video_path, mask_dir, out_mask_dir, min_fragment_frames=8, merge_threshold=0.6, signature_samples=5, pooled_samples_per_member=5, overlap_pixel_gap_threshold=15.0, overlap_centroid_threshold=115.0, min_overlap_frames=8, overlap_classifier_path=None, device='cpu', use_osnet=True)[source]

Reads mask_dir, merges fragmented identities via appearance (OSNet embedding + hue histogram), writes a NEW MaskDir at out_mask_dir (never touches the original), and returns a JSON-able report of every merge decision made – accepted and, for transparency, every candidate pair actually considered.

Return type:

dict

Parameters:
  • video_path (str)

  • mask_dir (str)

  • out_mask_dir (str)

  • min_fragment_frames (int)

  • merge_threshold (float)

  • signature_samples (int)

  • pooled_samples_per_member (int)

  • overlap_pixel_gap_threshold (float)

  • overlap_centroid_threshold (float)

  • min_overlap_frames (int)

  • overlap_classifier_path (str | None)

  • device (str)

  • use_osnet (bool)

Steps:
  1. Zeroth pass (_resolve_overlap_merges): ids alive at the same time that are really one body split in two parts.

  2. Pass 1 (_resolve_merges): a track that ENDS is matched against tracks that START later, by appearance, with one global Hungarian assignment.

  3. Pass 2 (_resolve_group_merges): start tracks pass 1 left unmatched are compared against whole pass-1 groups.

Every union goes through _group_chains_with_temporal_veto, which refuses a merge that would put two simultaneously-active ids in one group (only zeroth-pass pairs may coexist).

Mask videos are streamed from disk, never loaded whole in RAM.

torchreid_available()[source]

True if torchreid.utils.FeatureExtractor is actually reachable (same check as _resolve_feature_extractor, not just a bare import torchreid, which can succeed even when that attribute isn’t reachable – see that function’s docstring). Doesn’t instantiate a model or touch the GPU. For availability checks only: if OSNet is explicitly requested but unavailable, OSNetEmbedder.__init__ still raises with install instructions rather than silently skipping the signal.

Return type:

bool