Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
import time
|
| 2 |
-
total_time = time.time()
|
| 3 |
-
start_time = time.time()
|
| 4 |
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from base64 import b64encode
|
|
@@ -9,7 +7,6 @@ import numpy as np
|
|
| 9 |
from scipy.spatial import distance as dist
|
| 10 |
from typing import Union
|
| 11 |
|
| 12 |
-
from sahi.utils.cv import read_image_as_pil
|
| 13 |
from fastapi import FastAPI, File, UploadFile
|
| 14 |
from fastapi.responses import StreamingResponse
|
| 15 |
|
|
@@ -20,14 +17,10 @@ from huggingface_hub import hf_hub_download
|
|
| 20 |
|
| 21 |
from io import BytesIO
|
| 22 |
import zipfile
|
| 23 |
-
print("Import time", time.time() - start_time)
|
| 24 |
-
start_time = time.time()
|
| 25 |
|
| 26 |
model_path = hf_hub_download(repo_id="ultralyticsplus/yolov8s", filename='yolov8s.pt')
|
| 27 |
model = YOLO(model_path)
|
| 28 |
|
| 29 |
-
print("Load model time", time.time() - start_time)
|
| 30 |
-
|
| 31 |
CLASS = model.model.names
|
| 32 |
defaul_bot_voice = "γγ―γγγγγγγΎγ"
|
| 33 |
area_thres = 0.3
|
|
@@ -44,17 +37,18 @@ async def predict_api(
|
|
| 44 |
file: UploadFile = File(...),
|
| 45 |
last_seen: Union[UploadFile, None] = File(None)
|
| 46 |
):
|
|
|
|
| 47 |
start_time = time.time()
|
| 48 |
image = read_image_file(await file.read())
|
| 49 |
print("Read image", time.time() - start_time)
|
|
|
|
| 50 |
results = model.predict(image, show=False)[0]
|
| 51 |
-
|
| 52 |
masks, boxes = results.masks, results.boxes
|
| 53 |
area_image = image.width * image.height
|
| 54 |
most_close = 0
|
| 55 |
out_img = None
|
| 56 |
diff_value = 0.5
|
| 57 |
-
# total_time = time.time()
|
| 58 |
start_time = time.time()
|
| 59 |
if boxes is not None:
|
| 60 |
for xyxy, conf, cls in zip(boxes.xyxy, boxes.conf, boxes.cls):
|
|
@@ -65,7 +59,7 @@ async def predict_api(
|
|
| 65 |
if area_rate >= most_close:
|
| 66 |
out_img = image.crop(tuple(box)).resize((64, 64))
|
| 67 |
most_close = area_rate
|
| 68 |
-
print("
|
| 69 |
start_time = time.time()
|
| 70 |
if last_seen is not None:
|
| 71 |
last_seen = read_image_file(await last_seen.read())
|
|
|
|
| 1 |
import time
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from ultralytics import YOLO
|
| 4 |
from base64 import b64encode
|
|
|
|
| 7 |
from scipy.spatial import distance as dist
|
| 8 |
from typing import Union
|
| 9 |
|
|
|
|
| 10 |
from fastapi import FastAPI, File, UploadFile
|
| 11 |
from fastapi.responses import StreamingResponse
|
| 12 |
|
|
|
|
| 17 |
|
| 18 |
from io import BytesIO
|
| 19 |
import zipfile
|
|
|
|
|
|
|
| 20 |
|
| 21 |
model_path = hf_hub_download(repo_id="ultralyticsplus/yolov8s", filename='yolov8s.pt')
|
| 22 |
model = YOLO(model_path)
|
| 23 |
|
|
|
|
|
|
|
| 24 |
CLASS = model.model.names
|
| 25 |
defaul_bot_voice = "γγ―γγγγγγγΎγ"
|
| 26 |
area_thres = 0.3
|
|
|
|
| 37 |
file: UploadFile = File(...),
|
| 38 |
last_seen: Union[UploadFile, None] = File(None)
|
| 39 |
):
|
| 40 |
+
total_time = time.time()
|
| 41 |
start_time = time.time()
|
| 42 |
image = read_image_file(await file.read())
|
| 43 |
print("Read image", time.time() - start_time)
|
| 44 |
+
start_time = time.time()
|
| 45 |
results = model.predict(image, show=False)[0]
|
| 46 |
+
print("Model predict", time.time() - start_time)
|
| 47 |
masks, boxes = results.masks, results.boxes
|
| 48 |
area_image = image.width * image.height
|
| 49 |
most_close = 0
|
| 50 |
out_img = None
|
| 51 |
diff_value = 0.5
|
|
|
|
| 52 |
start_time = time.time()
|
| 53 |
if boxes is not None:
|
| 54 |
for xyxy, conf, cls in zip(boxes.xyxy, boxes.conf, boxes.cls):
|
|
|
|
| 59 |
if area_rate >= most_close:
|
| 60 |
out_img = image.crop(tuple(box)).resize((64, 64))
|
| 61 |
most_close = area_rate
|
| 62 |
+
print("Get face time", time.time() - start_time)
|
| 63 |
start_time = time.time()
|
| 64 |
if last_seen is not None:
|
| 65 |
last_seen = read_image_file(await last_seen.read())
|