Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,34 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
-
from fastapi.responses import JSONResponse,
|
3 |
from ultralytics import YOLO
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
# Load the
|
10 |
-
model = YOLO("yolov8m_custom.pt")
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
return ""
|
16 |
-
<h2>Door & Window Detection API</h2>
|
17 |
-
<p>Use <code>/predict</code> endpoint to POST a .jpg or .png image and get detections.</p>
|
18 |
-
"""
|
19 |
|
20 |
-
# 🔹 Change to /predict to match typical inference APIs
|
21 |
@app.post("/predict")
|
22 |
async def predict(file: UploadFile = File(...)):
|
|
|
23 |
if not file.filename.lower().endswith((".jpg", ".png")):
|
24 |
return JSONResponse(
|
25 |
content={"error": "Only JPG or PNG images are allowed"},
|
26 |
status_code=400
|
27 |
)
|
28 |
|
|
|
29 |
image_bytes = await file.read()
|
30 |
np_array = np.frombuffer(image_bytes, np.uint8)
|
31 |
image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
@@ -33,9 +36,10 @@ async def predict(file: UploadFile = File(...)):
|
|
33 |
if image is None:
|
34 |
return JSONResponse(content={"error": "Could not decode image"}, status_code=400)
|
35 |
|
36 |
-
# Run YOLOv8
|
37 |
-
results = model.predict(image)
|
38 |
|
|
|
39 |
detections = []
|
40 |
for result in results:
|
41 |
for box in result.boxes:
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.responses import JSONResponse, RedirectResponse
|
3 |
from ultralytics import YOLO
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
|
7 |
+
# Initialize FastAPI app
|
8 |
+
app = FastAPI(
|
9 |
+
title="Door & Window Detection API",
|
10 |
+
description="Upload an image (.jpg or .png) to detect doors and windows.",
|
11 |
+
version="1.0.0"
|
12 |
+
)
|
13 |
|
14 |
+
# Load the YOLOv8 model once at startup
|
15 |
+
model = YOLO("yolov8m_custom.pt") # Replace with yolov8n_custom.pt if needed
|
16 |
|
17 |
+
@app.get("/")
|
18 |
+
async def redirect_to_docs():
|
19 |
+
# Redirect to Swagger UI for easy testing
|
20 |
+
return RedirectResponse(url="/docs")
|
|
|
|
|
|
|
21 |
|
|
|
22 |
@app.post("/predict")
|
23 |
async def predict(file: UploadFile = File(...)):
|
24 |
+
# Validate file extension
|
25 |
if not file.filename.lower().endswith((".jpg", ".png")):
|
26 |
return JSONResponse(
|
27 |
content={"error": "Only JPG or PNG images are allowed"},
|
28 |
status_code=400
|
29 |
)
|
30 |
|
31 |
+
# Read file and convert to OpenCV image
|
32 |
image_bytes = await file.read()
|
33 |
np_array = np.frombuffer(image_bytes, np.uint8)
|
34 |
image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
|
|
36 |
if image is None:
|
37 |
return JSONResponse(content={"error": "Could not decode image"}, status_code=400)
|
38 |
|
39 |
+
# Run inference with YOLOv8
|
40 |
+
results = model.predict(image, verbose=False)
|
41 |
|
42 |
+
# Process results
|
43 |
detections = []
|
44 |
for result in results:
|
45 |
for box in result.boxes:
|