simple-sam2 0.1.0__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.
@@ -0,0 +1,17 @@
1
+ """
2
+ simple-sam2
3
+ ===========
4
+
5
+ A lightweight wrapper around Meta's SAM2 video predictor that makes
6
+ long-video segmentation practical:
7
+
8
+ - **Unified prompt API** — mix points, boxes, and masks in one call.
9
+ - **Batch processing** — only ``batch_size`` frames in GPU memory at once.
10
+ - **Incremental propagation** — segment, inspect, correct, and continue.
11
+
12
+ """
13
+
14
+ from .service import SAM2Service
15
+
16
+ __all__ = ["SAM2Service"]
17
+ __version__ = "0.1.0"
simple_sam2/service.py ADDED
@@ -0,0 +1,720 @@
1
+ import os
2
+ import torch
3
+ import numpy as np
4
+ from pathlib import Path
5
+ from PIL import Image
6
+ from sam2.build_sam import build_sam2_video_predictor
7
+ import shutil
8
+ import cv2
9
+ from typing import Optional, Dict, List, Tuple, Callable
10
+
11
+
12
+ # Default storage root in cwd.
13
+ _DEFAULT_STORAGE = str(Path.cwd() / "simple_sam2_storage")
14
+
15
+
16
+ def _sanitize_label(label: str) -> str:
17
+ """Sanitize label for safe use in filenames."""
18
+ safe = "".join(c if c.isalnum() or c == "-" else "-" for c in label)
19
+ return safe[:50] if safe else "object"
20
+
21
+
22
+ class SAM2Service:
23
+ """
24
+ A wrapper around Meta's SAM2 video predictor that adds:
25
+
26
+ 1. **Unified prompt handler** — accepts points, boxes, and masks together
27
+ in a single call (SAM2 natively only accepts masks OR points+box).
28
+ 2. **Incremental frame segmentation** — segment N frames at a time so the
29
+ user can inspect and correct before continuing (useful at occlusions).
30
+ 3. **Batch processing for long videos** — loads only ``batch_size`` frames
31
+ at a time, keeping GPU/CPU memory usage constant regardless of video
32
+ length.
33
+
34
+ Storage layout
35
+ --------------
36
+ All files are written under ``storage_dir`` (default:
37
+ ``<cwd>/simple_sam2_storage``)::
38
+
39
+ simple_sam2_storage/
40
+ └── <video_name>/
41
+ ├── frames/ # extracted JPEG frames (00000.jpg, …)
42
+ ├── masks/ # output PNGs (00042_1_person.png, …)
43
+ └── tmp_batches/ # transient working dirs (auto-cleaned)
44
+
45
+ Quick start
46
+ -----------
47
+ ::
48
+
49
+ from simple_sam2 import SAM2Service
50
+
51
+ svc = SAM2Service(cfg="sam2.1_hiera_t.yaml", ckpt="sam2.1_hiera_tiny.pt")
52
+
53
+ info = svc.init_video("demo", video_path="clip.mp4")
54
+ print(info["total_frames"], info["frame_dir"], info["masks_dir"])
55
+
56
+ svc.add_prompts("demo", frame_idx=0, obj_id=1, pos_points=[[320, 240]])
57
+
58
+ n = svc.propagate_and_save(
59
+ "demo",
60
+ obj_labels={"1": "person"},
61
+ progress_callback=lambda p: print(f"{p:.1f}%"),
62
+ )
63
+ svc.clear_video("demo")
64
+ """
65
+
66
+ def __init__(
67
+ self,
68
+ cfg: str,
69
+ ckpt: str,
70
+ batch_size: int = 10,
71
+ storage_dir: str = _DEFAULT_STORAGE,
72
+ ):
73
+ """
74
+ Parameters
75
+ ----------
76
+ cfg : str
77
+ Path to a SAM2 YAML config file.
78
+ Configs live in the SAM2 repo under
79
+ ``sam2/configs/sam2.1/``.
80
+ ckpt : str
81
+ Path to a SAM2 checkpoint ``.pt`` file.
82
+ Download from https://github.com/facebookresearch/sam2#model-description
83
+ batch_size : int
84
+ Number of frames loaded into memory at once.
85
+ Reduce if you run out of GPU/CPU memory. Default: 10.
86
+ storage_dir : str
87
+ Root directory for all simple-sam2 data.
88
+ Defaults to ``simple_sam2_storage/`` in the current working
89
+ directory.
90
+ """
91
+ if torch.cuda.is_available():
92
+ self.device = "cuda"
93
+ elif torch.backends.mps.is_available():
94
+ self.device = "mps"
95
+ else:
96
+ self.device = "cpu"
97
+
98
+ self.cfg = cfg
99
+ self.ckpt = ckpt
100
+ self.batch_size = batch_size
101
+ self.storage_dir = storage_dir
102
+ self._predictor = None
103
+
104
+ self.video_metadata: Dict[str, Dict] = {}
105
+ self.current_batch_info: Dict[str, Dict] = {}
106
+ self.prompt_info: Dict[str, Dict] = {}
107
+
108
+ # ── Model loading ────────────────────────────────────────────────────────
109
+
110
+ def _get_predictor(self):
111
+ """Lazily build and cache the SAM2 predictor (weights loaded once)."""
112
+ if self._predictor is None:
113
+ if not os.path.exists(self.ckpt):
114
+ raise FileNotFoundError(
115
+ f"Checkpoint not found: {self.ckpt}\n"
116
+ "Download SAM2 weights from "
117
+ "https://github.com/facebookresearch/sam2#model-description"
118
+ )
119
+ self._predictor = build_sam2_video_predictor(
120
+ config_file=self.cfg,
121
+ ckpt_path=self.ckpt,
122
+ device=self.device,
123
+ vos_optimized=False,
124
+ )
125
+ return self._predictor
126
+
127
+ # ── Mask conversion helpers ──────────────────────────────────────────────
128
+
129
+ @staticmethod
130
+ def _logits_to_uint8(logit_tensor: torch.Tensor) -> np.ndarray:
131
+ """Convert SAM2 logit tensor → uint8 mask (0 or 255)."""
132
+ mask = (logit_tensor > 0).cpu().numpy().astype("uint8") * 255
133
+ return np.squeeze(mask)
134
+
135
+ @staticmethod
136
+ def _logits_to_bool(logit_tensor: torch.Tensor) -> np.ndarray:
137
+ """Convert SAM2 logit tensor → boolean mask."""
138
+ mask = (logit_tensor > 0).cpu().numpy().astype(bool)
139
+ return np.squeeze(mask)
140
+
141
+ # ── Path helpers ─────────────────────────────────────────────────────────
142
+
143
+ def _video_dir(self, video_name: str) -> str:
144
+ return os.path.join(self.storage_dir, video_name)
145
+
146
+ def _frames_dir(self, video_name: str) -> str:
147
+ return os.path.join(self._video_dir(video_name), "frames")
148
+
149
+ def _masks_dir(self, video_name: str) -> str:
150
+ return os.path.join(self._video_dir(video_name), "masks")
151
+
152
+ def _tmp_batches_dir(self, video_name: str) -> str:
153
+ return os.path.join(self._video_dir(video_name), "tmp_batches")
154
+
155
+ # ── Batch / frame helpers ────────────────────────────────────────────────
156
+
157
+ def _get_batch_number(self, frame_idx: int) -> int:
158
+ return frame_idx // self.batch_size
159
+
160
+ def _get_batch_frame_range(self, batch_num: int, total_frames: int) -> Tuple[int, int]:
161
+ start = batch_num * self.batch_size
162
+ end = min(start + self.batch_size, total_frames)
163
+ return start, end
164
+
165
+ def _get_frame_files(self, frame_dir: str) -> List[str]:
166
+ return sorted(
167
+ f for f in os.listdir(frame_dir)
168
+ if f.lower().endswith((".jpg", ".jpeg", ".png"))
169
+ )
170
+
171
+ def _count_frames(self, frame_dir: str) -> int:
172
+ return len(self._get_frame_files(frame_dir))
173
+
174
+ def _create_batch_folder(
175
+ self,
176
+ video_name: str,
177
+ batch_num: int,
178
+ total_frames: int,
179
+ ) -> str:
180
+ """
181
+ Copy the frames for *batch_num* into a temp directory,
182
+ renumbered from 00000 so SAM2 can load them in order.
183
+ """
184
+ batch_dir = os.path.join(
185
+ self._tmp_batches_dir(video_name), f"batch_{batch_num}"
186
+ )
187
+ if os.path.exists(batch_dir):
188
+ shutil.rmtree(batch_dir)
189
+ os.makedirs(batch_dir, exist_ok=True)
190
+
191
+ source_dir = self._frames_dir(video_name)
192
+ start_frame, end_frame = self._get_batch_frame_range(batch_num, total_frames)
193
+ all_frames = self._get_frame_files(source_dir)
194
+
195
+ for i, frame_idx in enumerate(range(start_frame, end_frame)):
196
+ if frame_idx < len(all_frames):
197
+ src = os.path.join(source_dir, all_frames[frame_idx])
198
+ _, ext = os.path.splitext(all_frames[frame_idx])
199
+ dst = os.path.join(batch_dir, f"{i:05d}{ext}")
200
+ if os.path.exists(src):
201
+ shutil.copy(src, dst)
202
+ else:
203
+ print(f"Warning: source frame missing: {src}")
204
+
205
+ return batch_dir
206
+
207
+ # ── Public API ────────────────────────────────────────────────────────────
208
+
209
+ def _extract_frames(self, video_path: str, frame_dir: str) -> int:
210
+ """Extract frames from a video file into *frame_dir* using cv2."""
211
+ os.makedirs(frame_dir, exist_ok=True)
212
+ cap = cv2.VideoCapture(video_path)
213
+ if not cap.isOpened():
214
+ raise RuntimeError(f"Could not open video file: {video_path}")
215
+
216
+ frame_idx = 0
217
+ while True:
218
+ ret, frame = cap.read()
219
+ if not ret:
220
+ break
221
+ out_path = os.path.join(frame_dir, f"{frame_idx:05d}.jpg")
222
+ cv2.imwrite(out_path, frame)
223
+ frame_idx += 1
224
+
225
+ cap.release()
226
+ if frame_idx == 0:
227
+ raise RuntimeError(f"No frames could be extracted from: {video_path}")
228
+
229
+ print(f"Extracted {frame_idx} frames to {frame_dir}")
230
+ return frame_idx
231
+
232
+ def init_video(self, video_name: str, video_path: str) -> Dict:
233
+ """
234
+ Register a video and set up its storage layout.
235
+
236
+ Frames are extracted automatically from *video_path* using cv2.
237
+ No model weights are loaded yet — GPU memory is only touched when
238
+ :meth:`add_prompts` or :meth:`propagate_and_save` is called.
239
+
240
+ Storage layout created::
241
+
242
+ simple_sam2_storage/
243
+ └── <video_name>/
244
+ ├── frames/ ← extracted frames land here
245
+ ├── masks/ ← propagate_and_save() writes here
246
+ └── tmp_batches/ ← transient; auto-managed
247
+
248
+ If frames have already been extracted (e.g. from a previous run),
249
+ extraction is skipped automatically.
250
+
251
+ Parameters
252
+ ----------
253
+ video_name : str
254
+ Unique identifier for this video used in all subsequent calls.
255
+ video_path : str
256
+ Path to the video file (.mp4, .avi, .mov, …).
257
+
258
+ Returns
259
+ -------
260
+ dict
261
+ ``total_frames``, ``total_batches``, ``batch_size``,
262
+ ``frame_dir``, ``masks_dir``.
263
+
264
+ Example
265
+ -------
266
+ ::
267
+
268
+ info = svc.init_video("clip1", video_path="/data/videos/clip1.mp4")
269
+ print(info["total_frames"])
270
+ print(info["masks_dir"])
271
+ """
272
+ video_path = str(video_path)
273
+ if not os.path.exists(video_path):
274
+ raise FileNotFoundError(f"Video file does not exist: {video_path}")
275
+
276
+ # Create the canonical folder layout.
277
+ frame_dir = self._frames_dir(video_name)
278
+ masks_dir = self._masks_dir(video_name)
279
+ tmp_dir = self._tmp_batches_dir(video_name)
280
+ for d in (frame_dir, masks_dir, tmp_dir):
281
+ os.makedirs(d, exist_ok=True)
282
+
283
+ # Extract frames (skip if already done).
284
+ if self._count_frames(frame_dir) > 0:
285
+ print(f"Frames already extracted at {frame_dir}, reusing.")
286
+ total_frames = self._count_frames(frame_dir)
287
+ else:
288
+ print(f"Extracting frames from {video_path} …")
289
+ total_frames = self._extract_frames(video_path, frame_dir)
290
+
291
+ total_batches = (total_frames + self.batch_size - 1) // self.batch_size
292
+
293
+ self.video_metadata[video_name] = {
294
+ "frame_dir": frame_dir,
295
+ "masks_dir": masks_dir,
296
+ "total_frames": total_frames,
297
+ "total_batches": total_batches,
298
+ }
299
+ self.current_batch_info[video_name] = {
300
+ "batch_num": None,
301
+ "state": None,
302
+ "batch_dir": None,
303
+ }
304
+ self.prompt_info[video_name] = {}
305
+
306
+ return {
307
+ "total_frames": total_frames,
308
+ "total_batches": total_batches,
309
+ "batch_size": self.batch_size,
310
+ "frame_dir": frame_dir,
311
+ "masks_dir": masks_dir,
312
+ }
313
+
314
+ def add_prompts(
315
+ self,
316
+ video_name: str,
317
+ frame_idx: int,
318
+ obj_id: int,
319
+ pos_points: Optional[List[List[int]]] = None,
320
+ neg_points: Optional[List[List[int]]] = None,
321
+ box: Optional[List[int]] = None,
322
+ binary_mask: Optional[np.ndarray] = None,
323
+ ) -> np.ndarray:
324
+ """
325
+ Add prompts for an object at a specific frame.
326
+
327
+ Any combination of mask, positive/negative points, and bounding box
328
+ is accepted in a single call. The mask is applied first (SAM2
329
+ requirement), then points and box refine it.
330
+
331
+ Parameters
332
+ ----------
333
+ video_name : str
334
+ Video registered with :meth:`init_video`.
335
+ frame_idx : int
336
+ Global frame index where the object first appears.
337
+ obj_id : int
338
+ Integer ID for this object. Use different IDs for different objects.
339
+ pos_points : list of [x, y], optional
340
+ Positive click points (include the object).
341
+ neg_points : list of [x, y], optional
342
+ Negative click points (exclude these regions).
343
+ box : list of [x1, y1, x2, y2], optional
344
+ Bounding box around the object.
345
+ binary_mask : np.ndarray (H, W), optional
346
+ Boolean or uint8 initial mask.
347
+
348
+ Returns
349
+ -------
350
+ np.ndarray
351
+ uint8 mask (0 or 255) for the object at *frame_idx*.
352
+
353
+ Example
354
+ -------
355
+ ::
356
+
357
+ mask = svc.add_prompts(
358
+ "clip1", frame_idx=0, obj_id=1,
359
+ pos_points=[[320, 240]],
360
+ box=[200, 150, 450, 380],
361
+ )
362
+ """
363
+ if video_name not in self.video_metadata:
364
+ raise RuntimeError(
365
+ f"Video '{video_name}' not initialised. Call init_video() first."
366
+ )
367
+
368
+ state, batch_frame_idx = self._batch_relative_frame_idx(video_name, frame_idx)
369
+ predictor = self._get_predictor()
370
+
371
+ self.prompt_info[video_name][obj_id] = {
372
+ "frame_idx": frame_idx,
373
+ "pos_points": pos_points,
374
+ "neg_points": neg_points,
375
+ "box": box,
376
+ "binary_mask": binary_mask.copy() if binary_mask is not None else None,
377
+ }
378
+
379
+ has_points = bool(
380
+ (pos_points and len(pos_points) > 0)
381
+ or (neg_points and len(neg_points) > 0)
382
+ )
383
+ has_box = box is not None
384
+ has_mask = binary_mask is not None
385
+
386
+ if not has_mask and not has_points and not has_box:
387
+ raise ValueError(
388
+ "Provide at least one of: binary_mask, pos_points/neg_points, or box."
389
+ )
390
+
391
+ with torch.inference_mode():
392
+ if has_mask:
393
+ _, out_obj_ids, out_mask_logits = predictor.add_new_mask(
394
+ inference_state=state,
395
+ frame_idx=batch_frame_idx,
396
+ obj_id=obj_id,
397
+ mask=binary_mask.astype(bool),
398
+ )
399
+
400
+ if has_points or has_box:
401
+ all_points, all_labels = [], []
402
+ if pos_points:
403
+ all_points.extend(pos_points)
404
+ all_labels.extend([1] * len(pos_points))
405
+ if neg_points:
406
+ all_points.extend(neg_points)
407
+ all_labels.extend([0] * len(neg_points))
408
+
409
+ kwargs: Dict = dict(
410
+ inference_state=state,
411
+ frame_idx=batch_frame_idx,
412
+ obj_id=obj_id,
413
+ )
414
+ if all_points:
415
+ kwargs["points"] = np.array(all_points, dtype=np.float32)
416
+ kwargs["labels"] = np.array(all_labels, dtype=np.int32)
417
+ if box:
418
+ kwargs["box"] = np.array(box, dtype=np.float32)
419
+
420
+ _, out_obj_ids, out_mask_logits = predictor.add_new_points_or_box(**kwargs)
421
+
422
+ obj_index = list(out_obj_ids).index(obj_id) if obj_id in out_obj_ids else 0
423
+ return self._logits_to_uint8(out_mask_logits[obj_index])
424
+
425
+ def propagate_and_save(
426
+ self,
427
+ video_name: str,
428
+ out_dir: Optional[str] = None,
429
+ start_frame_idx: int = 0,
430
+ end_frame_idx: Optional[int] = None,
431
+ obj_labels: Optional[Dict[str, str]] = None,
432
+ progress_callback: Optional[Callable[[float], None]] = None,
433
+ ) -> int:
434
+ """
435
+ Propagate all tracked objects through the video and save masks.
436
+
437
+ Uses batch processing so memory usage stays proportional to
438
+ ``batch_size`` regardless of video length. Between batches, the last
439
+ frame's mask is carried over as the initialisation prompt for the next
440
+ batch.
441
+
442
+ Output filenames follow the pattern::
443
+
444
+ <frame_index>_<obj_id>_<label>.png
445
+ # e.g. 00042_1_person.png
446
+
447
+ Parameters
448
+ ----------
449
+ video_name : str
450
+ Video registered with :meth:`init_video`.
451
+ out_dir : str, optional
452
+ Directory where mask PNGs are written. Defaults to
453
+ ``simple_sam2_storage/<video_name>/masks/``.
454
+ start_frame_idx : int
455
+ First global frame to process. Default: 0.
456
+ end_frame_idx : int, optional
457
+ Last global frame (exclusive). Defaults to end of video.
458
+ obj_labels : dict, optional
459
+ ``{str(obj_id): "label"}`` mapping used in output filenames.
460
+ Example: ``{"1": "person", "2": "car"}``.
461
+ progress_callback : callable, optional
462
+ Called with a float in 0–100 after each frame is processed.
463
+
464
+ Returns
465
+ -------
466
+ int
467
+ Total number of mask files saved.
468
+
469
+ Example
470
+ -------
471
+ ::
472
+
473
+ n = svc.propagate_and_save(
474
+ "clip1",
475
+ obj_labels={"1": "person", "2": "car"},
476
+ progress_callback=lambda p: print(f"{p:.1f}%"),
477
+ )
478
+ print(f"Saved {n} masks")
479
+ """
480
+ if video_name not in self.video_metadata:
481
+ raise RuntimeError(
482
+ f"Video '{video_name}' not initialised. Call init_video() first."
483
+ )
484
+ if not self.prompt_info.get(video_name):
485
+ raise RuntimeError(
486
+ "No prompts added. Call add_prompts() before propagate_and_save()."
487
+ )
488
+
489
+ metadata = self.video_metadata[video_name]
490
+ predictor = self._get_predictor()
491
+
492
+ # Default output to the canonical masks/ folder.
493
+ if out_dir is None:
494
+ out_dir = metadata["masks_dir"]
495
+ out_dir = str(out_dir)
496
+ os.makedirs(out_dir, exist_ok=True)
497
+
498
+ obj_labels = obj_labels or {}
499
+
500
+ total_frames = metadata["total_frames"]
501
+ total_batches = metadata["total_batches"]
502
+ end_frame_idx = end_frame_idx if end_frame_idx is not None else total_frames
503
+ total_to_process = max(1, end_frame_idx - start_frame_idx)
504
+ processed_so_far = 0
505
+
506
+ start_batch = self._get_batch_number(start_frame_idx)
507
+ end_batch = self._get_batch_number(min(end_frame_idx - 1, total_frames - 1))
508
+
509
+ saved: int = 0
510
+ last_batch_masks: Dict[int, np.ndarray] = {}
511
+ batch_prompt_frame: int = 0
512
+
513
+ for batch_num in range(start_batch, end_batch + 1):
514
+ print(f"\nProcessing batch {batch_num + 1}/{total_batches}")
515
+ batch_start, batch_end = self._get_batch_frame_range(batch_num, total_frames)
516
+ print(f" Global frames: {batch_start} – {batch_end - 1}")
517
+
518
+ state = self._load_batch(video_name, batch_num)
519
+
520
+ for obj_id, prompt_data in self.prompt_info[video_name].items():
521
+ original_frame = prompt_data["frame_idx"]
522
+ prompt_batch = self._get_batch_number(original_frame)
523
+
524
+ if batch_num == prompt_batch:
525
+ batch_prompt_frame = original_frame - batch_start
526
+ print(f" Obj {obj_id}: original prompts at batch-frame {batch_prompt_frame}")
527
+ self._apply_prompts_to_state(
528
+ state, batch_prompt_frame, obj_id,
529
+ prompt_data["pos_points"],
530
+ prompt_data["neg_points"],
531
+ prompt_data["box"],
532
+ prompt_data["binary_mask"],
533
+ )
534
+
535
+ elif batch_num > prompt_batch:
536
+ if obj_id in last_batch_masks:
537
+ print(f" Obj {obj_id}: carried mask from previous batch → frame 0")
538
+ self._apply_prompts_to_state(
539
+ state, 0, obj_id,
540
+ binary_mask=last_batch_masks[obj_id],
541
+ )
542
+ else:
543
+ print(f" Warning: obj {obj_id} has no carried mask, skipping.")
544
+ continue
545
+
546
+ with torch.inference_mode():
547
+ for out_frame_idx, out_obj_ids, out_mask_logits in predictor.propagate_in_video(
548
+ state,
549
+ start_frame_idx=batch_prompt_frame if batch_num == start_batch else 0,
550
+ ):
551
+ global_frame_idx = batch_start + out_frame_idx
552
+
553
+ if global_frame_idx < start_frame_idx or global_frame_idx >= end_frame_idx:
554
+ continue
555
+
556
+ processed_so_far += 1
557
+ if progress_callback:
558
+ progress_callback(
559
+ min(100.0, processed_so_far / total_to_process * 100.0)
560
+ )
561
+
562
+ for i, out_obj_id in enumerate(out_obj_ids):
563
+ mask_uint8 = self._logits_to_uint8(out_mask_logits[i])
564
+ label = obj_labels.get(str(out_obj_id), f"Object{out_obj_id}")
565
+ fname = (
566
+ f"{global_frame_idx:05d}"
567
+ f"_{out_obj_id}"
568
+ f"_{_sanitize_label(label)}.png"
569
+ )
570
+ Image.fromarray(mask_uint8).save(os.path.join(out_dir, fname))
571
+ saved += 1
572
+
573
+ # Store carry-over mask for the next batch.
574
+ if out_frame_idx == (batch_end - batch_start - 1):
575
+ last_batch_masks[out_obj_id] = self._logits_to_bool(
576
+ out_mask_logits[i]
577
+ )
578
+ print(f" Stored carry-over mask for obj {out_obj_id}")
579
+
580
+ print(f" Running total: {saved} masks saved")
581
+
582
+ if batch_num < end_batch:
583
+ torch.cuda.empty_cache()
584
+
585
+ print(f"\nDone. Saved {saved} masks to: {out_dir}")
586
+ return saved
587
+
588
+ def clear_video(self, video_name: str, delete_storage: bool = False) -> None:
589
+ """
590
+ Free all memory and temp files associated with a video.
591
+
592
+ Parameters
593
+ ----------
594
+ video_name : str
595
+ Video to clear.
596
+ delete_storage : bool
597
+ If ``True``, delete the entire video directory
598
+ (``simple_sam2_storage/<video_name>/``) including frames, masks,
599
+ and tmp_batches. Default: ``False``.
600
+ """
601
+ self.current_batch_info.pop(video_name, {})
602
+
603
+ if delete_storage:
604
+ video_dir = self._video_dir(video_name)
605
+ if os.path.exists(video_dir):
606
+ try:
607
+ shutil.rmtree(video_dir)
608
+ print(f"Deleted storage directory: {video_dir}")
609
+ except Exception as e:
610
+ print(f"Warning: could not remove storage dir {video_dir}: {e}")
611
+ else:
612
+ # Only clean up the transient tmp_batches folder.
613
+ tmp_dir = self._tmp_batches_dir(video_name)
614
+ if os.path.exists(tmp_dir):
615
+ try:
616
+ shutil.rmtree(tmp_dir)
617
+ except Exception as e:
618
+ print(f"Warning: could not remove tmp_batches dir {tmp_dir}: {e}")
619
+
620
+ self.video_metadata.pop(video_name, None)
621
+ self.prompt_info.pop(video_name, None)
622
+
623
+ if torch.cuda.is_available():
624
+ torch.cuda.empty_cache()
625
+
626
+ print(f"Cleared '{video_name}' from memory.")
627
+
628
+ # ── Internal helpers ──────────────────────────────────────────────────────
629
+
630
+ def _load_batch(self, video_name: str, batch_num: int):
631
+ """Load a batch into a fresh SAM2 inference state."""
632
+ metadata = self.video_metadata[video_name]
633
+ predictor = self._get_predictor()
634
+
635
+ batch_dir = self._create_batch_folder(
636
+ video_name, batch_num, metadata["total_frames"]
637
+ )
638
+ batch_frames = self._get_frame_files(batch_dir)
639
+ if not batch_frames:
640
+ raise RuntimeError(f"Batch directory is empty: {batch_dir}")
641
+
642
+ print(f" Loading {len(batch_frames)} frames from {batch_dir}")
643
+
644
+ with torch.inference_mode():
645
+ state = predictor.init_state(video_path=batch_dir)
646
+ predictor.reset_state(state)
647
+
648
+ self.current_batch_info[video_name] = {
649
+ "batch_num": batch_num,
650
+ "state": state,
651
+ "batch_dir": batch_dir,
652
+ }
653
+ return state
654
+
655
+ def _batch_relative_frame_idx(
656
+ self, video_name: str, frame_idx: int
657
+ ) -> Tuple[object, int]:
658
+ """Return (state, batch_local_frame_idx), loading a new batch if needed."""
659
+ batch_num = self._get_batch_number(frame_idx)
660
+ current_info = self.current_batch_info.get(video_name, {})
661
+
662
+ if current_info.get("batch_num") != batch_num or current_info.get("state") is None:
663
+ state = self._load_batch(video_name, batch_num)
664
+ else:
665
+ state = current_info["state"]
666
+
667
+ start_frame, _ = self._get_batch_frame_range(
668
+ batch_num, self.video_metadata[video_name]["total_frames"]
669
+ )
670
+ return state, frame_idx - start_frame
671
+
672
+ def _apply_prompts_to_state(
673
+ self,
674
+ state,
675
+ batch_frame_idx: int,
676
+ obj_id: int,
677
+ pos_points: Optional[List[List[int]]] = None,
678
+ neg_points: Optional[List[List[int]]] = None,
679
+ box: Optional[List[int]] = None,
680
+ binary_mask: Optional[np.ndarray] = None,
681
+ ) -> None:
682
+ """Apply prompts to a batch state (used internally between batches)."""
683
+ predictor = self._get_predictor()
684
+ has_points = bool(
685
+ (pos_points and len(pos_points) > 0)
686
+ or (neg_points and len(neg_points) > 0)
687
+ )
688
+ has_box = box is not None
689
+ has_mask = binary_mask is not None
690
+
691
+ with torch.inference_mode():
692
+ if has_mask:
693
+ predictor.add_new_mask(
694
+ inference_state=state,
695
+ frame_idx=batch_frame_idx,
696
+ obj_id=obj_id,
697
+ mask=binary_mask.astype(bool),
698
+ )
699
+
700
+ if has_points or has_box:
701
+ all_points, all_labels = [], []
702
+ if pos_points:
703
+ all_points.extend(pos_points)
704
+ all_labels.extend([1] * len(pos_points))
705
+ if neg_points:
706
+ all_points.extend(neg_points)
707
+ all_labels.extend([0] * len(neg_points))
708
+
709
+ kwargs: Dict = dict(
710
+ inference_state=state,
711
+ frame_idx=batch_frame_idx,
712
+ obj_id=obj_id,
713
+ )
714
+ if all_points:
715
+ kwargs["points"] = np.array(all_points, dtype=np.float32)
716
+ kwargs["labels"] = np.array(all_labels, dtype=np.int32)
717
+ if box:
718
+ kwargs["box"] = np.array(box, dtype=np.float32)
719
+
720
+ predictor.add_new_points_or_box(**kwargs)
@@ -0,0 +1,376 @@
1
+ Metadata-Version: 2.4
2
+ Name: simple-sam2
3
+ Version: 0.1.0
4
+ Summary: A lightweight wrapper around Meta SAM2 for practical long-video segmentation.
5
+ Author-email: Varun Kolluru <your@email.com>
6
+ License: Apache License
7
+ Version 2.0, January 2004
8
+ http://www.apache.org/licenses/
9
+
10
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
11
+
12
+ 1. Definitions.
13
+
14
+ "License" shall mean the terms and conditions for use, reproduction,
15
+ and distribution as defined by Sections 1 through 9 of this document.
16
+
17
+ "Licensor" shall mean the copyright owner or entity authorized by
18
+ the copyright owner that is granting the License.
19
+
20
+ "Legal Entity" shall mean the union of the acting entity and all
21
+ other entities that control, are controlled by, or are under common
22
+ control with that entity. For the purposes of this definition,
23
+ "control" means (i) the power, direct or indirect, to cause the
24
+ direction or management of such entity, whether by contract or
25
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
26
+ outstanding shares, or (iii) beneficial ownership of such entity.
27
+
28
+ "You" (or "Your") shall mean an individual or Legal Entity
29
+ exercising permissions granted by this License.
30
+
31
+ "Source" form shall mean the preferred form for making modifications,
32
+ including but not limited to software source code, documentation
33
+ source, and configuration files.
34
+
35
+ "Object" form shall mean any form resulting from mechanical
36
+ transformation or translation of a Source form, including but
37
+ not limited to compiled object code, generated documentation,
38
+ and conversions to other media types.
39
+
40
+ "Work" shall mean the work of authorship made available under
41
+ the License, as indicated by a copyright notice that is included in
42
+ or attached to the work (an example is provided in the Appendix below).
43
+
44
+ "Derivative Works" shall mean any work, whether in Source or Object
45
+ form, that is based on (or derived from) the Work and for which the
46
+ editorial revisions, annotations, elaborations, or other modifications
47
+ represent, as a whole, an original work of authorship. For the purposes
48
+ of this License, Derivative Works shall not include works that remain
49
+ separable from, or merely link (or bind by name) to the interfaces of,
50
+ the Work and Derivative Works thereof.
51
+
52
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
53
+ in the Work by the copyright owner or by an individual or Legal Entity
54
+ authorized to submit on behalf of the copyright owner. For the purposes
55
+ of this definition, "submitted" means any form of electronic, verbal,
56
+ or written communication sent to the Licensor or its representatives,
57
+ including but not limited to communication on electronic mailing lists,
58
+ source code control systems, and issue tracking systems that are managed
59
+ by, or on behalf of, the Licensor for the purpose of discussing and
60
+ improving the Work.
61
+
62
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
63
+ whom a Contribution has been received by the Licensor and included
64
+ within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by the combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a cross-claim
83
+ or counterclaim in a lawsuit) alleging that the Work or any
84
+ Contribution embodied within the Work constitutes direct or
85
+ contributory patent infringement, then any patent licenses granted to
86
+ You under this License for that Work shall terminate as of the date
87
+ such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative Works
95
+ a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, You must include a readable copy of the attribution
108
+ notices contained within such NOTICE file, in at least one
109
+ of the following places: within a NOTICE text file distributed
110
+ as part of the Derivative Works; within the Source form or
111
+ documentation, if provided along with the Derivative Works; or,
112
+ within a display generated by the Derivative Works, if and
113
+ wherever such third-party notices normally appear. The contents
114
+ of the NOTICE file are for informational purposes only and
115
+ do not modify the License. You may add Your own attribution
116
+ notices within Derivative Works that You distribute, alongside
117
+ or as an addendum to the NOTICE text from the Work, provided
118
+ that such additional attribution notices cannot be construed
119
+ as modifying the License.
120
+
121
+ You may add Your own license statement for Your modifications and
122
+ may provide additional grant of rights to use, copy, modify, merge,
123
+ publish, distribute, sublicense, and/or sell copies of the
124
+ Contribution, either on an inclusive or exclusive basis, as You
125
+ determine appropriate, for any period, or Your discretion.
126
+
127
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
128
+ any Contribution intentionally submitted for inclusion in the Work
129
+ by You to the Licensor shall be under the terms and conditions of
130
+ this License, without any additional terms or conditions.
131
+ Notwithstanding the above, nothing herein shall supersede or modify
132
+ the terms of any separate license agreement you may have executed
133
+ with Licensor regarding such Contributions.
134
+
135
+ 6. Trademarks. This License does not grant permission to use the trade
136
+ names, trademarks, service marks, or product names of the Licensor,
137
+ except as required for reasonable and customary use in describing the
138
+ origin of the Work and reproducing the content of the NOTICE file.
139
+
140
+ 7. Disclaimer of Warranty. Unless required by applicable law or
141
+ agreed to in writing, Licensor provides the Work (and each
142
+ Contributor provides its Contributions) on an "AS IS" BASIS,
143
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
144
+ implied, including, without limitation, any warranties or conditions
145
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
146
+ PARTICULAR PURPOSE. You are solely responsible for determining the
147
+ appropriateness of using or reproducing the Work and assume any
148
+ risks associated with Your exercise of permissions under this License.
149
+
150
+ 8. Limitation of Liability. In no event and under no legal theory,
151
+ whether in tort (including negligence), contract, or otherwise,
152
+ unless required by applicable law (such as deliberate and grossly
153
+ negligent acts) or agreed to in writing, shall any Contributor be
154
+ liable to You for damages, including any direct, indirect, special,
155
+ incidental, or exemplary damages of any character arising as a
156
+ result of this License or out of the use or inability to use the
157
+ Work (including but not limited to damages for loss of goodwill,
158
+ work stoppage, computer failure or malfunction, or all other
159
+ commercial damages or losses), even if such Contributor has been
160
+ advised of the possibility of such damages.
161
+
162
+ 9. Accepting Warranty or Additional Liability. While redistributing
163
+ the Work or Derivative Works thereof, You may choose to offer,
164
+ and charge a fee for, acceptance of support, warranty, indemnity,
165
+ or other liability obligations and/or rights consistent with this
166
+ License. However, in accepting such obligations, You may act only
167
+ on Your own behalf and on Your sole responsibility, not on behalf
168
+ of any other Contributor, and only if You agree to indemnify,
169
+ defend, and hold each Contributor harmless for any liability
170
+ incurred by, or claims asserted against, such Contributor by reason
171
+ of your accepting any such warranty or additional liability.
172
+
173
+ END OF TERMS AND CONDITIONS
174
+
175
+ Copyright 2026 Varun Kolluru
176
+
177
+ Licensed under the Apache License, Version 2.0 (the "License");
178
+ you may not use this file except in compliance with the License.
179
+ You may obtain a copy of the License at
180
+
181
+ http://www.apache.org/licenses/LICENSE-2.0
182
+
183
+ Unless required by applicable law or agreed to in writing, software
184
+ distributed under the License is distributed on an "AS IS" BASIS,
185
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
186
+ See the License for the specific language governing permissions and
187
+ limitations under the License.
188
+
189
+ Project-URL: Homepage, https://github.com/varun-kolluru/simple-sam2
190
+ Project-URL: Repository, https://github.com/varun-kolluru/simple-sam2
191
+ Project-URL: Bug Tracker, https://github.com/varun-kolluru/simple-sam2/issues
192
+ Keywords: sam2,segmentation,video,computer vision,meta ai
193
+ Classifier: Development Status :: 3 - Alpha
194
+ Classifier: Intended Audience :: Developers
195
+ Classifier: Intended Audience :: Science/Research
196
+ Classifier: License :: OSI Approved :: Apache Software License
197
+ Classifier: Programming Language :: Python :: 3
198
+ Classifier: Programming Language :: Python :: 3.9
199
+ Classifier: Programming Language :: Python :: 3.10
200
+ Classifier: Programming Language :: Python :: 3.11
201
+ Classifier: Programming Language :: Python :: 3.12
202
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
203
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
204
+ Requires-Python: >=3.9
205
+ Description-Content-Type: text/markdown
206
+ License-File: LICENSE
207
+ License-File: NOTICE
208
+ Requires-Dist: torch>=2.0
209
+ Requires-Dist: torchvision
210
+ Requires-Dist: numpy
211
+ Requires-Dist: Pillow
212
+ Requires-Dist: opencv-python>=4.8
213
+ Dynamic: license-file
214
+
215
+ # simple-sam2
216
+
217
+ A lightweight Python wrapper around [Meta SAM2](https://github.com/facebookresearch/sam2) that makes long-video segmentation practical.
218
+
219
+ ## What this adds on top of SAM2
220
+
221
+ | Problem | What simple-sam2 does |
222
+ |---|---|
223
+ | SAM2 only accepts mask OR points+box separately | **Unified prompt API** — pass mask, points, and box together in one call |
224
+ | SAM2 loads the entire video into memory | **Batch processing** — only `batch_size` frames in memory at once |
225
+ | No way to inspect/correct mid-video | **Incremental propagation** — segment N frames, check, then continue |
226
+
227
+ ---
228
+
229
+ ## Installation
230
+
231
+ ```bash
232
+ # 1. Install SAM2 manually first (not on PyPI)
233
+ pip install git+https://github.com/facebookresearch/sam2.git
234
+
235
+ # 2. Then install simple-sam2
236
+ pip install simple-sam2
237
+ ```
238
+
239
+ > **Note:** SAM2 is a dependency which is installed from Meta's GitHub repo. You still need to **download the model weights** separately (see below).
240
+
241
+ ### Download SAM2 weights
242
+
243
+ ```bash
244
+ # Tiny (fastest)
245
+ wget https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_tiny.pt
246
+
247
+ # Small
248
+ wget https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_small.pt
249
+
250
+ # Base+
251
+ wget https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_base_plus.pt
252
+
253
+ # Large (most accurate)
254
+ wget https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_large.pt
255
+ ```
256
+
257
+ The matching config files are bundled with the `sam2` package — pass the config name directly (e.g. `"sam2.1_hiera_t.yaml"`).
258
+
259
+ ---
260
+
261
+ ## Quick start
262
+
263
+ ```python
264
+ from simple_sam2 import SAM2Service
265
+
266
+ CONFIG = "configs/sam2.1/sam2.1_hiera_t.yaml" # or if want to use sam2 then path is :- configs/sam2/sam2_hiera_t.yaml
267
+ WEIGHTS = "path/to/sam2.1_hiera_tiny.pt". # make sure .pt file matches the config
268
+ VIDEO = "path/to/my_video.mp4"
269
+
270
+ #Note:- you only have to download weights (.pt) files, config files are already present at configs/ dir of sam2
271
+
272
+ svc = SAM2Service(cfg=CONFIG, ckpt=WEIGHTS, batch_size=10)
273
+ ```
274
+
275
+ ### 1. Initialise the video
276
+
277
+ ```python
278
+ info = svc.init_video("demo", video_path=VIDEO)
279
+ print(f"Video has {info['total_frames']} frames")
280
+ print(f"Frames extracted to: {info['frame_dir']}")
281
+ print(f"Masks will be saved to: {info['masks_dir']}")
282
+ ```
283
+
284
+ This creates the following layout under `simple_sam2_storage/` in your current directory:
285
+
286
+ ```
287
+ simple_sam2_storage/
288
+ └── demo/
289
+ ├── frames/ ← extracted JPEG frames (00000.jpg, 00001.jpg, …)
290
+ ├── masks/ ← output masks written here
291
+ └── tmp_batches/ ← transient working dirs (auto-managed)
292
+ ```
293
+
294
+ If frames are already extracted from a previous run, extraction is skipped automatically.
295
+
296
+ ### 2. Add prompts
297
+
298
+ ```python
299
+ # Positive click only
300
+ svc.add_prompts("demo", frame_idx=0, obj_id=1, pos_points=[[320, 240]])
301
+
302
+ # Or mix points + bounding box
303
+ svc.add_prompts(
304
+ "demo", frame_idx=0, obj_id=2,
305
+ pos_points=[[400, 300]],
306
+ neg_points=[[100, 100]],
307
+ box=[200, 150, 600, 480],
308
+ )
309
+ ```
310
+
311
+ ### 3. Propagate and save
312
+
313
+ ```python
314
+ n = svc.propagate_and_save(
315
+ "demo",
316
+ start_frame_idx=0,
317
+ end_frame_idx=5, # optional, defaults to end of video
318
+ obj_labels={"1": "person", "2": "car"},
319
+ progress_callback=lambda p: print(f"Progress: {p:.1f}%"),
320
+ )
321
+ print(f"Done! Saved {n} masks to {info['masks_dir']}")
322
+ ```
323
+
324
+ Output filenames follow the pattern `<frame_index>_<obj_id>_<label>.png`:
325
+
326
+ ```
327
+ masks/
328
+ ├── 00000_1_person.png
329
+ ├── 00001_1_person.png
330
+ ├── 00000_2_car.png
331
+ ...
332
+ ```
333
+
334
+ ### 4. Clean up
335
+
336
+ ```python
337
+ svc.clear_video("demo") # free GPU memory
338
+ svc.clear_video("demo", delete_storage= True) # delete all video related frames, tmp_batches, masks
339
+ ```
340
+
341
+ ---
342
+
343
+ ## API reference
344
+
345
+ ### `SAM2Service(cfg, ckpt, batch_size=10, storage_dir=...)`
346
+
347
+ | Parameter | Description |
348
+ |---|---|
349
+ | `cfg` | SAM2 YAML config name concatenated to configs/sam2.1/ for sam2.1 or configs/sam2/ for sam2 (e.g. `"configs/sam2.1/sam2.1_hiera_t.yaml"`) |
350
+ | `ckpt` | Path to SAM2 `.pt` checkpoint file |
351
+ | `batch_size` | Frames in memory at once. Reduce if you run out of GPU memory |
352
+ | `storage_dir` | Root directory for all data. Defaults to `<cwd>/simple_sam2_storage` |
353
+
354
+ ### `init_video(video_name, video_path)`
355
+
356
+ Extracts frames and sets up storage. Returns a dict with `total_frames`, `total_batches`, `batch_size`, `frame_dir`, `masks_dir`.
357
+
358
+ ### `add_prompts(video_name, frame_idx, obj_id, pos_points, neg_points, box, binary_mask)`
359
+
360
+ All prompt types are optional — provide at least one. Returns a uint8 preview mask (0 or 255).
361
+
362
+ ### `propagate_and_save(video_name, out_dir, start_frame_idx, end_frame_idx, obj_labels, progress_callback)`
363
+
364
+ Propagates all tracked objects and saves mask PNGs. `out_dir` defaults to `simple_sam2_storage/<video_name>/masks/`. Returns the number of files saved.
365
+
366
+ ### `clear_video(video_name, delete_storag=False)`
367
+
368
+ Frees GPU memory and removes temp files. Pass `delete_storage= True` to also delete the extracted frames,masks directory.
369
+
370
+ ---
371
+
372
+ ## License
373
+
374
+ Apache 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
375
+
376
+ SAM2 is © Meta Platforms, Inc. and affiliates, also under Apache 2.0.
@@ -0,0 +1,8 @@
1
+ simple_sam2/__init__.py,sha256=vn332po0xmxr6BDTI7u4TxlFYQ2NDniQ9i6KTYkDO1g,444
2
+ simple_sam2/service.py,sha256=vpmy3dJZDG7J7ulc4w-ZzxF4yZTxMErtfc6MiN8EF0I,27623
3
+ simple_sam2-0.1.0.dist-info/licenses/LICENSE,sha256=ftx_OeO8adhbEG42EcfUyU210Qs6nO2ogRBVkIoMg2U,10158
4
+ simple_sam2-0.1.0.dist-info/licenses/NOTICE,sha256=jSAaZTpnNcH_cZ2iJRChIPPlO8XfuxyOGeGn0yi9oXc,490
5
+ simple_sam2-0.1.0.dist-info/METADATA,sha256=vHL6_eD7J2huIt3Evwy5IbundOWK7doljnxuR2ih468,18077
6
+ simple_sam2-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
7
+ simple_sam2-0.1.0.dist-info/top_level.txt,sha256=vGsiJ1TIFnQo4fA3DcQMOJewcPOZ4A-AKhNnjTsif7I,12
8
+ simple_sam2-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,182 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and Derivative Works thereof.
46
+
47
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
48
+ in the Work by the copyright owner or by an individual or Legal Entity
49
+ authorized to submit on behalf of the copyright owner. For the purposes
50
+ of this definition, "submitted" means any form of electronic, verbal,
51
+ or written communication sent to the Licensor or its representatives,
52
+ including but not limited to communication on electronic mailing lists,
53
+ source code control systems, and issue tracking systems that are managed
54
+ by, or on behalf of, the Licensor for the purpose of discussing and
55
+ improving the Work.
56
+
57
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
58
+ whom a Contribution has been received by the Licensor and included
59
+ within the Work.
60
+
61
+ 2. Grant of Copyright License. Subject to the terms and conditions of
62
+ this License, each Contributor hereby grants to You a perpetual,
63
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
64
+ copyright license to reproduce, prepare Derivative Works of,
65
+ publicly display, publicly perform, sublicense, and distribute the
66
+ Work and such Derivative Works in Source or Object form.
67
+
68
+ 3. Grant of Patent License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ (except as stated in this section) patent license to make, have made,
72
+ use, offer to sell, sell, import, and otherwise transfer the Work,
73
+ where such license applies only to those patent claims licensable
74
+ by such Contributor that are necessarily infringed by their
75
+ Contribution(s) alone or by the combination of their Contribution(s)
76
+ with the Work to which such Contribution(s) was submitted. If You
77
+ institute patent litigation against any entity (including a cross-claim
78
+ or counterclaim in a lawsuit) alleging that the Work or any
79
+ Contribution embodied within the Work constitutes direct or
80
+ contributory patent infringement, then any patent licenses granted to
81
+ You under this License for that Work shall terminate as of the date
82
+ such litigation is filed.
83
+
84
+ 4. Redistribution. You may reproduce and distribute copies of the
85
+ Work or Derivative Works thereof in any medium, with or without
86
+ modifications, and in Source or Object form, provided that You
87
+ meet the following conditions:
88
+
89
+ (a) You must give any other recipients of the Work or Derivative Works
90
+ a copy of this License; and
91
+
92
+ (b) You must cause any modified files to carry prominent notices
93
+ stating that You changed the files; and
94
+
95
+ (c) You must retain, in the Source form of any Derivative Works
96
+ that You distribute, all copyright, patent, trademark, and
97
+ attribution notices from the Source form of the Work,
98
+ excluding those notices that do not pertain to any part of
99
+ the Derivative Works; and
100
+
101
+ (d) If the Work includes a "NOTICE" text file as part of its
102
+ distribution, You must include a readable copy of the attribution
103
+ notices contained within such NOTICE file, in at least one
104
+ of the following places: within a NOTICE text file distributed
105
+ as part of the Derivative Works; within the Source form or
106
+ documentation, if provided along with the Derivative Works; or,
107
+ within a display generated by the Derivative Works, if and
108
+ wherever such third-party notices normally appear. The contents
109
+ of the NOTICE file are for informational purposes only and
110
+ do not modify the License. You may add Your own attribution
111
+ notices within Derivative Works that You distribute, alongside
112
+ or as an addendum to the NOTICE text from the Work, provided
113
+ that such additional attribution notices cannot be construed
114
+ as modifying the License.
115
+
116
+ You may add Your own license statement for Your modifications and
117
+ may provide additional grant of rights to use, copy, modify, merge,
118
+ publish, distribute, sublicense, and/or sell copies of the
119
+ Contribution, either on an inclusive or exclusive basis, as You
120
+ determine appropriate, for any period, or Your discretion.
121
+
122
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
123
+ any Contribution intentionally submitted for inclusion in the Work
124
+ by You to the Licensor shall be under the terms and conditions of
125
+ this License, without any additional terms or conditions.
126
+ Notwithstanding the above, nothing herein shall supersede or modify
127
+ the terms of any separate license agreement you may have executed
128
+ with Licensor regarding such Contributions.
129
+
130
+ 6. Trademarks. This License does not grant permission to use the trade
131
+ names, trademarks, service marks, or product names of the Licensor,
132
+ except as required for reasonable and customary use in describing the
133
+ origin of the Work and reproducing the content of the NOTICE file.
134
+
135
+ 7. Disclaimer of Warranty. Unless required by applicable law or
136
+ agreed to in writing, Licensor provides the Work (and each
137
+ Contributor provides its Contributions) on an "AS IS" BASIS,
138
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
139
+ implied, including, without limitation, any warranties or conditions
140
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
141
+ PARTICULAR PURPOSE. You are solely responsible for determining the
142
+ appropriateness of using or reproducing the Work and assume any
143
+ risks associated with Your exercise of permissions under this License.
144
+
145
+ 8. Limitation of Liability. In no event and under no legal theory,
146
+ whether in tort (including negligence), contract, or otherwise,
147
+ unless required by applicable law (such as deliberate and grossly
148
+ negligent acts) or agreed to in writing, shall any Contributor be
149
+ liable to You for damages, including any direct, indirect, special,
150
+ incidental, or exemplary damages of any character arising as a
151
+ result of this License or out of the use or inability to use the
152
+ Work (including but not limited to damages for loss of goodwill,
153
+ work stoppage, computer failure or malfunction, or all other
154
+ commercial damages or losses), even if such Contributor has been
155
+ advised of the possibility of such damages.
156
+
157
+ 9. Accepting Warranty or Additional Liability. While redistributing
158
+ the Work or Derivative Works thereof, You may choose to offer,
159
+ and charge a fee for, acceptance of support, warranty, indemnity,
160
+ or other liability obligations and/or rights consistent with this
161
+ License. However, in accepting such obligations, You may act only
162
+ on Your own behalf and on Your sole responsibility, not on behalf
163
+ of any other Contributor, and only if You agree to indemnify,
164
+ defend, and hold each Contributor harmless for any liability
165
+ incurred by, or claims asserted against, such Contributor by reason
166
+ of your accepting any such warranty or additional liability.
167
+
168
+ END OF TERMS AND CONDITIONS
169
+
170
+ Copyright 2026 Varun Kolluru
171
+
172
+ Licensed under the Apache License, Version 2.0 (the "License");
173
+ you may not use this file except in compliance with the License.
174
+ You may obtain a copy of the License at
175
+
176
+ http://www.apache.org/licenses/LICENSE-2.0
177
+
178
+ Unless required by applicable law or agreed to in writing, software
179
+ distributed under the License is distributed on an "AS IS" BASIS,
180
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
181
+ See the License for the specific language governing permissions and
182
+ limitations under the License.
@@ -0,0 +1,12 @@
1
+ simple-sam2
2
+ Copyright 2026 Varun Kolluru
3
+
4
+ This project is a wrapper around SAM2 (Segment Anything Model 2) by Meta AI Research.
5
+
6
+ SAM2 Repository : https://github.com/facebookresearch/sam2
7
+ SAM2 License : Apache 2.0
8
+ SAM2 Copyright : Copyright (c) Meta Platforms, Inc. and affiliates.
9
+
10
+ In compliance with the Apache 2.0 License, the original SAM2 license and
11
+ copyright notice are reproduced above. This project does not redistribute
12
+ SAM2 source code; it declares SAM2 as a pip dependency.
@@ -0,0 +1 @@
1
+ simple_sam2