Update main.py
Browse files
main.py
CHANGED
|
@@ -2,11 +2,12 @@ from fastapi import FastAPI, HTTPException
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from ultralytics import YOLO
|
| 5 |
-
from PIL import Image
|
| 6 |
import io
|
| 7 |
import base64
|
| 8 |
import traceback
|
| 9 |
import math
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
@@ -21,6 +22,8 @@ app.add_middleware(
|
|
| 21 |
model = YOLO("best.pt")
|
| 22 |
responders_db = []
|
| 23 |
|
|
|
|
|
|
|
| 24 |
|
| 25 |
class ImageData(BaseModel):
|
| 26 |
image_base64: str
|
|
@@ -45,42 +48,86 @@ def read_root():
|
|
| 45 |
return {"status": "SA'EF API is live and running!"}
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def run_wound_detection(image_base64: str):
|
|
|
|
|
|
|
| 49 |
image_bytes = base64.b64decode(image_base64)
|
| 50 |
-
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
detections = []
|
| 56 |
|
| 57 |
if result.boxes is None or len(result.boxes) == 0:
|
| 58 |
return {
|
| 59 |
"status": "not_found",
|
| 60 |
"message": "No wound detected",
|
|
|
|
|
|
|
| 61 |
"detections": [],
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
for i, box in enumerate(result.boxes):
|
| 65 |
confidence = float(box.conf.item())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
class_id = int(box.cls.item())
|
| 67 |
class_name = result.names[class_id]
|
| 68 |
|
| 69 |
-
# Normalized YOLO
|
| 70 |
x_center, y_center, width, height = box.xywhn[0].tolist()
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
detections.append(
|
| 73 |
{
|
| 74 |
"id": i,
|
| 75 |
"wound_type": class_name,
|
| 76 |
"confidence": confidence,
|
| 77 |
-
|
| 78 |
-
"
|
| 79 |
-
"
|
| 80 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
}
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
detections.sort(key=lambda item: item["confidence"], reverse=True)
|
| 85 |
best = detections[0]
|
| 86 |
|
|
@@ -88,11 +135,21 @@ def run_wound_detection(image_base64: str):
|
|
| 88 |
"status": "success",
|
| 89 |
"wound_type": best["wound_type"],
|
| 90 |
"confidence": best["confidence"],
|
|
|
|
| 91 |
"x_center": best["x_center"],
|
| 92 |
"y_center": best["y_center"],
|
| 93 |
"width": best["width"],
|
| 94 |
"height": best["height"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
"detections": detections,
|
|
|
|
| 96 |
}
|
| 97 |
|
| 98 |
|
|
@@ -119,7 +176,11 @@ def register_responder(responder: Responder):
|
|
| 119 |
responder_dict = responder.dict()
|
| 120 |
responder_dict["active"] = True
|
| 121 |
responders_db.append(responder_dict)
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
|
| 125 |
@app.post("/alert-nearby")
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
from PIL import Image, ImageOps
|
| 6 |
import io
|
| 7 |
import base64
|
| 8 |
import traceback
|
| 9 |
import math
|
| 10 |
+
import time
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
|
|
| 22 |
model = YOLO("best.pt")
|
| 23 |
responders_db = []
|
| 24 |
|
| 25 |
+
MIN_CONFIDENCE = 0.20
|
| 26 |
+
|
| 27 |
|
| 28 |
class ImageData(BaseModel):
|
| 29 |
image_base64: str
|
|
|
|
| 48 |
return {"status": "SA'EF API is live and running!"}
|
| 49 |
|
| 50 |
|
| 51 |
+
def clamp01(value: float) -> float:
|
| 52 |
+
return max(0.0, min(1.0, float(value)))
|
| 53 |
+
|
| 54 |
+
|
| 55 |
def run_wound_detection(image_base64: str):
|
| 56 |
+
started = time.time()
|
| 57 |
+
|
| 58 |
image_bytes = base64.b64decode(image_base64)
|
|
|
|
| 59 |
|
| 60 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 61 |
+
|
| 62 |
+
# VERY IMPORTANT for phone camera images.
|
| 63 |
+
# Without this, YOLO can detect correctly but boxes appear shifted/rotated in-app.
|
| 64 |
+
image = ImageOps.exif_transpose(image).convert("RGB")
|
| 65 |
+
|
| 66 |
+
image_width, image_height = image.size
|
| 67 |
|
| 68 |
+
results = model(
|
| 69 |
+
image,
|
| 70 |
+
imgsz=640,
|
| 71 |
+
conf=MIN_CONFIDENCE,
|
| 72 |
+
verbose=False,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
result = results[0]
|
| 76 |
detections = []
|
| 77 |
|
| 78 |
if result.boxes is None or len(result.boxes) == 0:
|
| 79 |
return {
|
| 80 |
"status": "not_found",
|
| 81 |
"message": "No wound detected",
|
| 82 |
+
"image_width": image_width,
|
| 83 |
+
"image_height": image_height,
|
| 84 |
"detections": [],
|
| 85 |
+
"latency_ms": round((time.time() - started) * 1000, 2),
|
| 86 |
}
|
| 87 |
|
| 88 |
for i, box in enumerate(result.boxes):
|
| 89 |
confidence = float(box.conf.item())
|
| 90 |
+
|
| 91 |
+
if confidence < MIN_CONFIDENCE:
|
| 92 |
+
continue
|
| 93 |
+
|
| 94 |
class_id = int(box.cls.item())
|
| 95 |
class_name = result.names[class_id]
|
| 96 |
|
| 97 |
+
# Normalized YOLO center format: x_center, y_center, width, height
|
| 98 |
x_center, y_center, width, height = box.xywhn[0].tolist()
|
| 99 |
|
| 100 |
+
# Normalized corner format: x_min, y_min, x_max, y_max
|
| 101 |
+
x_min, y_min, x_max, y_max = box.xyxyn[0].tolist()
|
| 102 |
+
|
| 103 |
detections.append(
|
| 104 |
{
|
| 105 |
"id": i,
|
| 106 |
"wound_type": class_name,
|
| 107 |
"confidence": confidence,
|
| 108 |
+
|
| 109 |
+
"x_center": clamp01(x_center),
|
| 110 |
+
"y_center": clamp01(y_center),
|
| 111 |
+
"width": clamp01(width),
|
| 112 |
+
"height": clamp01(height),
|
| 113 |
+
|
| 114 |
+
"x_min": clamp01(x_min),
|
| 115 |
+
"y_min": clamp01(y_min),
|
| 116 |
+
"x_max": clamp01(x_max),
|
| 117 |
+
"y_max": clamp01(y_max),
|
| 118 |
}
|
| 119 |
)
|
| 120 |
|
| 121 |
+
if len(detections) == 0:
|
| 122 |
+
return {
|
| 123 |
+
"status": "not_found",
|
| 124 |
+
"message": "No confident wound detected",
|
| 125 |
+
"image_width": image_width,
|
| 126 |
+
"image_height": image_height,
|
| 127 |
+
"detections": [],
|
| 128 |
+
"latency_ms": round((time.time() - started) * 1000, 2),
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
detections.sort(key=lambda item: item["confidence"], reverse=True)
|
| 132 |
best = detections[0]
|
| 133 |
|
|
|
|
| 135 |
"status": "success",
|
| 136 |
"wound_type": best["wound_type"],
|
| 137 |
"confidence": best["confidence"],
|
| 138 |
+
|
| 139 |
"x_center": best["x_center"],
|
| 140 |
"y_center": best["y_center"],
|
| 141 |
"width": best["width"],
|
| 142 |
"height": best["height"],
|
| 143 |
+
|
| 144 |
+
"x_min": best["x_min"],
|
| 145 |
+
"y_min": best["y_min"],
|
| 146 |
+
"x_max": best["x_max"],
|
| 147 |
+
"y_max": best["y_max"],
|
| 148 |
+
|
| 149 |
+
"image_width": image_width,
|
| 150 |
+
"image_height": image_height,
|
| 151 |
"detections": detections,
|
| 152 |
+
"latency_ms": round((time.time() - started) * 1000, 2),
|
| 153 |
}
|
| 154 |
|
| 155 |
|
|
|
|
| 176 |
responder_dict = responder.dict()
|
| 177 |
responder_dict["active"] = True
|
| 178 |
responders_db.append(responder_dict)
|
| 179 |
+
|
| 180 |
+
return {
|
| 181 |
+
"success": True,
|
| 182 |
+
"responder": responder_dict,
|
| 183 |
+
}
|
| 184 |
|
| 185 |
|
| 186 |
@app.post("/alert-nearby")
|