Inigozr commited on
Commit
d89619d
1 Parent(s): de34afd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
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('/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
28
 
29
- # Process results list
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("/uploadfile")
47
- async def create_upload_file(file: UploadFile = File(...)):
48
- contents = await file.read()
49
- image = Image.open(io.BytesIO(contents))
50
-
51
- # Save the image to a static directory
52
- save_path = f"/tmp/{file.filename}"
53
- image.save(save_path)
54
-
55
- # Perform YOLO prediction
56
- predictions = predict_yolo(save_path)
57
-
58
- # Draw bounding boxes on the image
59
- image_np = np.array(image)
60
- image_with_boxes = draw_boxes(image_np, predictions)
61
-
62
- # Save the image with bounding boxes
63
- image_with_boxes_path = f"/tmp/{file.filename.split('.')[0]}_with_boxes.jpg"
64
- cv2.imwrite(image_with_boxes_path, cv2.cvtColor(image_with_boxes, cv2.COLOR_RGB2BGR))
65
-
66
- # Render the HTML with the image and bounding boxes
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"}