Spaces:
Sleeping
Sleeping
Small Edits
Browse files
main.py
CHANGED
|
@@ -1,73 +1,83 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
import uvicorn
|
| 3 |
import base64
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
from ultralytics import YOLO
|
| 7 |
from datetime import datetime
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
-
|
| 10 |
-
app = FastAPI()
|
| 11 |
|
|
|
|
| 12 |
model = YOLO("yolo_modeln11_1502.pt")
|
| 13 |
|
| 14 |
class ImageRequest(BaseModel):
|
|
|
|
| 15 |
image: str
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
async def root():
|
|
|
|
| 19 |
current_time = datetime.now().isoformat()
|
| 20 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@app.post("/predict")
|
| 23 |
async def predict(request: ImageRequest):
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if not request.image:
|
| 26 |
return {"error": "Invalid Image"}
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
# Attempt to decode Base64 string to bytes
|
| 30 |
image_bytes = base64.b64decode(request.image, validate=True)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if image is None:
|
| 41 |
-
return {"error": "Invalid image"}
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
results = model.predict(image)
|
| 45 |
-
result = results[0]
|
| 46 |
-
|
| 47 |
-
# Response
|
| 48 |
-
json_result = {}
|
| 49 |
-
class_counters = {}
|
| 50 |
-
|
| 51 |
-
for box in result.boxes:
|
| 52 |
-
class_id = int(box.cls[0])
|
| 53 |
-
class_name = result.names[class_id]
|
| 54 |
-
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
else:
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if hasattr(result, "summary") and isinstance(result.summary, dict):
|
| 66 |
-
statistics_summary = result.summary
|
| 67 |
-
else:
|
| 68 |
-
statistics_summary = {name: count for name, count in class_counters.items()}
|
| 69 |
-
|
| 70 |
-
return {"statistics": statistics_summary, "detections": json_result}
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import uvicorn
|
| 4 |
import base64
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
from ultralytics import YOLO
|
| 8 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
app = FastAPI(title="PCB Defect Detection API")
|
| 11 |
model = YOLO("yolo_modeln11_1502.pt")
|
| 12 |
|
| 13 |
class ImageRequest(BaseModel):
|
| 14 |
+
"""Request model for image processing endpoint."""
|
| 15 |
image: str
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
async def root():
|
| 19 |
+
"""Root endpoint to verify API status."""
|
| 20 |
current_time = datetime.now().isoformat()
|
| 21 |
+
return {
|
| 22 |
+
"message": "PCB Defects API works",
|
| 23 |
+
"time": current_time
|
| 24 |
+
}
|
| 25 |
|
| 26 |
@app.post("/predict")
|
| 27 |
async def predict(request: ImageRequest):
|
| 28 |
+
"""
|
| 29 |
+
Process an image to detect PCB defects.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
request: Contains base64 encoded image
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
JSON with detection statistics and bounding boxes
|
| 36 |
+
"""
|
| 37 |
+
# Validate image input
|
| 38 |
if not request.image:
|
| 39 |
return {"error": "Invalid Image"}
|
| 40 |
|
| 41 |
try:
|
|
|
|
| 42 |
image_bytes = base64.b64decode(request.image, validate=True)
|
| 43 |
+
|
| 44 |
+
np_arr = np.frombuffer(image_bytes, np.uint8)
|
| 45 |
+
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 46 |
+
|
| 47 |
+
if image is None:
|
| 48 |
+
return {"error": "Invalid image"}
|
| 49 |
+
|
| 50 |
+
results = model.predict(image)
|
| 51 |
+
result = results[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
json_result = {}
|
| 54 |
+
class_counters = {}
|
| 55 |
+
|
| 56 |
+
for box in result.boxes:
|
| 57 |
+
class_id = int(box.cls[0])
|
| 58 |
+
class_name = result.names[class_id]
|
| 59 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 60 |
+
|
| 61 |
+
if class_name in class_counters:
|
| 62 |
+
class_counters[class_name] += 1
|
| 63 |
+
else:
|
| 64 |
+
class_counters[class_name] = 1
|
| 65 |
+
|
| 66 |
+
key = f"{class_name}{class_counters[class_name]}" if class_counters[class_name] > 1 else class_name
|
| 67 |
+
json_result[key] = [x1, y1, x2, y2]
|
| 68 |
+
|
| 69 |
+
if hasattr(result, "summary") and isinstance(result.summary, dict):
|
| 70 |
+
statistics_summary = result.summary
|
| 71 |
else:
|
| 72 |
+
statistics_summary = {name: count for name, count in class_counters.items()}
|
| 73 |
+
|
| 74 |
+
return {
|
| 75 |
+
"statistics": statistics_summary,
|
| 76 |
+
"detections": json_result
|
| 77 |
+
}
|
| 78 |
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return {"error": f"Invalid Image: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|