Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,51 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from PIL import Image
|
7 |
-
import os
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
cap = cv2.VideoCapture(video_file)
|
12 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
13 |
-
random_frame_number = random.randint(0, total_frames - 1)
|
14 |
-
cap.set(cv2.CAP_PROP_POS_FRAMES, random_frame_number)
|
15 |
-
success, frame = cap.read()
|
16 |
-
cap.release()
|
17 |
-
|
18 |
-
return frame if success else None
|
19 |
-
|
20 |
-
# Заголовок приложения
|
21 |
-
st.title("Video to Text Converter")
|
22 |
|
23 |
-
# Загрузка видеофайла
|
24 |
-
uploaded_file = st.file_uploader("
|
25 |
|
26 |
-
if uploaded_file is not None:
|
27 |
-
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN_READ')}"}
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
output_text = response.json().get("generated_text")
|
47 |
-
st.success(output_text)
|
48 |
-
else:
|
49 |
-
st.error("Ошибка при обработке изображения!")
|
50 |
-
else:
|
51 |
-
st.error("Не удалось извлечь кадр из видео.")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import requests
|
5 |
+
import tempfile
|
|
|
|
|
6 |
|
7 |
+
# Заголовок приложения
|
8 |
+
st.title("Video Frame to Image Description")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Загрузка видеофайла
|
11 |
+
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
|
12 |
|
13 |
+
if uploaded_file is not None:
|
14 |
+
# Создаем временный файл для хранения видео
|
15 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
16 |
+
tfile.write(uploaded_file.read())
|
17 |
|
18 |
+
# Захват видео
|
19 |
+
cap = cv2.VideoCapture(tfile.name)
|
20 |
+
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
21 |
+
|
22 |
+
# Выбор случайного кадра
|
23 |
+
random_frame = np.random.randint(length)
|
24 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, random_frame)
|
25 |
+
ret, frame = cap.read()
|
|
|
26 |
|
27 |
+
if ret:
|
28 |
+
# Отображение выбранного кадра
|
29 |
+
st.image(frame, channels="BGR", caption=f"Random Frame {random_frame}")
|
30 |
+
|
31 |
+
# Конвертация кадра в формат, пригодный для отправки в модель
|
32 |
+
_, buf = cv2.imencode('.jpg', frame)
|
33 |
+
files = {'file': ('image.jpg', buf.tobytes(), 'image/jpeg')}
|
34 |
+
|
35 |
+
# Отправка изображения в модель
|
36 |
+
response = requests.post(
|
37 |
+
"https://hf.space/embed/nttdataspain/Image-To-Text-Lora-ViT/api/predict",
|
38 |
+
files=files
|
39 |
+
)
|
40 |
+
|
41 |
+
# Получение и отображение результата
|
42 |
+
if response.status_code == 200:
|
43 |
+
result = response.json()
|
44 |
+
description = result['data'][0]['generated_text']
|
45 |
+
st.success(f"Generated Description: {description}")
|
46 |
+
else:
|
47 |
+
st.error("Error: Could not get a response from the model.")
|
48 |
+
else:
|
49 |
+
st.error("Error: Could not read a frame from the video.")
|
50 |
|
51 |
+
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|