Update main.py
Browse files
main.py
CHANGED
|
@@ -9,39 +9,43 @@ import traceback
|
|
| 9 |
app = FastAPI()
|
| 10 |
model = YOLO("best.pt")
|
| 11 |
|
|
|
|
| 12 |
class ImageData(BaseModel):
|
| 13 |
image_base64: str
|
| 14 |
|
|
|
|
| 15 |
@app.get("/")
|
| 16 |
def read_root():
|
| 17 |
return {"status": "SA'EF API is live and running!"}
|
| 18 |
|
|
|
|
| 19 |
@app.post("/predict")
|
| 20 |
async def predict(data: ImageData):
|
| 21 |
try:
|
| 22 |
image_bytes = base64.b64decode(data.image_base64)
|
| 23 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 24 |
-
|
| 25 |
results = model(image)
|
| 26 |
-
result = results
|
| 27 |
|
| 28 |
if len(result.boxes) == 0:
|
| 29 |
return {"status": "not_found", "message": "No wound detected"}
|
| 30 |
|
| 31 |
-
box = result.boxes
|
|
|
|
| 32 |
confidence = float(box.conf.item())
|
| 33 |
class_id = int(box.cls.item())
|
| 34 |
class_name = result.names[class_id]
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
coords = box.xywhn.tolist()
|
| 38 |
|
| 39 |
return {
|
| 40 |
"status": "success",
|
| 41 |
"wound_type": class_name,
|
| 42 |
"confidence": confidence,
|
| 43 |
-
"x_center": coords,
|
| 44 |
-
"y_center": coords
|
| 45 |
}
|
| 46 |
|
| 47 |
except Exception as e:
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
model = YOLO("best.pt")
|
| 11 |
|
| 12 |
+
|
| 13 |
class ImageData(BaseModel):
|
| 14 |
image_base64: str
|
| 15 |
|
| 16 |
+
|
| 17 |
@app.get("/")
|
| 18 |
def read_root():
|
| 19 |
return {"status": "SA'EF API is live and running!"}
|
| 20 |
|
| 21 |
+
|
| 22 |
@app.post("/predict")
|
| 23 |
async def predict(data: ImageData):
|
| 24 |
try:
|
| 25 |
image_bytes = base64.b64decode(data.image_base64)
|
| 26 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 27 |
+
|
| 28 |
results = model(image)
|
| 29 |
+
result = results[0] # FIX 1: index into the list
|
| 30 |
|
| 31 |
if len(result.boxes) == 0:
|
| 32 |
return {"status": "not_found", "message": "No wound detected"}
|
| 33 |
|
| 34 |
+
box = result.boxes[0] # FIX 2: take only the first (highest-conf) box
|
| 35 |
+
|
| 36 |
confidence = float(box.conf.item())
|
| 37 |
class_id = int(box.cls.item())
|
| 38 |
class_name = result.names[class_id]
|
| 39 |
+
|
| 40 |
+
# FIX 3: unpack coords properly — xywhn shape is (1,4), tolist() → [x,y,w,h]
|
| 41 |
+
coords = box.xywhn[0].tolist() # [x_center, y_center, width, height]
|
| 42 |
|
| 43 |
return {
|
| 44 |
"status": "success",
|
| 45 |
"wound_type": class_name,
|
| 46 |
"confidence": confidence,
|
| 47 |
+
"x_center": coords[0],
|
| 48 |
+
"y_center": coords[1]
|
| 49 |
}
|
| 50 |
|
| 51 |
except Exception as e:
|