Spaces:
Sleeping
Sleeping
Update video_processing.py
Browse files- video_processing.py +5 -5
video_processing.py
CHANGED
@@ -33,7 +33,7 @@ def sanitize_filename(filename):
|
|
33 |
def find_scenes(video_path):
|
34 |
video_manager = VideoManager([video_path])
|
35 |
scene_manager = SceneManager()
|
36 |
-
scene_manager.add_detector(ContentDetector(threshold=30))
|
37 |
video_manager.set_downscale_factor()
|
38 |
video_manager.start()
|
39 |
scene_manager.detect_scenes(frame_source=video_manager)
|
@@ -51,7 +51,8 @@ def extract_frames(video_path, start_time, end_time):
|
|
51 |
start_seconds = convert_timestamp_to_seconds(start_time)
|
52 |
end_seconds = convert_timestamp_to_seconds(end_time)
|
53 |
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
54 |
-
|
|
|
55 |
frame = video_clip.get_frame(frame_time / video_clip.fps)
|
56 |
frames.append(frame)
|
57 |
return frames
|
@@ -76,9 +77,8 @@ def analyze_scenes(video_path, scenes, description):
|
|
76 |
image_input = processor(images=image, return_tensors="pt").to(device)
|
77 |
with torch.no_grad():
|
78 |
image_features = model.get_image_features(**image_input).detach()
|
79 |
-
logits = (image_features
|
80 |
-
|
81 |
-
scene_prob += probs.max().item()
|
82 |
|
83 |
scene_prob /= len(frames)
|
84 |
print(f"Scene {scene_num + 1}: Start={start_time}, End={end_time}, Probability={scene_prob}")
|
|
|
33 |
def find_scenes(video_path):
|
34 |
video_manager = VideoManager([video_path])
|
35 |
scene_manager = SceneManager()
|
36 |
+
scene_manager.add_detector(ContentDetector(threshold=30)) # Adjusted threshold for finer segmentation
|
37 |
video_manager.set_downscale_factor()
|
38 |
video_manager.start()
|
39 |
scene_manager.detect_scenes(frame_source=video_manager)
|
|
|
51 |
start_seconds = convert_timestamp_to_seconds(start_time)
|
52 |
end_seconds = convert_timestamp_to_seconds(end_time)
|
53 |
video_clip = VideoFileClip(video_path).subclip(start_seconds, end_seconds)
|
54 |
+
# Extract more frames: every frame in the scene
|
55 |
+
for frame_time in range(0, int(video_clip.duration * video_clip.fps)):
|
56 |
frame = video_clip.get_frame(frame_time / video_clip.fps)
|
57 |
frames.append(frame)
|
58 |
return frames
|
|
|
77 |
image_input = processor(images=image, return_tensors="pt").to(device)
|
78 |
with torch.no_grad():
|
79 |
image_features = model.get_image_features(**image_input).detach()
|
80 |
+
logits = torch.cosine_similarity(image_features, text_features).squeeze().item()
|
81 |
+
scene_prob += logits
|
|
|
82 |
|
83 |
scene_prob /= len(frames)
|
84 |
print(f"Scene {scene_num + 1}: Start={start_time}, End={end_time}, Probability={scene_prob}")
|