Spaces:
Running
Running
File size: 1,488 Bytes
d4c086b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import cv2
import numpy as np
from PIL import Image
import tempfile
import os
def process_image(image) -> str:
"""Basic image description"""
try:
captioner = ModelLoader().load_image_captioner() # singleton-like
result = captioner(image)
return result[0]['generated_text']
except:
return "Image processed. Describe what you see or ask a question about it."
def process_video(video_path) -> str:
"""Extract key frames and describe"""
cap = cv2.VideoCapture(video_path)
frames_desc = []
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
step = max(1, frame_count // 5) # ~5 key frames
for i in range(0, frame_count, step):
cap.set(cv2.CAP_PROP_POS_FRAMES, i)
ret, frame = cap.read()
if ret:
pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
desc = process_image(pil_img)
frames_desc.append(f"Frame {i//step + 1}: {desc}")
cap.release()
return "\n".join(frames_desc) + "\n\nVideo summarized from key frames."
def generate_references(query: str) -> str:
"""Fake realistic references (static or rule-based)"""
refs = [
f"https://en.wikipedia.org/wiki/{query.replace(' ', '_')}",
"https://arxiv.org/search/?query=" + query.replace(" ", "+"),
"https://scholar.google.com/scholar?q=" + query.replace(" ", "+")
]
return "\n".join([f"- [{r.split('//')[1].split('/')[0]}]({r})" for r in refs]) |