Spaces:
Sleeping
Sleeping
Update dino_processor.py
Browse files- dino_processor.py +19 -6
dino_processor.py
CHANGED
|
@@ -111,21 +111,28 @@ def generate_attention_maps(frame_path, model, device, output_dir, frame_name):
|
|
| 111 |
return overlay_path, attn_path
|
| 112 |
|
| 113 |
# --- Main orchestrator function ---
|
| 114 |
-
def process_video_with_dino(video_path
|
| 115 |
"""
|
| 116 |
Main function to process a video and generate DINO attention maps.
|
|
|
|
| 117 |
|
| 118 |
Args:
|
| 119 |
video_path (str): Path to the input video.
|
| 120 |
-
output_dir (str): Directory to save all intermediate and final files.
|
| 121 |
|
| 122 |
Returns:
|
| 123 |
list: A list of tuples, where each tuple contains (overlay_path, attention_map_path).
|
| 124 |
"""
|
| 125 |
-
#
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
| 129 |
|
| 130 |
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 131 |
|
|
@@ -143,7 +150,7 @@ def process_video_with_dino(video_path, output_dir="dino_output"):
|
|
| 143 |
model.load_state_dict(state_dict, strict=True)
|
| 144 |
print("DINO weights loaded successfully from torch.hub.")
|
| 145 |
|
| 146 |
-
# Step 1: Extract frames
|
| 147 |
frame_paths = extract_frames(video_path, output_dir)
|
| 148 |
if not frame_paths:
|
| 149 |
raise ValueError("No frames were extracted from the video.")
|
|
@@ -160,7 +167,13 @@ def process_video_with_dino(video_path, output_dir="dino_output"):
|
|
| 160 |
for frame_name in selected_frames:
|
| 161 |
frame_path = os.path.join(frames_dir, frame_name)
|
| 162 |
frame_name_no_ext = os.path.splitext(frame_name)[0]
|
|
|
|
| 163 |
overlay_path, attn_path = generate_attention_maps(frame_path, model, device, output_dir, frame_name_no_ext)
|
| 164 |
results.append((overlay_path, attn_path))
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
return results
|
|
|
|
| 111 |
return overlay_path, attn_path
|
| 112 |
|
| 113 |
# --- Main orchestrator function ---
|
| 114 |
+
def process_video_with_dino(video_path):
|
| 115 |
"""
|
| 116 |
Main function to process a video and generate DINO attention maps.
|
| 117 |
+
Saves all outputs to a permanent, timestamped folder.
|
| 118 |
|
| 119 |
Args:
|
| 120 |
video_path (str): Path to the input video.
|
|
|
|
| 121 |
|
| 122 |
Returns:
|
| 123 |
list: A list of tuples, where each tuple contains (overlay_path, attention_map_path).
|
| 124 |
"""
|
| 125 |
+
# --- MODIFICATION START ---
|
| 126 |
+
# 1. Define a permanent archive directory.
|
| 127 |
+
archive_dir = "dino_archive"
|
| 128 |
+
os.makedirs(archive_dir, exist_ok=True)
|
| 129 |
+
|
| 130 |
+
# 2. Create a unique, timestamped directory for this specific run.
|
| 131 |
+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 132 |
+
output_dir = os.path.join(archive_dir, timestamp)
|
| 133 |
os.makedirs(output_dir, exist_ok=True)
|
| 134 |
+
print(f"Results for this run will be saved in: {output_dir}")
|
| 135 |
+
# --- MODIFICATION END ---
|
| 136 |
|
| 137 |
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 138 |
|
|
|
|
| 150 |
model.load_state_dict(state_dict, strict=True)
|
| 151 |
print("DINO weights loaded successfully from torch.hub.")
|
| 152 |
|
| 153 |
+
# Step 1: Extract frames (saves into the new unique output_dir)
|
| 154 |
frame_paths = extract_frames(video_path, output_dir)
|
| 155 |
if not frame_paths:
|
| 156 |
raise ValueError("No frames were extracted from the video.")
|
|
|
|
| 167 |
for frame_name in selected_frames:
|
| 168 |
frame_path = os.path.join(frames_dir, frame_name)
|
| 169 |
frame_name_no_ext = os.path.splitext(frame_name)[0]
|
| 170 |
+
# The generated images will now be saved inside the unique timestamped folder
|
| 171 |
overlay_path, attn_path = generate_attention_maps(frame_path, model, device, output_dir, frame_name_no_ext)
|
| 172 |
results.append((overlay_path, attn_path))
|
| 173 |
|
| 174 |
+
# We no longer need the temporary frames, so we can clean them up to save space.
|
| 175 |
+
# The final images (overlays and heatmaps) will remain.
|
| 176 |
+
shutil.rmtree(frames_dir)
|
| 177 |
+
print(f"Cleaned up temporary frames directory: {frames_dir}")
|
| 178 |
+
|
| 179 |
return results
|