Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +12 -5
video_processing.py
CHANGED
|
@@ -59,8 +59,9 @@ def analyze_scenes(video_path, scenes, description):
|
|
| 59 |
highest_prob = 0.0
|
| 60 |
best_scene = None
|
| 61 |
|
| 62 |
-
for start_time, end_time in scenes:
|
| 63 |
frames = extract_frames(video_path, start_time, end_time)
|
|
|
|
| 64 |
for frame in frames:
|
| 65 |
image = Image.fromarray(frame[..., ::-1])
|
| 66 |
inputs = processor(text=description, images=image, return_tensors="pt", padding=True).to(device)
|
|
@@ -68,11 +69,17 @@ def analyze_scenes(video_path, scenes, description):
|
|
| 68 |
outputs = model(**inputs)
|
| 69 |
logits_per_image = outputs.logits_per_image
|
| 70 |
probs = logits_per_image.softmax(dim=1)
|
| 71 |
-
|
| 72 |
-
if max_prob > highest_prob:
|
| 73 |
-
highest_prob = max_prob
|
| 74 |
-
best_scene = (start_time, end_time)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
return best_scene
|
| 77 |
|
| 78 |
def extract_best_scene(video_path, scene):
|
|
|
|
| 59 |
highest_prob = 0.0
|
| 60 |
best_scene = None
|
| 61 |
|
| 62 |
+
for scene_num, (start_time, end_time) in enumerate(scenes):
|
| 63 |
frames = extract_frames(video_path, start_time, end_time)
|
| 64 |
+
scene_prob = 0.0
|
| 65 |
for frame in frames:
|
| 66 |
image = Image.fromarray(frame[..., ::-1])
|
| 67 |
inputs = processor(text=description, images=image, return_tensors="pt", padding=True).to(device)
|
|
|
|
| 69 |
outputs = model(**inputs)
|
| 70 |
logits_per_image = outputs.logits_per_image
|
| 71 |
probs = logits_per_image.softmax(dim=1)
|
| 72 |
+
scene_prob += max(probs[0]).item()
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Average the probabilities over the frames
|
| 75 |
+
scene_prob /= len(frames)
|
| 76 |
+
print(f"Scene {scene_num + 1}: Start={start_time}, End={end_time}, Probability={scene_prob}")
|
| 77 |
+
|
| 78 |
+
if scene_prob > highest_prob:
|
| 79 |
+
highest_prob = scene_prob
|
| 80 |
+
best_scene = (start_time, end_time)
|
| 81 |
+
|
| 82 |
+
print(f"Best Scene: Start={best_scene[0]}, End={best_scene[1]}, Probability={highest_prob}")
|
| 83 |
return best_scene
|
| 84 |
|
| 85 |
def extract_best_scene(video_path, scene):
|