-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
1371 lines (1146 loc) · 53.4 KB
/
Copy pathpipeline.py
File metadata and controls
1371 lines (1146 loc) · 53.4 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pipeline_enhanced_modified.py
# ---------------------------------------------------------
# Enhanced Video Segmentation Pipeline with:
# 1. Left-right layout with larger visualization
# 2. Video processing (extract frames from video)
# 3. Custom categories: player_name and motion_class
# 4. Save to configurable output directory structure
# 5. Centralized annotations.json
# 6. Binary mask export with light pink visualization
# ---------------------------------------------------------
import os
import cv2
import json
import shutil
import tempfile
import traceback
import numpy as np
from typing import List, Dict, Tuple
from datetime import datetime
import colorsys
import gradio as gr
from PIL import Image
import torch
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
# Supervision for professional annotations
import supervision as sv
# ====== SAM2 modules from Grounded-SAM-2 repository ======
from sam2.build_sam import build_sam2_video_predictor, build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
# =========================
# Global Configuration
# =========================
DEFAULT_VIDEO_PATH = ""
DEFAULT_PROMPT = "basketball player."
DEFAULT_PLAYER_NAME = ""
DEFAULT_MOTION_CLASS = ""
IMAGE_EXTS = {".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG"}
# Grounding DINO (HuggingFace)
GDINO_MODEL_ID = "IDEA-Research/grounding-dino-tiny"
# SAM2 weights and configuration
SAM2_CHECKPOINT = "./checkpoints/sam2.1_hiera_large.pt"
SAM2_CFG = "configs/sam2.1/sam2.1_hiera_l.yaml"
DEFAULT_ALPHA = 0.6
DEFAULT_FOURCC = "mp4v"
DEFAULT_RESULTS_DIR = "./Results"
ANNOTATIONS_JSON_PATH = os.path.join(DEFAULT_RESULTS_DIR, "annotations.json")
# Color palette - only light pink for masks
LIGHT_PINK_COLOR = (193, 182, 255) # Light pink color for masks
MOTION_CLASS_OPTIONS = [
"2points shooting",
"3points shooting",
"freethrow",
]
def generate_colors(n):
"""Generate n distinct colors"""
colors = []
for i in range(n):
hue = i / n
rgb = colorsys.hsv_to_rgb(hue, 0.8, 0.9)
colors.append(tuple(int(c * 255) for c in rgb))
return colors
# =========================
# Video Processing Functions
# =========================
def extract_frames_from_video(video_path: str, output_dir: str) -> Tuple[List[str], float]:
"""Extract all frames from video at original fps"""
if not os.path.exists(video_path):
raise FileNotFoundError(f"Video file not found: {video_path}")
os.makedirs(output_dir, exist_ok=True)
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise ValueError(f"Cannot open video: {video_path}")
original_fps = cap.get(cv2.CAP_PROP_FPS)
frame_names = []
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_name = f"{frame_count:05d}.jpg"
frame_path = os.path.join(output_dir, frame_name)
cv2.imwrite(frame_path, frame)
frame_names.append(frame_name)
frame_count += 1
cap.release()
if not frame_names:
raise ValueError("No frames were extracted from the video")
return frame_names, original_fps
def get_video_info(video_path: str) -> Dict:
"""Get video information"""
if not os.path.exists(video_path):
return {}
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return {}
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
duration = frame_count / fps if fps > 0 else 0
cap.release()
return {
"fps": fps,
"frame_count": frame_count,
"width": width,
"height": height,
"duration": duration
}
# =========================
# Supervision Annotators Initialization
# =========================
def get_annotators():
"""Create annotators based on supervision version"""
try:
bbox_annotator = sv.BoxAnnotator(color=sv.Color(0,0,255), thickness=3)
selected_bbox_annotator = sv.BoxAnnotator(color=sv.Color.GREEN, thickness=5)
except AttributeError:
try:
bbox_annotator = sv.BoundingBoxAnnotator(color=sv.Color(0,0,255), thickness=3)
selected_bbox_annotator = sv.BoundingBoxAnnotator(color=sv.Color.GREEN, thickness=5)
except AttributeError:
bbox_annotator = sv.RoundBoxAnnotator(color=sv.Color(0,0,255), thickness=3)
selected_bbox_annotator = sv.RoundBoxAnnotator(color=sv.Color.GREEN, thickness=5)
try:
label_annotator = sv.LabelAnnotator(
color=sv.Color(0,0,255), text_color=sv.Color.WHITE,
text_scale=0.7, text_thickness=2, text_padding=8, border_radius=5
)
selected_label_annotator = sv.LabelAnnotator(
color=sv.Color.GREEN, text_color=sv.Color.WHITE,
text_scale=0.7, text_thickness=2, text_padding=8, border_radius=5
)
except (AttributeError, TypeError):
label_annotator = sv.LabelAnnotator()
selected_label_annotator = sv.LabelAnnotator()
try:
mask_annotator = sv.MaskAnnotator(color=sv.Color.GREEN, opacity=DEFAULT_ALPHA)
except (AttributeError, TypeError):
mask_annotator = sv.MaskAnnotator()
try:
positive_dot_annotator = sv.DotAnnotator(
color=sv.Color.GREEN, radius=10, outline_color=sv.Color.BLACK, outline_thickness=3
)
negative_dot_annotator = sv.DotAnnotator(
color=sv.Color(0,0,255), radius=10, outline_color=sv.Color.BLACK, outline_thickness=3
)
except (AttributeError, TypeError):
try:
positive_dot_annotator = sv.CircleAnnotator(color=sv.Color.GREEN)
negative_dot_annotator = sv.CircleAnnotator(color=sv.Color(0,0,255))
except AttributeError:
positive_dot_annotator = None
negative_dot_annotator = None
return (bbox_annotator, selected_bbox_annotator,
label_annotator, selected_label_annotator,
mask_annotator, positive_dot_annotator, negative_dot_annotator)
# Get annotators
(bbox_annotator, selected_bbox_annotator,
label_annotator, selected_label_annotator,
mask_annotator, positive_dot_annotator, negative_dot_annotator) = get_annotators()
# =========================
# Utility Functions
# =========================
def is_cuda_ok() -> bool:
return torch.cuda.is_available()
def set_torch_perf_flags():
if not is_cuda_ok():
return
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
if torch.cuda.get_device_properties(0).major >= 8:
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
def scan_frame_names(frames_dir: str) -> List[str]:
if not os.path.isdir(frames_dir):
raise FileNotFoundError(f"Directory does not exist: {frames_dir}")
names = [p for p in os.listdir(frames_dir) if os.path.splitext(p)[-1] in IMAGE_EXTS]
if not names:
raise FileNotFoundError(f"No frame images found in directory")
try:
names.sort(key=lambda p: int(os.path.splitext(p)[0]))
except Exception:
names.sort()
return names
def read_image_rgb(path: str) -> np.ndarray:
bgr = cv2.imread(path, cv2.IMREAD_COLOR)
if bgr is None:
raise FileNotFoundError(f"Cannot read image: {path}")
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
return rgb
def create_detections(boxes_xyxy: np.ndarray, labels: List[str], scores: np.ndarray = None) -> sv.Detections:
"""Create supervision Detections object"""
if scores is None:
scores = np.ones(len(boxes_xyxy))
detections = sv.Detections(
xyxy=boxes_xyxy,
confidence=scores,
class_id=np.arange(len(boxes_xyxy))
)
return detections
def annotate_detections_with_labels(image: np.ndarray,
detections: sv.Detections,
labels: List[str],
selected_idx: int = None) -> np.ndarray:
"""Annotate detection results using supervision"""
annotated_image = image.copy()
if selected_idx is not None:
unselected_mask = np.ones(len(detections), dtype=bool)
unselected_mask[selected_idx] = False
if np.any(unselected_mask):
unselected_detections = detections[unselected_mask]
unselected_labels = [f"{i}: {labels[i]}" for i, keep in enumerate(unselected_mask) if keep]
annotated_image = bbox_annotator.annotate(scene=annotated_image, detections=unselected_detections)
annotated_image = label_annotator.annotate(scene=annotated_image, detections=unselected_detections, labels=unselected_labels)
selected_detection = detections[selected_idx:selected_idx+1]
selected_label = [f"{selected_idx}: {labels[selected_idx]}"]
annotated_image = selected_bbox_annotator.annotate(scene=annotated_image, detections=selected_detection)
annotated_image = selected_label_annotator.annotate(scene=annotated_image, detections=selected_detection, labels=selected_label)
else:
formatted_labels = [f"{i}: {label}" for i, label in enumerate(labels)]
annotated_image = bbox_annotator.annotate(scene=annotated_image, detections=detections)
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections, labels=formatted_labels)
return annotated_image
def annotate_colorful_mask_on_image(rgb_img: np.ndarray, mask: np.ndarray, color: Tuple[int, int, int] = None, alpha: float = DEFAULT_ALPHA) -> np.ndarray:
"""Apply colorful mask overlay using supervision's MaskAnnotator"""
H, W = rgb_img.shape[:2]
m = np.array(mask)
m = np.squeeze(m)
if m.ndim == 3 and m.shape[-1] == 1:
m = m[..., 0]
if m.ndim != 2:
raise ValueError(f"Mask must be 2D after squeeze, got shape {m.shape}")
if m.shape != (H, W):
m = cv2.resize(m.astype(np.uint8), (W, H), interpolation=cv2.INTER_NEAREST).astype(bool)
else:
m = m.astype(bool)
if not np.any(m):
return rgb_img.copy()
mask_indices = np.where(m)
y_min, y_max = mask_indices[0].min(), mask_indices[0].max()
x_min, x_max = mask_indices[1].min(), mask_indices[1].max()
bbox = np.array([[x_min, y_min, x_max, y_max]])
mask_3d = m[np.newaxis, ...]
if color is None:
color = LIGHT_PINK_COLOR
try:
custom_mask_annotator = sv.MaskAnnotator(
color=sv.Color(r=color[0], g=color[1], b=color[2]),
opacity=alpha
)
except (AttributeError, TypeError):
custom_mask_annotator = mask_annotator
detections = sv.Detections(
xyxy=bbox,
mask=mask_3d,
confidence=np.array([1.0]),
class_id=np.array([0])
)
annotated_image = custom_mask_annotator.annotate(scene=rgb_img.copy(), detections=detections)
return annotated_image
def annotate_points(rgb_img: np.ndarray, points: List[Tuple[int, int]], labels: List[int]) -> np.ndarray:
"""Annotate points using supervision point annotators, fallback to OpenCV if unavailable"""
if not points or not labels:
return rgb_img.copy()
annotated_image = rgb_img.copy()
if positive_dot_annotator is not None and negative_dot_annotator is not None:
positive_points = []
negative_points = []
for (x, y), lab in zip(points, labels):
if lab == 1:
positive_points.append([x, y])
else:
negative_points.append([x, y])
if positive_points:
pos_points_array = np.array(positive_points)
pos_detections = sv.Detections(
xyxy=np.array([[x-1, y-1, x+1, y+1] for x, y in pos_points_array]),
confidence=np.ones(len(pos_points_array)),
class_id=np.zeros(len(pos_points_array), dtype=int)
)
try:
annotated_image = positive_dot_annotator.annotate(scene=annotated_image, detections=pos_detections)
except (AttributeError, TypeError):
for x, y in pos_points_array:
cv2.circle(annotated_image, (int(x), int(y)), 10, (0, 255, 0), -1, cv2.LINE_AA)
cv2.circle(annotated_image, (int(x), int(y)), 12, (0, 0, 0), 3, cv2.LINE_AA)
if negative_points:
neg_points_array = np.array(negative_points)
neg_detections = sv.Detections(
xyxy=np.array([[x-1, y-1, x+1, y+1] for x, y in neg_points_array]),
confidence=np.ones(len(neg_points_array)),
class_id=np.zeros(len(neg_points_array), dtype=int)
)
try:
annotated_image = negative_dot_annotator.annotate(scene=annotated_image, detections=neg_detections)
except (AttributeError, TypeError):
for x, y in neg_points_array:
cv2.circle(annotated_image, (int(x), int(y)), 10, (0, 0, 255), -1, cv2.LINE_AA)
cv2.circle(annotated_image, (int(x), int(y)), 12, (0, 0, 0), 3, cv2.LINE_AA)
else:
for (x, y), lab in zip(points, labels):
color = (0, 255, 0) if lab == 1 else (0, 0, 255)
cv2.circle(annotated_image, (int(x), int(y)), 10, color, -1, cv2.LINE_AA)
cv2.circle(annotated_image, (int(x), int(y)), 12, (0, 0, 0), 3, cv2.LINE_AA)
return annotated_image
def point_in_box(x, y, box):
x1, y1, x2, y2 = box
return (x >= x1) and (x <= x2) and (y >= y1) and (y <= y2)
def single_mask_to_rle(mask: np.ndarray) -> Dict:
"""Convert single mask to RLE format"""
mask = np.array(mask, dtype=np.uint8, order='F')
if mask.ndim == 3:
mask = mask[:, :, 0]
try:
from pycocotools import mask as mask_utils
rle = mask_utils.encode(mask)
rle['counts'] = rle['counts'].decode('utf-8')
return rle
except ImportError:
pixels = mask.flatten(order='F')
pixels = np.concatenate([[0], pixels, [0]])
runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return {'size': [mask.shape[0], mask.shape[1]], 'counts': runs.tolist()}
def save_annotations_to_json(video_segments: Dict[int, Dict[int, np.ndarray]],
frames_dir: str,
frame_names: List[str],
video_name: str,
player_name: str,
motion_class: str,
fps: float,
output_base_dir: str,
selected_box: np.ndarray = None,
frame_paths_for_json: List[str] = None) -> str:
"""Save annotations in the required JSON format and append to central annotations.json
For the same video, overwrite previous annotations; for different videos, keep them separate"""
print("Start dumping the annotation...")
# Use the provided output base directory for the central annotations.json
annotations_json_path = os.path.join(output_base_dir, "annotations.json")
# Ensure output directory exists
os.makedirs(output_base_dir, exist_ok=True)
# Load existing annotations or create new
existing_annotations = []
if os.path.exists(annotations_json_path):
try:
with open(annotations_json_path, 'r') as f:
loaded_annotations = json.load(f)
if isinstance(loaded_annotations, list):
existing_annotations = loaded_annotations
except Exception as e:
print(f"Warning: Could not load existing annotations: {e}")
existing_annotations = []
# Filter out annotations from the current video and player (to overwrite them)
filtered_annotations = []
if existing_annotations:
for annotation in existing_annotations:
if not (annotation.get("video_name") == video_name and
annotation.get("annotations") and
len(annotation["annotations"]) > 0 and
annotation["annotations"][0].get("player_name") == player_name):
filtered_annotations.append(annotation)
print(f"Removed {len(existing_annotations) - len(filtered_annotations)} existing annotations for video '{video_name}' and player '{player_name}'")
# Process each frame for the current video
new_annotations = []
for frame_idx in sorted(video_segments.keys()):
if frame_idx >= len(frame_names):
continue
# Use the proper saved frame path if provided
if frame_paths_for_json and frame_idx < len(frame_paths_for_json):
frame_path = frame_paths_for_json[frame_idx]
else:
frame_name = frame_names[frame_idx]
frame_path = os.path.join(frames_dir, frame_name)
# Convert absolute path to relative path from output_base_dir
try:
# Get relative path from output_base_dir
relative_frame_path = os.path.relpath(frame_path, output_base_dir)
# Normalize path separators for consistency
relative_frame_path = relative_frame_path.replace(os.path.sep, '/')
except ValueError:
# If relative path calculation fails, use the full path
relative_frame_path = frame_path
# Read image to get dimensions
try:
with Image.open(frame_path) as img:
img_width, img_height = img.size
except Exception:
try:
# Try reading the original frame from temp directory
frame_name = frame_names[frame_idx]
temp_frame_path = os.path.join(frames_dir, frame_name)
img = cv2.imread(temp_frame_path)
if img is None:
continue
img_height, img_width = img.shape[:2]
except:
continue
# Process masks for this frame
segs = video_segments[frame_idx]
if not segs:
continue
# Collect all masks and boxes for this frame
masks = []
boxes = []
scores = []
for obj_id, mask_logits in segs.items():
mask = np.squeeze(mask_logits).astype(np.uint8)
if not np.any(mask):
continue
masks.append(mask)
# Calculate bounding box from mask
y_indices, x_indices = np.where(mask)
if len(y_indices) == 0:
continue
x_min, x_max = float(x_indices.min()), float(x_indices.max())
y_min, y_max = float(y_indices.min()), float(y_indices.max())
boxes.append([x_min, y_min, x_max, y_max])
scores.append(1.0)
if not masks:
continue
# Convert masks to RLE format
mask_rles = [single_mask_to_rle(mask) for mask in masks]
relative_mask_path = relative_frame_path.replace('/frames/', '/masks/').replace('frame_', 'mask_')
frame_annotation = {
"frame_path": relative_frame_path,
"mask_path": relative_mask_path,
"video_name": video_name,
"fps": fps,
"annotations": [
{
"class_name": motion_class.strip() if motion_class else "object",
"player_name": player_name.strip() if player_name else "unknown",
"bbox": box,
"segmentation": mask_rle
}
for box, mask_rle, score in zip(boxes, mask_rles, scores)
],
"box_format": "xyxy",
"img_width": img_width,
"img_height": img_height,
}
new_annotations.append(frame_annotation)
# Combine filtered existing annotations with new annotations
final_annotations = filtered_annotations + new_annotations
# Save updated annotations
with open(annotations_json_path, "w") as f:
json.dump(final_annotations, f, indent=4)
print(f'Added {len(new_annotations)} new annotations for video "{video_name}" and player "{player_name}"')
print(f'Total annotations in file: {len(final_annotations)} (from {len(set([(ann.get("video_name", "unknown"), ann["annotations"][0].get("player_name", "unknown") if ann.get("annotations") else "unknown") for ann in final_annotations]))} video-player combinations)')
print(f'Annotations have been saved to "{annotations_json_path}"')
return annotations_json_path
def export_results(video_segments: Dict[int, Dict[int, np.ndarray]],
frames_dir: str,
frame_names: List[str],
output_base_dir: str,
video_name: str,
player_name: str,
motion_class: str,
fps: float,
selected_box: np.ndarray = None):
"""Export frames and binary masks to organized folder structure under player name"""
# Create directory structure: output_base_dir/player_name/video_name/
player_output_dir = os.path.join(output_base_dir, player_name if player_name.strip() else "unknown_player")
video_output_dir = os.path.join(player_output_dir, video_name)
frames_output_dir = os.path.join(video_output_dir, "frames")
masks_output_dir = os.path.join(video_output_dir, "masks")
os.makedirs(frames_output_dir, exist_ok=True)
os.makedirs(masks_output_dir, exist_ok=True)
# Export frame by frame
exported_count = 0
frame_paths_for_json = []
for frame_idx in sorted(video_segments.keys()):
if frame_idx >= len(frame_names):
continue
frame_name = frame_names[frame_idx]
frame_path = os.path.join(frames_dir, frame_name)
original_rgb = read_image_rgb(frame_path)
segs = video_segments[frame_idx]
obj_ids = sorted(segs.keys())
if not obj_ids:
continue
mask = np.squeeze(segs[obj_ids[0]]).astype(bool)
# Save original frame (without mask overlay)
output_frame_name = f"frame_{frame_idx:05d}.jpg"
output_frame_path = os.path.join(frames_output_dir, output_frame_name)
frame_bgr = cv2.cvtColor(original_rgb, cv2.COLOR_RGB2BGR)
cv2.imwrite(output_frame_path, frame_bgr)
# Save binary mask (255 for mask, 0 for background) as PNG
binary_mask = mask.astype(np.uint8) * 255 # Convert True->255, False->0
output_mask_name = f"mask_{frame_idx:05d}.jpg"
output_mask_path = os.path.join(masks_output_dir, output_mask_name)
cv2.imwrite(output_mask_path, binary_mask)
frame_paths_for_json.append(output_frame_path)
exported_count += 1
# Save annotations to JSON using user-specified output directory
json_path = save_annotations_to_json(
video_segments, frames_output_dir, frame_names, video_name,
player_name, motion_class, fps, output_base_dir, selected_box, frame_paths_for_json
)
return exported_count, json_path
# =========================
# Model Loading
# =========================
_GDINO = None
_GDINO_PROC = None
_SAM2_VIDEO = None
_SAM2_IMG = None
def get_gdino():
global _GDINO, _GDINO_PROC
if _GDINO is None or _GDINO_PROC is None:
device = "cuda" if is_cuda_ok() else "cpu"
_GDINO_PROC = AutoProcessor.from_pretrained(GDINO_MODEL_ID)
_GDINO = AutoModelForZeroShotObjectDetection.from_pretrained(GDINO_MODEL_ID).to(device)
return _GDINO, _GDINO_PROC
def get_sam2_models():
global _SAM2_VIDEO, _SAM2_IMG
if _SAM2_VIDEO is None or _SAM2_IMG is None:
if not os.path.isfile(SAM2_CHECKPOINT):
raise FileNotFoundError(f"SAM2 weights not found: {SAM2_CHECKPOINT}")
try:
vp = build_sam2_video_predictor(SAM2_CFG, SAM2_CHECKPOINT)
im = SAM2ImagePredictor(build_sam2(SAM2_CFG, SAM2_CHECKPOINT))
_SAM2_VIDEO, _SAM2_IMG = vp, im
except Exception as e:
raise RuntimeError(f"SAM2 config loading failed: {SAM2_CFG}\n{e}")
return _SAM2_VIDEO, _SAM2_IMG
def reset_sam2_models():
global _SAM2_VIDEO, _SAM2_IMG
_SAM2_VIDEO = None
_SAM2_IMG = None
# =========================
# Business Logic
# =========================
def process_video_and_detect(video_path: str, text_prompt: str, box_thr: float, text_thr: float):
"""Process video: extract frames and run detection on first frame"""
set_torch_perf_flags()
# Get video info
video_info = get_video_info(video_path)
video_name = os.path.splitext(os.path.basename(video_path))[0]
# Create temporary directory for frames
temp_frames_dir = tempfile.mkdtemp(prefix=f"frames_{video_name}_")
# Extract all frames at original fps
frame_names, original_fps = extract_frames_from_video(video_path, temp_frames_dir)
# Update video info with fps
video_info['original_fps'] = original_fps
# Run detection on first frame
first_path = os.path.join(temp_frames_dir, frame_names[0])
image_pil = Image.open(first_path).convert("RGB")
w, h = image_pil.size
model, proc = get_gdino()
device = next(model.parameters()).device.type
text_prompt = (text_prompt or "").strip()
if not text_prompt.endswith("."):
text_prompt += "."
text_prompt = text_prompt.lower()
inputs = proc(images=image_pil, text=text_prompt, return_tensors="pt").to(device)
with torch.no_grad():
outputs = model(**inputs)
results = proc.post_process_grounded_object_detection(
outputs, inputs.input_ids, threshold=box_thr, text_threshold=text_thr, target_sizes=[(h, w)]
)
boxes = results[0]["boxes"].detach().cpu().numpy().astype(np.float32)
scores = results[0]["scores"].detach().cpu().numpy().astype(np.float32)
labels = results[0]["labels"]
rgb = read_image_rgb(first_path)
detections = create_detections(boxes, labels, scores)
vis = annotate_detections_with_labels(rgb, detections, labels, selected_idx=None)
return vis, boxes, labels, frame_names, temp_frames_dir, video_name, video_info
def initial_propagate_with_selected_box(frames_dir: str, frame_names: List[str],
boxes_xyxy: np.ndarray, labels: List[str], selected_idx: int):
set_torch_perf_flags()
sam2_video, sam2_img = get_sam2_models()
inference_state = sam2_video.init_state(video_path=frames_dir)
first_path = os.path.join(frames_dir, frame_names[0])
first_rgb = read_image_rgb(first_path)
sel_box = boxes_xyxy[selected_idx].astype(np.float32)
sam2_img.set_image(first_rgb)
masks, scores, logits = sam2_img.predict(
point_coords=None, point_labels=None, box=sel_box[None, :], multimask_output=False,
)
mask0 = np.squeeze(masks[0]).astype(bool)
obj_id = 1
_ret, _obj_ids, _mask_logits = sam2_video.add_new_mask(
inference_state=inference_state, frame_idx=0, obj_id=obj_id, mask=mask0
)
video_segments = {}
for out_frame_idx, out_obj_ids, out_mask_logits in sam2_video.propagate_in_video(inference_state):
video_segments[out_frame_idx] = {
out_obj_id: (out_mask_logits[i] > 0.0).cpu().numpy()
for i, out_obj_id in enumerate(out_obj_ids)
}
return inference_state, video_segments
def repropagate_with_points(frames_dir: str, frame_idx: int, points_xy: np.ndarray,
points_labels: np.ndarray, inference_state, obj_id: int = 1):
set_torch_perf_flags()
sam2_video, _ = get_sam2_models()
_ret, _obj_ids, _mask_logits = sam2_video.add_new_points_or_box(
inference_state=inference_state, frame_idx=int(frame_idx), obj_id=int(obj_id),
points=points_xy.astype(np.float32), labels=points_labels.astype(np.int32),
)
video_segments = {}
for out_frame_idx, out_obj_ids, out_mask_logits in sam2_video.propagate_in_video(inference_state):
video_segments[out_frame_idx] = {
out_obj_id: (out_mask_logits[i] > 0.0).cpu().numpy()
for i, out_obj_id in enumerate(out_obj_ids)
}
return video_segments
# =========================
# Gradio Interface
# =========================
def create_custom_css():
return """
.gradio-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 1600px !important;
margin: 0 auto;
}
.main-container {
display: flex;
justify-content: center;
align-items: flex-start;
gap: 20px;
padding: 20px;
}
.left-column {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
border-radius: 15px;
padding: 20px;
width: 45%;
margin: 0 auto;
}
.right-column {
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
border-radius: 15px;
padding: 20px;
width: 55%;
margin: 0 auto;
}
.gr-button {
border-radius: 8px;
font-weight: 600;
transition: all 0.3s ease;
margin: 4px;
display: block;
margin-left: auto;
margin-right: auto;
}
.gr-button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
border: none !important;
color: white !important;
}
.secondary {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
border: none !important;
color: white !important;
}
.gr-textbox, .gr-file {
border-radius: 8px;
border: 2px solid #e1e5e9;
transition: border-color 0.3s ease;
text-align: center;
}
.gr-textbox:focus {
border-color: #667eea;
}
.gr-image {
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
margin: 0 auto;
}
.section-header {
color: #2d3748;
font-size: 1.3em;
font-weight: 700;
margin-bottom: 15px;
text-align: center;
}
.info-card {
background: rgba(255,255,255,0.9);
border-radius: 10px;
padding: 15px;
margin: 10px auto;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
text-align: center;
}
"""
with gr.Blocks(title="NBA Re-id Annotation Pipeline", theme="soft", css=create_custom_css()) as demo:
gr.Markdown(
"""
# NBA Re-id Annotation Pipeline with Custom Categories
""",
elem_classes=["section-header"]
)
with gr.Row():
# Left Column - Configuration and Controls
with gr.Column(scale=1, elem_classes=["left-column"]):
gr.Markdown("## Configuration & Controls", elem_classes=["section-header"])
# Video Input
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Video Input")
video_path_inp = gr.Textbox(
label="Video File Path",
placeholder="Enter path to video file (e.g., /path/to/video.mp4)",
value=""
)
load_video_btn = gr.Button("Load Video Info", variant="secondary")
video_info_display = gr.Textbox(
label="Video Information",
interactive=False,
lines=4
)
# Output Path Configuration
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Output Settings")
output_base_dir_inp = gr.Textbox(
label="Output Base Directory",
value=DEFAULT_RESULTS_DIR,
placeholder="Enter base directory for results"
)
# Detection Settings
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Detection Settings")
text_inp = gr.Textbox(
label="Detection Prompt",
value=DEFAULT_PROMPT,
placeholder="basketball player."
)
with gr.Row():
box_thr = gr.Slider(0.05, 0.9, value=0.25, step=0.05, label="Box Threshold")
text_thr = gr.Slider(0.05, 0.9, value=0.30, step=0.05, label="Text Threshold")
# Custom Categories
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Custom Categories")
player_name_inp = gr.Textbox(
label="Player Name",
value=DEFAULT_PLAYER_NAME,
placeholder="Enter player name"
)
motion_class_inp = gr.Dropdown(
choices=MOTION_CLASS_OPTIONS,
label="Motion Class",
value="shooting",
interactive=True
)
# Processing Settings
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Processing Settings")
alpha_inp = gr.Slider(0.1, 1.0, value=DEFAULT_ALPHA, step=0.05, label="Overlay Opacity")
# Main Control Buttons
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Main Controls")
process_video_btn = gr.Button("Process Video & Detect", variant="primary", size="lg")
run_segmentation_btn = gr.Button("Start Segmentation & Tracking", variant="primary", size="lg")
with gr.Row():
reset_selection_btn = gr.Button("Clear Selection", variant="secondary")
reset_models_btn = gr.Button("Reset Models", variant="secondary")
# # Frame Correction Controls
# with gr.Group(elem_classes=["info-card"]):
# gr.Markdown("### Frame Correction")
# frame_slider = gr.Slider(value=0, minimum=0, maximum=0, step=1, label="Frame Index")
# point_label_radio = gr.Radio(
# choices=[("Positive (Include)", 1), ("Negative (Exclude)", 0)],
# value=1,
# label="Point Type"
# )
# with gr.Row():
# clear_points_btn = gr.Button("Clear Points", variant="secondary")
# apply_points_btn = gr.Button("Apply Points", variant="primary")
# Right Column - Display and Results (Made Larger)
with gr.Column(scale=3, elem_classes=["right-column"]):
gr.Markdown("## Detection & Results", elem_classes=["section-header"])
# Detection Results
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Detection Results")
det_img = gr.Image(
label="First Frame Detection (Click to Select Target)",
interactive=True,
height=700
)
selection_info = gr.Textbox(
label="Selection Status",
interactive=False,
value="No selection yet"
)
# Preview and Correction
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Current Frame Preview")
preview_img = gr.Image(
label="Current Frame with Segmentation",
interactive=False,
height=600
)
# Frame Control and Interactive Correction - 新的集成部分
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Frame Control & Interactive Correction")
# Frame navigation
with gr.Row():
frame_slider = gr.Slider(
value=0,
minimum=0,
maximum=0,
step=1,
label="Frame Index",
scale=3
)
with gr.Column(scale=1):
gr.Markdown("**Point Type**")
point_label_radio = gr.Radio(
choices=[("✓ Include", 1), ("✗ Exclude", 0)],
value=1,
label="",
container=False
)
# Interactive correction image
interact_img = gr.Image(
label="Click to Add Correction Points (Green=Include, Red=Exclude)",
interactive=True,
height=700
)
# Point status and controls
with gr.Row():
pending_info = gr.Textbox(
label="Pending Points Status",
interactive=False,
placeholder="No pending points",
scale=2
)
with gr.Column(scale=1):
clear_points_btn = gr.Button("Clear Points", variant="secondary", size="sm")
apply_points_btn = gr.Button("Apply Points", variant="primary", size="sm")
# Results
with gr.Group(elem_classes=["info-card"]):
gr.Markdown("### Export Results")