Spaces:
Runtime error
Runtime error
File size: 1,095 Bytes
b0f51d3 edf409a b0f51d3 edf409a b0f51d3 edf409a b0f51d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from fastapi import FastAPI, UploadFile, File
from ultralytics import YOLO
from transformers import pipeline
from PIL import Image
import io
# Initialize the FastAPI app
app = FastAPI()
# Load the YOLOv5 model for object detection
model = YOLO("yolov8n.pt")
@app.post("/detect_objects/")
async def detect_objects(file: UploadFile = File(...)):
# Read image from uploaded file
image_bytes = await file.read()
image = Image.open(io.BytesIO(image_bytes))
# Perform object detection
results = model(image)
# Extract bounding box coordinates and labels
bounding_boxes = []
for result in results:
box = result['box']
bbox = {
'label': result['label'],
'confidence': result['score'],
'x_min': box['xmin'],
'y_min': box['ymin'],
'x_max': box['xmax'],
'y_max': box['ymax']
}
bounding_boxes.append(bbox)
# Return bounding box coordinates as response
return {"bounding_boxes": bounding_boxes}
# To run the server, use: uvicorn app:app --reload |