video_screen / app.py
AlexCool2024's picture
Update app.py
64fa793 verified
raw
history blame
1.9 kB
import streamlit as st
import numpy as np
import cv2
import requests
import tempfile
# Заголовок приложения
st.title("Video Frame to Image Description")
# Загрузка видеофайла
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
if uploaded_file is not None:
# Создаем временный файл для хранения видео
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(uploaded_file.read())
# Захват видео
cap = cv2.VideoCapture(tfile.name)
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Выбор случайного кадра
random_frame = np.random.randint(length)
cap.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
ret, frame = cap.read()
if ret:
# Отображение выбранного кадра
st.image(frame, channels="BGR", caption=f"Random Frame {random_frame}")
# Конвертация кадра в формат, пригодный для отправки в модель
_, buf = cv2.imencode('.jpg', frame)
files = {'file': ('image.jpg', buf.tobytes(), 'image/jpeg')}
# Отправка изображения в модель
response = requests.post(
"https://hf.space/embed/nttdataspain/Image-To-Text-Lora-ViT/api/predict",
files=files
)
# Получение и отображение результата
if response.status_code == 200:
result = response.json()
description = result['data'][0]['generated_text']
st.success(f"Generated Description: {description}")
else:
st.error("Error: Could not get a response from the model.")
else:
st.error("Error: Could not read a frame from the video.")
cap.release()