Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from moviepy.editor import VideoFileClip
|
| 6 |
+
|
| 7 |
+
def extract_keyframes(video_path, interval=5):
|
| 8 |
+
cap = cv2.VideoCapture(video_path)
|
| 9 |
+
frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
|
| 10 |
+
keyframes = []
|
| 11 |
+
frame_count = 0
|
| 12 |
+
|
| 13 |
+
while cap.isOpened():
|
| 14 |
+
ret, frame = cap.read()
|
| 15 |
+
if not ret:
|
| 16 |
+
break
|
| 17 |
+
if frame_count % (frame_rate * interval) == 0:
|
| 18 |
+
keyframes.append(frame)
|
| 19 |
+
frame_count += 1
|
| 20 |
+
|
| 21 |
+
cap.release()
|
| 22 |
+
return keyframes
|
| 23 |
+
|
| 24 |
+
def generate_captions(frames):
|
| 25 |
+
caption_generator = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 26 |
+
captions = [caption_generator(frame)[0]['generated_text'] for frame in frames]
|
| 27 |
+
return captions
|
| 28 |
+
|
| 29 |
+
def summarize_text(texts):
|
| 30 |
+
summarizer = pipeline("summarization")
|
| 31 |
+
summary = summarizer(" ".join(texts), max_length=50, min_length=10, do_sample=False)
|
| 32 |
+
return summary[0]['summary_text']
|
| 33 |
+
|
| 34 |
+
def summarize_video(video_path):
|
| 35 |
+
frames = extract_keyframes(video_path)
|
| 36 |
+
captions = generate_captions(frames)
|
| 37 |
+
summary = summarize_text(captions)
|
| 38 |
+
return summary
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
video_path = "input.mp4" # Change to your video file
|
| 42 |
+
summary = summarize_video(video_path)
|
| 43 |
+
print("Video Summary:", summary)
|