Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,73 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
-
import
|
|
|
|
|
|
|
4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
-
from io import BytesIO
|
6 |
|
7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
|
10 |
-
def
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
with gr.Blocks() as demo:
|
27 |
-
gr.Markdown("#
|
28 |
youtube_url = gr.Textbox(label="YouTube URL", value="https://www.youtube.com/watch?v=R5i7aeV8SB8")
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
run_btn.click(
|
34 |
|
35 |
demo.launch()
|
36 |
|
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
+
import yt_dlp
|
4 |
+
import cv2
|
5 |
+
import tempfile
|
6 |
+
import time
|
7 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
8 |
|
9 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
10 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
11 |
|
12 |
+
def get_video_frame(youtube_url, seek_time=0):
|
13 |
+
# Download een stukje van de video (paar seconden), pak een frame rond seek_time
|
14 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4") as temp_video:
|
15 |
+
ydl_opts = {
|
16 |
+
'outtmpl': temp_video.name,
|
17 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
|
18 |
+
'quiet': True,
|
19 |
+
'noplaylist': True,
|
20 |
+
'download_ranges': f"*{seek_time}-{seek_time+1}",
|
21 |
+
'retries': 3,
|
22 |
+
}
|
23 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
24 |
+
try:
|
25 |
+
ydl.download([youtube_url])
|
26 |
+
except Exception as e:
|
27 |
+
return None, f"Download error: {str(e)}"
|
28 |
+
|
29 |
+
# Pak frame
|
30 |
+
vidcap = cv2.VideoCapture(temp_video.name)
|
31 |
+
vidcap.set(cv2.CAP_PROP_POS_MSEC, 500) # Pak frame halverwege het stukje
|
32 |
+
success, image = vidcap.read()
|
33 |
+
vidcap.release()
|
34 |
+
if success:
|
35 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
36 |
+
pil_image = Image.fromarray(image)
|
37 |
+
return pil_image, None
|
38 |
+
else:
|
39 |
+
return None, "Kon geen frame uitlezen."
|
40 |
+
|
41 |
+
def analyse_stream(youtube_url, interval=10, num_frames=3):
|
42 |
+
results = []
|
43 |
+
for i in range(num_frames):
|
44 |
+
seek = i * interval
|
45 |
+
img, err = get_video_frame(youtube_url, seek)
|
46 |
+
if err or img is None:
|
47 |
+
results.append((f"Fout: {err}", None))
|
48 |
+
continue
|
49 |
+
# Caption
|
50 |
+
inputs = processor(images=img, return_tensors="pt")
|
51 |
+
out = model.generate(**inputs)
|
52 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
53 |
+
results.append((caption, img))
|
54 |
+
return results
|
55 |
+
|
56 |
+
def gradio_multi(youtube_url):
|
57 |
+
res = analyse_stream(youtube_url, interval=10, num_frames=3)
|
58 |
+
texts = [r[0] for r in res]
|
59 |
+
imgs = [r[1] for r in res]
|
60 |
+
return texts, imgs
|
61 |
|
62 |
with gr.Blocks() as demo:
|
63 |
+
gr.Markdown("# 🎥 YouTube livestream analyse (meerdere frames)")
|
64 |
youtube_url = gr.Textbox(label="YouTube URL", value="https://www.youtube.com/watch?v=R5i7aeV8SB8")
|
65 |
+
run_btn = gr.Button("Analyseer 3 beelden (om de 10 sec)")
|
66 |
+
output = gr.Dataframe(label="Model antwoorden", headers=["Beschrijving"])
|
67 |
+
images = gr.Gallery(label="Frames")
|
68 |
|
69 |
+
run_btn.click(gradio_multi, inputs=youtube_url, outputs=[output, images])
|
70 |
|
71 |
demo.launch()
|
72 |
|
73 |
+
|