Update app.py
Browse files
app.py
CHANGED
|
@@ -21,19 +21,12 @@ templates = Jinja2Templates(directory="templates")
|
|
| 21 |
|
| 22 |
def predict_yolo(image_path):
|
| 23 |
# Load a model
|
| 24 |
-
model = YOLO('/
|
| 25 |
|
| 26 |
# Run batched inference on a list of images
|
| 27 |
results = model(image_path) # return a list of Results objects
|
| 28 |
|
| 29 |
-
|
| 30 |
-
for result in results:
|
| 31 |
-
boxes = result.boxes # Boxes object for bbox outputs
|
| 32 |
-
# masks = result.masks # Masks object for segmentation masks outputs
|
| 33 |
-
# keypoints = result.keypoints # Keypoints object for pose outputs
|
| 34 |
-
# probs = result.probs # Probs object for classification outputs
|
| 35 |
-
predictions = boxes.json()
|
| 36 |
-
return predictions
|
| 37 |
|
| 38 |
|
| 39 |
def draw_boxes(image, boxes):
|
|
@@ -43,29 +36,30 @@ def draw_boxes(image, boxes):
|
|
| 43 |
return image
|
| 44 |
|
| 45 |
|
| 46 |
-
@app.post("/
|
| 47 |
-
async def
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
return templates.TemplateResponse("prediction.html", {"request": file, "image_path": image_with_boxes_path})
|
| 68 |
|
|
|
|
|
|
|
| 69 |
@app.get("/test")
|
| 70 |
async def read_root():
|
| 71 |
return {"message": "TEST"}
|
|
|
|
| 21 |
|
| 22 |
def predict_yolo(image_path):
|
| 23 |
# Load a model
|
| 24 |
+
model = YOLO('ultralyticsplus/yolov8s')
|
| 25 |
|
| 26 |
# Run batched inference on a list of images
|
| 27 |
results = model(image_path) # return a list of Results objects
|
| 28 |
|
| 29 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def draw_boxes(image, boxes):
|
|
|
|
| 36 |
return image
|
| 37 |
|
| 38 |
|
| 39 |
+
@app.post("/", response_class=HTMLResponse)
|
| 40 |
+
async def detect_yolo(request: Request, url: str = Form(...)
|
| 41 |
+
try:
|
| 42 |
+
# Download the image from the specified URL
|
| 43 |
+
async with httpx.AsyncClient() as client:
|
| 44 |
+
response = await client.get(url)
|
| 45 |
+
response.raise_for_status() # Raise an exception if there is an error in the request
|
| 46 |
+
content = response.content
|
| 47 |
+
|
| 48 |
+
image = Image.open(io.BytesIO(content))
|
| 49 |
+
|
| 50 |
+
results = predict_yolo(image)
|
| 51 |
+
|
| 52 |
+
render = render_result(model=model, image=image, result=results[0])
|
| 53 |
+
|
| 54 |
+
# Save the modified image to a byte sequence
|
| 55 |
+
image_byte_array = io.BytesIO()
|
| 56 |
+
image.save(image_byte_array, format="PNG")
|
| 57 |
+
|
| 58 |
+
# Return the image as a response with content type "image/png"
|
| 59 |
+
return templates.TemplateResponse("result.html", {"request": request, "image": base64.b64encode(image_byte_array.getvalue()).decode()})
|
|
|
|
| 60 |
|
| 61 |
+
except Exception as e:
|
| 62 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 63 |
@app.get("/test")
|
| 64 |
async def read_root():
|
| 65 |
return {"message": "TEST"}
|