Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
import cv2
|
4 |
-
import requests
|
5 |
import tempfile
|
6 |
-
import
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Заголовок приложения
|
9 |
st.title("Video Frame to Image Description")
|
@@ -11,12 +14,6 @@ st.title("Video Frame to Image Description")
|
|
11 |
# Загрузка видеофайла
|
12 |
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
|
13 |
|
14 |
-
try:
|
15 |
-
response = requests.get("https://hf.space")
|
16 |
-
print(f"Status Code: {response.status_code}")
|
17 |
-
except requests.exceptions.SSLError as e:
|
18 |
-
print("SSL error occurred:", e)
|
19 |
-
|
20 |
cap = None # Инициализируем объект cap как None
|
21 |
|
22 |
if uploaded_file is not None:
|
@@ -35,31 +32,29 @@ if uploaded_file is not None:
|
|
35 |
ret, frame = cap.read()
|
36 |
|
37 |
if ret:
|
|
|
|
|
|
|
|
|
38 |
# Отображение выбранного кадра
|
39 |
-
st.image(
|
40 |
-
|
41 |
-
#
|
42 |
-
|
43 |
-
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
verify=False
|
54 |
-
)
|
55 |
-
|
56 |
-
# Получение и отображение результата
|
57 |
-
if response.status_code == 200:
|
58 |
-
result = response.json()
|
59 |
-
description = result['data'][0]['generated_text']
|
60 |
st.success(f"Generated Description: {description}")
|
61 |
-
|
62 |
-
st.error("Error: Could not get a response from the model.")
|
63 |
else:
|
64 |
st.error("Error: Could not read a frame from the video.")
|
65 |
else:
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
import cv2
|
|
|
4 |
import tempfile
|
5 |
+
from gradio_client import Client
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# Инициализация клиента для нового API
|
9 |
+
client = Client("https://pragnakalp-ocr-image-to-text.hf.space/--replicas/lhzf3/")
|
10 |
|
11 |
# Заголовок приложения
|
12 |
st.title("Video Frame to Image Description")
|
|
|
14 |
# Загрузка видеофайла
|
15 |
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
cap = None # Инициализируем объект cap как None
|
18 |
|
19 |
if uploaded_file is not None:
|
|
|
32 |
ret, frame = cap.read()
|
33 |
|
34 |
if ret:
|
35 |
+
# Конвертация кадра OpenCV в PIL Image
|
36 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
37 |
+
pil_image = Image.fromarray(frame_rgb)
|
38 |
+
|
39 |
# Отображение выбранного кадра
|
40 |
+
st.image(pil_image, caption=f"Random Frame {random_frame}")
|
41 |
+
|
42 |
+
# Сохранение изображения в байты
|
43 |
+
buf = tempfile.NamedTemporaryFile(suffix='.jpg', delete=False)
|
44 |
+
pil_image.save(buf, format='JPEG')
|
45 |
+
buf.close()
|
46 |
|
47 |
+
# Отправка изображения в новый API
|
48 |
+
try:
|
49 |
+
result = client.predict(
|
50 |
+
"PaddleOCR", # Метод для использования
|
51 |
+
buf.name, # Путь к изображению
|
52 |
+
api_name="/predict"
|
53 |
+
)
|
54 |
+
description = result['data']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
st.success(f"Generated Description: {description}")
|
56 |
+
except Exception as e:
|
57 |
+
st.error(f"Error: Could not get a response from the model. {str(e)}")
|
58 |
else:
|
59 |
st.error("Error: Could not read a frame from the video.")
|
60 |
else:
|