File size: 10,634 Bytes
3cddaf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
Example script showing how to use the SAM2 Video Background Remover API.

This script demonstrates various use cases:
1. Simple single object tracking
2. Multiple object tracking
3. Refined segmentation with background points
4. Batch processing multiple videos
"""

from gradio_client import Client
import json
from pathlib import Path


def example_1_simple_tracking():
    """
    Example 1: Track a single object (e.g., person, ball, car)
    """
    print("=" * 60)
    print("Example 1: Simple Single Object Tracking")
    print("=" * 60)
    
    # Connect to your Space
    client = Client("furbola/chaskick")
    
    # Simple annotation: click on the center of your object in the first frame
    annotations = [
        {
            "frame_idx": 0,      # First frame
            "object_id": 1,      # First object
            "points": [[320, 240]],  # x, y coordinates of the object center
            "labels": [1]        # 1 = this is a foreground point
        }
    ]
    
    # Process the video
    result = client.predict(
        video_file="./input_video.mp4",
        annotations_json=json.dumps(annotations),
        remove_background=True,
        max_frames=None,  # Process all frames
        api_name="/segment_video_api"
    )
    
    print(f"βœ… Output saved to: {result}")


def example_2_multi_object_tracking():
    """
    Example 2: Track multiple objects simultaneously
    Useful for: tracking player + ball, multiple people, etc.
    """
    print("\n" + "=" * 60)
    print("Example 2: Multi-Object Tracking")
    print("=" * 60)
    
    client = Client("furbola/chaskick")
    
    annotations = [
        # Object 1: Player
        {
            "frame_idx": 0,
            "object_id": 1,
            "points": [[320, 240]],
            "labels": [1]
        },
        # Object 2: Ball
        {
            "frame_idx": 0,
            "object_id": 2,
            "points": [[500, 300]],
            "labels": [1]
        },
        # Object 3: Another player
        {
            "frame_idx": 0,
            "object_id": 3,
            "points": [[150, 200]],
            "labels": [1]
        }
    ]
    
    result = client.predict(
        video_file="./soccer_match.mp4",
        annotations_json=json.dumps(annotations),
        remove_background=True,
        max_frames=300,  # Limit to 300 frames for speed
        api_name="/segment_video_api"
    )
    
    print(f"βœ… Tracked 3 objects! Output: {result}")


def example_3_refined_segmentation():
    """
    Example 3: Use both foreground AND background points for better accuracy
    Useful when: object is complex, background is similar color, etc.
    """
    print("\n" + "=" * 60)
    print("Example 3: Refined Segmentation with Negative Points")
    print("=" * 60)
    
    client = Client("furbola/chaskick")
    
    annotations = [
        {
            "frame_idx": 0,
            "object_id": 1,
            "points": [
                [320, 240],  # βœ… Point ON the person's body
                [350, 250],  # βœ… Another point on the person
                [280, 220],  # βœ… Third point for better coverage
                [100, 100],  # ❌ Point on the BACKGROUND to exclude
                [600, 400]   # ❌ Another background point
            ],
            "labels": [
                1,  # foreground
                1,  # foreground
                1,  # foreground
                0,  # background (exclude this area)
                0   # background (exclude this area)
            ]
        }
    ]
    
    result = client.predict(
        video_file="./person_video.mp4",
        annotations_json=json.dumps(annotations),
        remove_background=True,
        max_frames=None,
        api_name="/segment_video_api"
    )
    
    print(f"βœ… Refined segmentation complete: {result}")


def example_4_temporal_annotations():
    """
    Example 4: Add annotations on multiple frames
    Useful when: object changes appearance, camera cuts, occlusions
    """
    print("\n" + "=" * 60)
    print("Example 4: Multi-Frame Annotations")
    print("=" * 60)
    
    client = Client("furbola/chaskick")
    
    annotations = [
        # Annotate frame 0
        {
            "frame_idx": 0,
            "object_id": 1,
            "points": [[320, 240]],
            "labels": [1]
        },
        # Annotate frame 50 (object might have moved or changed)
        {
            "frame_idx": 50,
            "object_id": 1,
            "points": [[450, 300]],
            "labels": [1]
        },
        # Annotate frame 100 (after a camera cut or scene change)
        {
            "frame_idx": 100,
            "object_id": 1,
            "points": [[200, 180]],
            "labels": [1]
        }
    ]
    
    result = client.predict(
        video_file="./long_video.mp4",
        annotations_json=json.dumps(annotations),
        remove_background=True,
        max_frames=None,
        api_name="/segment_video_api"
    )
    
    print(f"βœ… Multi-frame tracking complete: {result}")


def example_5_batch_processing():
    """
    Example 5: Process multiple videos in batch
    """
    print("\n" + "=" * 60)
    print("Example 5: Batch Processing Multiple Videos")
    print("=" * 60)
    
    client = Client("furbola/chaskick")
    
    # List of videos to process
    videos = [
        {"path": "./video1.mp4", "point": [320, 240]},
        {"path": "./video2.mp4", "point": [400, 300]},
        {"path": "./video3.mp4", "point": [250, 200]},
    ]
    
    results = []
    
    for i, video in enumerate(videos, 1):
        print(f"\nProcessing video {i}/{len(videos)}: {video['path']}")
        
        annotations = [{
            "frame_idx": 0,
            "object_id": 1,
            "points": [video['point']],
            "labels": [1]
        }]
        
        try:
            result = client.predict(
                video_file=video['path'],
                annotations_json=json.dumps(annotations),
                remove_background=True,
                max_frames=200,  # Limit frames for faster batch processing
                api_name="/segment_video_api"
            )
            results.append({"input": video['path'], "output": result, "status": "βœ…"})
            print(f"  βœ… Success: {result}")
        except Exception as e:
            results.append({"input": video['path'], "output": None, "status": f"❌ {str(e)}"})
            print(f"  ❌ Failed: {e}")
    
    print("\n" + "=" * 60)
    print("Batch Processing Summary:")
    print("=" * 60)
    for r in results:
        print(f"{r['status']} {r['input']} -> {r['output']}")


def example_6_highlight_mode():
    """
    Example 6: Highlight objects instead of removing background
    Useful for: visualization, debugging, object detection demos
    """
    print("\n" + "=" * 60)
    print("Example 6: Highlight Mode (Keep Background)")
    print("=" * 60)
    
    client = Client("furbola/chaskick")
    
    annotations = [{
        "frame_idx": 0,
        "object_id": 1,
        "points": [[320, 240]],
        "labels": [1]
    }]
    
    result = client.predict(
        video_file="./input_video.mp4",
        annotations_json=json.dumps(annotations),
        remove_background=False,  # Keep background, just highlight the object
        max_frames=None,
        api_name="/segment_video_api"
    )
    
    print(f"βœ… Object highlighted: {result}")


def example_7_find_coordinates():
    """
    Example 7: Helper to find coordinates in a video
    Opens the first frame so you can identify x,y coordinates
    """
    print("\n" + "=" * 60)
    print("Example 7: Find Coordinates Helper")
    print("=" * 60)
    
    import cv2
    
    video_path = "./input_video.mp4"
    
    # Read first frame
    cap = cv2.VideoCapture(video_path)
    ret, frame = cap.read()
    cap.release()
    
    if ret:
        # Save first frame
        cv2.imwrite("first_frame.jpg", frame)
        print(f"βœ… Saved first frame to: first_frame.jpg")
        print(f"   Video size: {frame.shape[1]}x{frame.shape[0]} (width x height)")
        print(f"   Open this image and note the x,y coordinates of your object")
        print(f"   Then use those coordinates in your annotation!")
    else:
        print("❌ Could not read video")


# ============================================================================
# UTILITY FUNCTIONS
# ============================================================================

def create_annotation(frame_idx, object_id, points, labels=None):
    """
    Helper function to create annotation objects.
    
    Args:
        frame_idx: Frame number (0 = first frame)
        object_id: Unique object ID (1, 2, 3, ...)
        points: List of [x, y] coordinates, e.g., [[320, 240]]
        labels: List of labels (1=foreground, 0=background). Defaults to all 1s.
    
    Returns:
        Dictionary with annotation
    """
    if labels is None:
        labels = [1] * len(points)
    
    return {
        "frame_idx": frame_idx,
        "object_id": object_id,
        "points": points,
        "labels": labels
    }


def load_annotations_from_file(json_file):
    """Load annotations from a JSON file."""
    with open(json_file, 'r') as f:
        return json.load(f)


def save_annotations_to_file(annotations, json_file):
    """Save annotations to a JSON file."""
    with open(json_file, 'w') as f:
        json.dump(annotations, f, indent=2)


# ============================================================================
# MAIN
# ============================================================================

if __name__ == "__main__":
    print("""
    ╔════════════════════════════════════════════════════════════╗
    β•‘  SAM2 Video Background Remover - API Examples              β•‘
    β•‘  Choose an example to run or uncomment in the code         β•‘
    β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
    """)
    
    # Uncomment the examples you want to run:
    
    # example_1_simple_tracking()
    # example_2_multi_object_tracking()
    # example_3_refined_segmentation()
    # example_4_temporal_annotations()
    # example_5_batch_processing()
    # example_6_highlight_mode()
    # example_7_find_coordinates()
    
    print("\nβœ… Done! Check the output files.")
    print("\nπŸŽ‰ Your Space: https://huggingface.co/spaces/furbola/chaskick")