Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from segment_anything import sam_model_registry, SamPredictor
|
| 6 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 7 |
+
|
| 8 |
+
def video_to_frames(video_path, output_dir, frame_rate=0.7):
|
| 9 |
+
if not os.path.exists(output_dir):
|
| 10 |
+
os.makedirs(output_dir)
|
| 11 |
+
cap = cv2.VideoCapture(video_path)
|
| 12 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 13 |
+
frame_interval = int(fps / frame_rate)
|
| 14 |
+
frame_count = 0
|
| 15 |
+
while True:
|
| 16 |
+
ret, frame = cap.read()
|
| 17 |
+
if not ret:
|
| 18 |
+
break
|
| 19 |
+
if frame_count % frame_interval == 0:
|
| 20 |
+
cv2.imwrite(os.path.join(output_dir, f'frame_{frame_count:05d}.jpg'), frame)
|
| 21 |
+
frame_count += 1
|
| 22 |
+
cap.release()
|
| 23 |
+
return fps
|
| 24 |
+
|
| 25 |
+
def select_background_points(image, num_points=4):
|
| 26 |
+
h, w, _ = image.shape
|
| 27 |
+
points = np.array([
|
| 28 |
+
[0, 0], # top-left corner
|
| 29 |
+
[0, w - 1], # top-right corner
|
| 30 |
+
[h - 1, 0], # bottom-left corner
|
| 31 |
+
[h - 1, w - 1] # bottom-right corner
|
| 32 |
+
])
|
| 33 |
+
|
| 34 |
+
if num_points > 4:
|
| 35 |
+
points = np.vstack([points,
|
| 36 |
+
[0, w // 2],
|
| 37 |
+
[h // 2, 0],
|
| 38 |
+
[h - 1, w // 2],
|
| 39 |
+
[h // 2, w - 1]])
|
| 40 |
+
|
| 41 |
+
return points
|
| 42 |
+
|
| 43 |
+
def compare_histograms(frame1, frame2, threshold=0.4):
|
| 44 |
+
hist1 = cv2.calcHist([frame1], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
|
| 45 |
+
hist2 = cv2.calcHist([frame2], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
|
| 46 |
+
hist1 = cv2.normalize(hist1, hist1).flatten()
|
| 47 |
+
hist2 = cv2.normalize(hist2, hist2).flatten()
|
| 48 |
+
diff = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
|
| 49 |
+
return diff < threshold
|
| 50 |
+
|
| 51 |
+
def detect_scene_changes(frame_dir, fps, threshold=0.15, hist_threshold=0.3):
|
| 52 |
+
frames = sorted(os.listdir(frame_dir))
|
| 53 |
+
scene_changes = []
|
| 54 |
+
prev_mask = None
|
| 55 |
+
prev_frame = None
|
| 56 |
+
|
| 57 |
+
for i, frame_name in enumerate(frames):
|
| 58 |
+
frame = cv2.imread(os.path.join(frame_dir, frame_name))
|
| 59 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 60 |
+
predictor.set_image(frame_rgb)
|
| 61 |
+
background_points = select_background_points(frame_rgb)
|
| 62 |
+
point_labels = np.zeros(background_points.shape[0], dtype=int) # Label points as background (0)
|
| 63 |
+
masks, _, _ = predictor.predict(point_coords=background_points,
|
| 64 |
+
point_labels=point_labels,
|
| 65 |
+
multimask_output=False)
|
| 66 |
+
mask_diff = 0
|
| 67 |
+
if prev_mask is not None:
|
| 68 |
+
mask_diff = np.logical_xor(masks[0], prev_mask).mean()
|
| 69 |
+
hist_diff = False
|
| 70 |
+
if prev_frame is not None:
|
| 71 |
+
hist_diff = compare_histograms(prev_frame, frame, threshold=hist_threshold)
|
| 72 |
+
|
| 73 |
+
if mask_diff > threshold or hist_diff:
|
| 74 |
+
timestamp = int(frame_name.split('_')[1].split('.')[0]) / fps
|
| 75 |
+
scene_changes.append(timestamp)
|
| 76 |
+
|
| 77 |
+
prev_mask = masks[0]
|
| 78 |
+
prev_frame = frame
|
| 79 |
+
|
| 80 |
+
return scene_changes
|
| 81 |
+
|
| 82 |
+
def get_transcript(video_id):
|
| 83 |
+
try:
|
| 84 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 85 |
+
return transcript
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return []
|
| 88 |
+
|
| 89 |
+
def group_transcripts_by_scenes(transcripts, scene_changes):
|
| 90 |
+
grouped_transcripts = []
|
| 91 |
+
scene_index = 0
|
| 92 |
+
current_group = []
|
| 93 |
+
|
| 94 |
+
for transcript in transcripts:
|
| 95 |
+
start_time = transcript['start']
|
| 96 |
+
if scene_index < len(scene_changes) and start_time > scene_changes[scene_index]:
|
| 97 |
+
grouped_transcripts.append(' '.join([t['text'] for t in current_group]))
|
| 98 |
+
current_group = []
|
| 99 |
+
scene_index += 1
|
| 100 |
+
current_group.append(transcript)
|
| 101 |
+
|
| 102 |
+
if current_group:
|
| 103 |
+
grouped_transcripts.append(' '.join([t['text'] for t in current_group]))
|
| 104 |
+
|
| 105 |
+
return grouped_transcripts
|
| 106 |
+
|
| 107 |
+
def process_video_and_transcript(video_file, youtube_video_id):
|
| 108 |
+
output_dir = "frames_output"
|
| 109 |
+
|
| 110 |
+
# Save the uploaded video to a temporary location
|
| 111 |
+
video_path = os.path.join(output_dir, "uploaded_video.mp4")
|
| 112 |
+
with open(video_path, "wb") as f:
|
| 113 |
+
f.write(video_file.read())
|
| 114 |
+
|
| 115 |
+
fps = video_to_frames(video_path, output_dir, frame_rate=0.7)
|
| 116 |
+
|
| 117 |
+
# Initialize the SAM predictor
|
| 118 |
+
model = sam_model_registry["vit_h"](checkpoint="sam_vit_h_4b8939.pth")
|
| 119 |
+
global predictor
|
| 120 |
+
predictor = SamPredictor(model)
|
| 121 |
+
|
| 122 |
+
# Detect scene changes
|
| 123 |
+
scene_changes = detect_scene_changes(output_dir, fps, threshold=0.15, hist_threshold=0.3)
|
| 124 |
+
|
| 125 |
+
# Get YouTube transcript
|
| 126 |
+
transcripts = get_transcript(youtube_video_id)
|
| 127 |
+
|
| 128 |
+
# Group transcripts by scene changes
|
| 129 |
+
grouped_transcripts = group_transcripts_by_scenes(transcripts, scene_changes)
|
| 130 |
+
|
| 131 |
+
return "\n\n".join([f"Scene {i + 1}: {text}" for i, text in enumerate(grouped_transcripts)])
|
| 132 |
+
|
| 133 |
+
# Gradio Interface
|
| 134 |
+
interface = gr.Interface(
|
| 135 |
+
fn=process_video_and_transcript,
|
| 136 |
+
inputs=[
|
| 137 |
+
gr.Video(label="Upload Video File (.mp4)"),
|
| 138 |
+
gr.Textbox(label="YouTube Video ID")
|
| 139 |
+
],
|
| 140 |
+
outputs="text",
|
| 141 |
+
title="Scene Change Detection & Transcript Grouping",
|
| 142 |
+
description="Upload a video file and input a YouTube video ID. The app will detect scene changes in the video and group the transcript text according to these scene changes."
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
interface.launch()
|