Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +15 -13
video_processing.py
CHANGED
@@ -15,10 +15,10 @@ def process_video(video_url, description):
|
|
15 |
scenes = detect_scenes(video_path)
|
16 |
|
17 |
# Extract frames and analyze with CLIP model
|
18 |
-
|
19 |
|
20 |
-
#
|
21 |
-
final_clip =
|
22 |
|
23 |
# Ensure the output directory exists
|
24 |
output_dir = "output"
|
@@ -46,7 +46,8 @@ def analyze_scenes(video_path, scenes, description):
|
|
46 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
47 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
48 |
|
49 |
-
|
|
|
50 |
|
51 |
for scene in scenes:
|
52 |
# Extract every 5th frame from the scene
|
@@ -59,12 +60,12 @@ def analyze_scenes(video_path, scenes, description):
|
|
59 |
logits_per_image = outputs.logits_per_image
|
60 |
probs = logits_per_image.softmax(dim=1)
|
61 |
|
62 |
-
|
63 |
-
if
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
return
|
68 |
|
69 |
def extract_frames(video_path, scene):
|
70 |
frames = []
|
@@ -77,9 +78,11 @@ def extract_frames(video_path, scene):
|
|
77 |
|
78 |
return frames
|
79 |
|
80 |
-
def
|
81 |
-
|
82 |
-
|
|
|
|
|
83 |
|
84 |
def download_video(video_url):
|
85 |
ydl_opts = {
|
@@ -93,4 +96,3 @@ def download_video(video_url):
|
|
93 |
video_file = ydl.prepare_filename(info_dict)
|
94 |
|
95 |
return video_file
|
96 |
-
|
|
|
15 |
scenes = detect_scenes(video_path)
|
16 |
|
17 |
# Extract frames and analyze with CLIP model
|
18 |
+
best_scene = analyze_scenes(video_path, scenes, description)
|
19 |
|
20 |
+
# Extract the best scene into a final clip
|
21 |
+
final_clip = extract_best_scene(video_path, best_scene)
|
22 |
|
23 |
# Ensure the output directory exists
|
24 |
output_dir = "output"
|
|
|
46 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
47 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
48 |
|
49 |
+
best_scene = None
|
50 |
+
highest_prob = 0.0
|
51 |
|
52 |
for scene in scenes:
|
53 |
# Extract every 5th frame from the scene
|
|
|
60 |
logits_per_image = outputs.logits_per_image
|
61 |
probs = logits_per_image.softmax(dim=1)
|
62 |
|
63 |
+
max_prob = max(probs[0]).item()
|
64 |
+
if max_prob > highest_prob:
|
65 |
+
highest_prob = max_prob
|
66 |
+
best_scene = scene
|
67 |
|
68 |
+
return best_scene
|
69 |
|
70 |
def extract_frames(video_path, scene):
|
71 |
frames = []
|
|
|
78 |
|
79 |
return frames
|
80 |
|
81 |
+
def extract_best_scene(video_path, scene):
|
82 |
+
start_time = scene[0].get_seconds()
|
83 |
+
end_time = scene[1].get_seconds()
|
84 |
+
video_clip = VideoFileClip(video_path).subclip(start_time, end_time)
|
85 |
+
return video_clip
|
86 |
|
87 |
def download_video(video_url):
|
88 |
ydl_opts = {
|
|
|
96 |
video_file = ydl.prepare_filename(info_dict)
|
97 |
|
98 |
return video_file
|
|