Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +11 -4
video_processing.py
CHANGED
@@ -50,8 +50,8 @@ def extract_frames(video_path, start_time, end_time):
|
|
50 |
start_seconds = convert_timestamp_to_seconds(start_time)
|
51 |
end_seconds = convert_timestamp_to_seconds(end_time)
|
52 |
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
53 |
-
for frame_time in range(0, int(video_clip.duration), 5):
|
54 |
-
frame = video_clip.get_frame(frame_time)
|
55 |
frames.append(frame)
|
56 |
return frames
|
57 |
|
@@ -61,6 +61,10 @@ def analyze_scenes(video_path, scenes, description):
|
|
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])
|
@@ -71,7 +75,6 @@ def analyze_scenes(video_path, scenes, description):
|
|
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 |
|
@@ -79,7 +82,11 @@ def analyze_scenes(video_path, scenes, description):
|
|
79 |
highest_prob = scene_prob
|
80 |
best_scene = (start_time, end_time)
|
81 |
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
return best_scene
|
84 |
|
85 |
def extract_best_scene(video_path, scene):
|
|
|
50 |
start_seconds = convert_timestamp_to_seconds(start_time)
|
51 |
end_seconds = convert_timestamp_to_seconds(end_time)
|
52 |
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
53 |
+
for frame_time in range(0, int(video_clip.duration * video_clip.fps), 5):
|
54 |
+
frame = video_clip.get_frame(frame_time / video_clip.fps)
|
55 |
frames.append(frame)
|
56 |
return frames
|
57 |
|
|
|
61 |
|
62 |
for scene_num, (start_time, end_time) in enumerate(scenes):
|
63 |
frames = extract_frames(video_path, start_time, end_time)
|
64 |
+
if not frames:
|
65 |
+
print(f"Scene {scene_num + 1}: Start={start_time}, End={end_time} - No frames extracted")
|
66 |
+
continue
|
67 |
+
|
68 |
scene_prob = 0.0
|
69 |
for frame in frames:
|
70 |
image = Image.fromarray(frame[..., ::-1])
|
|
|
75 |
probs = logits_per_image.softmax(dim=1)
|
76 |
scene_prob += max(probs[0]).item()
|
77 |
|
|
|
78 |
scene_prob /= len(frames)
|
79 |
print(f"Scene {scene_num + 1}: Start={start_time}, End={end_time}, Probability={scene_prob}")
|
80 |
|
|
|
82 |
highest_prob = scene_prob
|
83 |
best_scene = (start_time, end_time)
|
84 |
|
85 |
+
if best_scene:
|
86 |
+
print(f"Best Scene: Start={best_scene[0]}, End={best_scene[1]}, Probability={highest_prob}")
|
87 |
+
else:
|
88 |
+
print("No suitable scene found")
|
89 |
+
|
90 |
return best_scene
|
91 |
|
92 |
def extract_best_scene(video_path, scene):
|