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