m4az19 commited on
Commit
a437062
·
verified ·
1 Parent(s): d4d725d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +14 -22
main.py CHANGED
@@ -5,7 +5,7 @@ import time
5
  import traceback
6
  from io import BytesIO
7
 
8
- from fastapi import FastAPI, HTTPException
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from PIL import Image, ImageOps
11
  from pydantic import BaseModel
@@ -22,7 +22,7 @@ app.add_middleware(
22
  )
23
 
24
  MIN_CONFIDENCE = float(os.environ.get("SAEF_MIN_CONFIDENCE", "0.20"))
25
- MAX_IMAGE_DIMENSION = int(os.environ.get("SAEF_MAX_IMAGE_DIMENSION", "1280"))
26
  MODEL_PATH = os.environ.get("SAEF_MODEL_PATH", "best.pt")
27
 
28
  model = YOLO(MODEL_PATH)
@@ -35,10 +35,6 @@ _inference_lock = threading.Lock()
35
  responders_db = []
36
 
37
 
38
- class ImageData(BaseModel):
39
- image_base64: str
40
-
41
-
42
  class Responder(BaseModel):
43
  name: str
44
  phone: str
@@ -87,14 +83,9 @@ def clamp01(value: float) -> float:
87
  return max(0.0, min(1.0, float(value)))
88
 
89
 
90
- def run_wound_detection(image_base64: str) -> dict:
91
  started = time.time()
92
 
93
- try:
94
- image_bytes = base64_decode(image_base64)
95
- except Exception as exc:
96
- raise HTTPException(status_code=400, detail=f"Invalid image data: {exc}")
97
-
98
  try:
99
  image = Image.open(BytesIO(image_bytes))
100
  # VERY IMPORTANT for phone camera images. Without this, YOLO can
@@ -187,12 +178,6 @@ def run_wound_detection(image_base64: str) -> dict:
187
  }
188
 
189
 
190
- def base64_decode(data: str) -> bytes:
191
- import base64
192
-
193
- return base64.b64decode(data)
194
-
195
-
196
  # NOTE: these routes are intentionally plain `def`, not `async def`. YOLO
197
  # inference is a blocking, CPU-bound call — running it inside an `async def`
198
  # route would execute it directly on the single event loop thread and stall
@@ -200,10 +185,16 @@ def base64_decode(data: str) -> bytes:
200
  # until it finished. A sync `def` route is automatically dispatched by
201
  # FastAPI to a worker threadpool, so one slow inference no longer blocks
202
  # the whole server.
 
 
 
 
 
203
  @app.post("/predict")
204
- def predict(data: ImageData):
205
  try:
206
- return run_wound_detection(data.image_base64)
 
207
  except HTTPException:
208
  raise
209
  except Exception as e:
@@ -212,9 +203,10 @@ def predict(data: ImageData):
212
 
213
 
214
  @app.post("/track")
215
- def track(data: ImageData):
216
  try:
217
- return run_wound_detection(data.image_base64)
 
218
  except HTTPException:
219
  raise
220
  except Exception as e:
 
5
  import traceback
6
  from io import BytesIO
7
 
8
+ from fastapi import FastAPI, File, HTTPException, UploadFile
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from PIL import Image, ImageOps
11
  from pydantic import BaseModel
 
22
  )
23
 
24
  MIN_CONFIDENCE = float(os.environ.get("SAEF_MIN_CONFIDENCE", "0.20"))
25
+ MAX_IMAGE_DIMENSION = int(os.environ.get("SAEF_MAX_IMAGE_DIMENSION", "960"))
26
  MODEL_PATH = os.environ.get("SAEF_MODEL_PATH", "best.pt")
27
 
28
  model = YOLO(MODEL_PATH)
 
35
  responders_db = []
36
 
37
 
 
 
 
 
38
  class Responder(BaseModel):
39
  name: str
40
  phone: str
 
83
  return max(0.0, min(1.0, float(value)))
84
 
85
 
86
+ def run_wound_detection(image_bytes: bytes) -> dict:
87
  started = time.time()
88
 
 
 
 
 
 
89
  try:
90
  image = Image.open(BytesIO(image_bytes))
91
  # VERY IMPORTANT for phone camera images. Without this, YOLO can
 
178
  }
179
 
180
 
 
 
 
 
 
 
181
  # NOTE: these routes are intentionally plain `def`, not `async def`. YOLO
182
  # inference is a blocking, CPU-bound call — running it inside an `async def`
183
  # route would execute it directly on the single event loop thread and stall
 
185
  # until it finished. A sync `def` route is automatically dispatched by
186
  # FastAPI to a worker threadpool, so one slow inference no longer blocks
187
  # the whole server.
188
+ #
189
+ # These also now take the JPEG as a raw multipart file instead of a
190
+ # base64-encoded JSON string. Base64 inflates the payload by ~33% and costs
191
+ # encode/decode CPU time on both ends for zero benefit on a binary upload —
192
+ # cutting it out is a straightforward latency win on every single frame.
193
  @app.post("/predict")
194
+ def predict(image: UploadFile = File(...)):
195
  try:
196
+ image_bytes = image.file.read()
197
+ return run_wound_detection(image_bytes)
198
  except HTTPException:
199
  raise
200
  except Exception as e:
 
203
 
204
 
205
  @app.post("/track")
206
+ def track(image: UploadFile = File(...)):
207
  try:
208
+ image_bytes = image.file.read()
209
+ return run_wound_detection(image_bytes)
210
  except HTTPException:
211
  raise
212
  except Exception as e: