Spaces:
Runtime error
Runtime error
Update tasks/image.py
Browse files- tasks/image.py +38 -1
tasks/image.py
CHANGED
@@ -180,4 +180,41 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
180 |
}
|
181 |
}
|
182 |
|
183 |
-
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
}
|
181 |
}
|
182 |
|
183 |
+
return results
|
184 |
+
|
185 |
+
@router.post("/detect-smoke", tags=["Smoke Detection"])
|
186 |
+
async def detect_smoke_image(file: UploadFile = File(...)):
|
187 |
+
"""
|
188 |
+
Detect smoke in a single uploaded image using YOLO model.
|
189 |
+
Returns whether smoke is detected and any bounding boxes.
|
190 |
+
"""
|
191 |
+
# Load the uploaded image
|
192 |
+
image_bytes = await file.read()
|
193 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
194 |
+
|
195 |
+
# Load model
|
196 |
+
model_path = Path("tasks", "models")
|
197 |
+
model_name = "best.pt"
|
198 |
+
model = YOLO(Path(model_path, model_name))
|
199 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
200 |
+
model = model.to(device)
|
201 |
+
|
202 |
+
# Run prediction
|
203 |
+
results = model.predict(image, verbose=False)
|
204 |
+
boxes = results[0].boxes
|
205 |
+
|
206 |
+
response = {
|
207 |
+
"smoke_detected": bool(len(boxes)),
|
208 |
+
"num_detections": len(boxes),
|
209 |
+
"boxes": []
|
210 |
+
}
|
211 |
+
|
212 |
+
for box in boxes:
|
213 |
+
xywhn = box.xywhn[0].cpu().numpy().tolist()
|
214 |
+
conf = float(box.conf[0]) if box.conf is not None else None
|
215 |
+
response["boxes"].append({
|
216 |
+
"xywhn": xywhn,
|
217 |
+
"confidence": conf
|
218 |
+
})
|
219 |
+
|
220 |
+
return JSONResponse(content=response)
|