Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
model = YOLO("best.pt")
|
| 10 |
+
|
| 11 |
+
class ImageData(BaseModel):
|
| 12 |
+
image_base64: str
|
| 13 |
+
|
| 14 |
+
@app.post("/predict")
|
| 15 |
+
async def predict(data: ImageData):
|
| 16 |
+
try:
|
| 17 |
+
image_bytes = base64.b64decode(data.image_base64)
|
| 18 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 19 |
+
|
| 20 |
+
results = model(image)
|
| 21 |
+
|
| 22 |
+
if len(results.boxes) > 0:
|
| 23 |
+
box = results.boxes
|
| 24 |
+
confidence = float(box.conf)
|
| 25 |
+
class_id = int(box.cls)
|
| 26 |
+
class_name = model.names[class_id]
|
| 27 |
+
|
| 28 |
+
return {
|
| 29 |
+
"status": "success",
|
| 30 |
+
"wound_type": class_name,
|
| 31 |
+
"confidence": confidence
|
| 32 |
+
}
|
| 33 |
+
else:
|
| 34 |
+
return {
|
| 35 |
+
"status": "not_found",
|
| 36 |
+
"message": "No wound detected"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
raise HTTPException(status_code=500, detail=str(e))
|