Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -12,6 +12,7 @@ from fastapi.responses import StreamingResponse
|
|
12 |
from utils import tts, read_image_file, pil_to_base64, get_hist
|
13 |
from typing import Optional
|
14 |
from huggingface_hub import hf_hub_download
|
|
|
15 |
|
16 |
from io import BytesIO
|
17 |
import zipfile
|
@@ -42,6 +43,8 @@ async def predict_api(
|
|
42 |
most_close = 0
|
43 |
out_img = None
|
44 |
diff_value = 0.5
|
|
|
|
|
45 |
if boxes is not None:
|
46 |
for xyxy, conf, cls in zip(boxes.xyxy, boxes.conf, boxes.cls):
|
47 |
if int(cls) != 0:
|
@@ -51,20 +54,26 @@ async def predict_api(
|
|
51 |
if area_rate >= most_close:
|
52 |
out_img = image.crop(tuple(box)).resize((64, 64))
|
53 |
most_close = area_rate
|
|
|
|
|
54 |
if last_seen is not None:
|
55 |
last_seen = read_image_file(await last_seen.read())
|
56 |
if out_img is not None:
|
57 |
diff_value = dist.euclidean(get_hist(out_img), get_hist(last_seen))
|
58 |
-
print(
|
|
|
|
|
59 |
if most_close >= area_thres and diff_value >= 0.5:
|
60 |
voice_bot_path = tts(defaul_bot_voice, language="ja")
|
61 |
image_bot_path = pil_to_base64(out_img)
|
|
|
62 |
io = BytesIO()
|
63 |
zip_filename = "final_archive.zip"
|
64 |
with zipfile.ZipFile(io, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
|
65 |
for file_path in [voice_bot_path, image_bot_path]:
|
66 |
zf.write(file_path)
|
67 |
zf.close()
|
|
|
68 |
return StreamingResponse(
|
69 |
iter([io.getvalue()]),
|
70 |
media_type="application/x-zip-compressed",
|
|
|
12 |
from utils import tts, read_image_file, pil_to_base64, get_hist
|
13 |
from typing import Optional
|
14 |
from huggingface_hub import hf_hub_download
|
15 |
+
import time
|
16 |
|
17 |
from io import BytesIO
|
18 |
import zipfile
|
|
|
43 |
most_close = 0
|
44 |
out_img = None
|
45 |
diff_value = 0.5
|
46 |
+
total_time = time.time()
|
47 |
+
start_time = time.time()
|
48 |
if boxes is not None:
|
49 |
for xyxy, conf, cls in zip(boxes.xyxy, boxes.conf, boxes.cls):
|
50 |
if int(cls) != 0:
|
|
|
54 |
if area_rate >= most_close:
|
55 |
out_img = image.crop(tuple(box)).resize((64, 64))
|
56 |
most_close = area_rate
|
57 |
+
print("Yolo time", time.time() - start_time)
|
58 |
+
start_time = time.time()
|
59 |
if last_seen is not None:
|
60 |
last_seen = read_image_file(await last_seen.read())
|
61 |
if out_img is not None:
|
62 |
diff_value = dist.euclidean(get_hist(out_img), get_hist(last_seen))
|
63 |
+
print("Hist time", time.time() - start_time)
|
64 |
+
start_time = time.time()
|
65 |
+
print(f"Distance: {most_close}. Different value: {diff_value}")
|
66 |
if most_close >= area_thres and diff_value >= 0.5:
|
67 |
voice_bot_path = tts(defaul_bot_voice, language="ja")
|
68 |
image_bot_path = pil_to_base64(out_img)
|
69 |
+
print("Voice time", time.time() - start_time)
|
70 |
io = BytesIO()
|
71 |
zip_filename = "final_archive.zip"
|
72 |
with zipfile.ZipFile(io, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
|
73 |
for file_path in [voice_bot_path, image_bot_path]:
|
74 |
zf.write(file_path)
|
75 |
zf.close()
|
76 |
+
print("Total time", time.time() - total_time)
|
77 |
return StreamingResponse(
|
78 |
iter([io.getvalue()]),
|
79 |
media_type="application/x-zip-compressed",
|