Spaces:
Running
Running
File size: 2,909 Bytes
e65e6e5 |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import streamlit as st
import cv2
import random
import numpy as np
import requests
from PIL import Image
# Функция для извлечения случайного кадра из видео
def get_random_frame(video_file):
# Открываем видео файл
cap = cv2.VideoCapture(video_file)
# Получаем общее количество кадров
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Генерируем случайный номер кадра
random_frame_number = random.randint(0, total_frames - 1)
# Устанавливаем указатель на случайный кадр
cap.set(cv2.CAP_PROP_POS_FRAMES, random_frame_number)
success, frame = cap.read()
# Закрываем видеопоток
cap.release()
if success:
return frame
else:
return None
# Заголовок приложения
st.title("Video to Text Converter")
# Загрузка видеофайла
uploaded_file = st.file_uploader("Загрузите видео файл...", type=["mp4", "avi"])
if uploaded_file is not None:
# Извлечение случайного кадра из видео
frame = get_random_frame(uploaded_file)
if frame is not None:
# Отображение извлечённого кадра
st.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), caption='Случайный кадр', use_column_width=True)
# Сохранение временного изображения для отправки в модель
_, buffer = cv2.imencode('.png', frame)
image_data = buffer.tobytes()
# Обработка изображения с помощью модели
if st.button("Преобразовать в текст"):
# Здесь должен быть URL вашей модели
model_url = "https://api-inference.huggingface.co/models/nttdataspain/Image-To-Text-Lora-ViT"
headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"} # Замените на ваш токен
# Отправка запроса к модели
response = requests.post(
model_url,
headers=headers,
files={"file": image_data}
)
if response.status_code == 200:
# Отображаем сгенерированный текст
output_text = response.json().get("generated_text")
st.success(output_text)
else:
st.error("Ошибка при обработке изображения!")
else:
st.error("Не удалось извлечь кадр из видео.") |