Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -16,8 +16,42 @@ CLASS = model.model.names
|
|
16 |
defaul_bot_voice = "γγ―γγγγγγγΎγ"
|
17 |
area_thres = 0.3
|
18 |
|
19 |
-
app = FastAPI()
|
20 |
-
|
21 |
@app.get("/")
|
22 |
def read_root():
|
23 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
defaul_bot_voice = "γγ―γγγγγγγΎγ"
|
17 |
area_thres = 0.3
|
18 |
|
|
|
|
|
19 |
@app.get("/")
|
20 |
def read_root():
|
21 |
+
return {"Message": "Application startup complete"}
|
22 |
+
|
23 |
+
|
24 |
+
@app.post("/aisatsu_api/")
|
25 |
+
async def predict_api(
|
26 |
+
file: UploadFile = File(...),
|
27 |
+
last_seen: Optional[str] = Form(None)
|
28 |
+
):
|
29 |
+
image = read_image_file(await file.read())
|
30 |
+
results = model.predict(image, show=False)[0]
|
31 |
+
image = read_image_as_pil(image)
|
32 |
+
masks, boxes = results.masks, results.boxes
|
33 |
+
area_image = image.width * image.height
|
34 |
+
voice_bot = None
|
35 |
+
most_close = 0
|
36 |
+
out_img = None
|
37 |
+
diff_value = 0.5
|
38 |
+
if boxes is not None:
|
39 |
+
for xyxy, conf, cls in zip(boxes.xyxy, boxes.conf, boxes.cls):
|
40 |
+
if int(cls) != 0:
|
41 |
+
continue
|
42 |
+
box = xyxy.tolist()
|
43 |
+
area_rate = (box[2] - box[0]) * (box[3] - box[1]) / area_image
|
44 |
+
if area_rate >= most_close:
|
45 |
+
out_img = image.crop(tuple(box)).resize((64, 64))
|
46 |
+
most_close = area_rate
|
47 |
+
if last_seen is not None:
|
48 |
+
last_seen = base64_to_pil(last_seen)
|
49 |
+
if out_img is not None:
|
50 |
+
diff_value = dist.euclidean(get_hist(out_img), get_hist(last_seen))
|
51 |
+
print(most_close, diff_value)
|
52 |
+
if most_close >= area_thres and diff_value >= 0.5:
|
53 |
+
voice_bot = tts(defaul_bot_voice, language="ja")
|
54 |
+
return {
|
55 |
+
"voice": voice_bot,
|
56 |
+
"image": pil_to_base64(out_img) if out_img is not None else None
|
57 |
+
}
|