Interactive object segmentation and anonymization using SAM, KLT tracking, and optical flow.
Install dependencies:
pip install -r requirements.txtEnsure the SAM checkpoint exists:
model/checkpoints/sam_vit_b_01ec64.pth
- Images: Place files in the
IMG/folder - Videos: Place files in the
VID/folder
Sample files are already included in both folders for immediate testing.
Open main.py and specify your input:
-
For Image mode (line ~1927):
selected_image = "YOUR_IMAGE_FROM_IMG_FOLDER.jpeg"
-
For Video mode (line ~2072):
selected_video = "YOUR_VIDEO_FROM_VID_FOLDER.mp4"
python main.pyChoose between:
- Image – Process a single image
- Video – Process video with frame-by-frame tracking
- A window displays your image (or video's first frame)
- Drag a rectangle around the object to anonymize
- Press ENTER or ESC to confirm
Select how the mask is generated:
-
SAM (Segment Anything Model)
AI-powered segmentation—automatic and accurate -
Shi–Tomasi Corner Detection + Convex Hull
Classical feature-based approach—lightweight
Select how the mask is propagated across frames:
-
Dense Optical Flow (Farnebäck)
Pixel-wise motion warping—handles deformations -
KLT (Kanade–Lucas–Tomasi)
Feature point tracking with global transform—fast and stable
-
Images: Saved to
IMG/with filename pattern:
{original_name}_anonymized_{timestamp}.png -
Videos: Saved to
VID/with filename pattern:
{original_name}_blur_{tracker}_{timestamp}.mp4