Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,7 @@ templates = Jinja2Templates(directory="templates")
|
|
21 |
|
22 |
def predict_yolo(image_path):
|
23 |
# Load a model
|
24 |
-
model = YOLO('yolov8n.pt') # pretrained YOLOv8n model
|
25 |
|
26 |
# Run batched inference on a list of images
|
27 |
results = model(image_path) # return a list of Results objects
|
@@ -73,4 +73,24 @@ async def read_root():
|
|
73 |
|
74 |
@app.get("/")
|
75 |
async def read_root():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
return {"message": "Hello, this is a YOLO prediction API using FastAPI!"}
|
|
|
21 |
|
22 |
def predict_yolo(image_path):
|
23 |
# Load a model
|
24 |
+
model = YOLO('/usr/src/ultralytics/yolov8n.pt') # pretrained YOLOv8n model
|
25 |
|
26 |
# Run batched inference on a list of images
|
27 |
results = model(image_path) # return a list of Results objects
|
|
|
73 |
|
74 |
@app.get("/")
|
75 |
async def read_root():
|
76 |
+
# Download image content from URL
|
77 |
+
image_url = "https://s1.eestatic.com/2018/04/10/actualidad/actualidad_298735505_130172549_1000x500.jpg"
|
78 |
+
response = requests.get(image_url)
|
79 |
+
response.raise_for_status()
|
80 |
+
|
81 |
+
# Open the image using PIL
|
82 |
+
image = Image.open(io.BytesIO(response.content))
|
83 |
+
# Perform YOLO prediction
|
84 |
+
predictions = predict_yolo(image_url)
|
85 |
+
|
86 |
+
# Draw bounding boxes on the image
|
87 |
+
image_np = np.array(image)
|
88 |
+
image_with_boxes = draw_boxes(image_np, predictions)
|
89 |
+
|
90 |
+
# Save the image with bounding boxes
|
91 |
+
image_with_boxes_path = f"/tmp/{file.filename.split('.')[0]}_with_boxes.jpg"
|
92 |
+
cv2.imwrite(image_with_boxes_path, cv2.cvtColor(image_with_boxes, cv2.COLOR_RGB2BGR))
|
93 |
+
|
94 |
+
# Render the HTML with the image and bounding boxes
|
95 |
+
return templates.TemplateResponse("prediction.html", {"request": file, "image_path": image_with_boxes_path})
|
96 |
return {"message": "Hello, this is a YOLO prediction API using FastAPI!"}
|