Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,56 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import requests
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Create a new FastAPI app instance
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
yolos_model = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny')
|
| 12 |
-
yolos_image_processor = YolosImageProcessor.from_pretrained("hustvl/yolos-tiny")
|
| 13 |
-
|
| 14 |
-
# Route for object detection using Yolos model
|
| 15 |
-
@app.get("/detect-objects")
|
| 16 |
def detect_objects(url: str):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ffrom fastapi import FastAPI, HTTPException, Response
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
import torch
|
| 5 |
import requests
|
| 6 |
+
import io
|
| 7 |
+
import base64
|
| 8 |
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
@app.get("/detect-objects", response_class=HTMLResponse)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def detect_objects(url: str):
|
| 13 |
+
try:
|
| 14 |
+
# Download the image from the specified URL
|
| 15 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 16 |
+
|
| 17 |
+
# Preprocess the image using the Yolos image processor
|
| 18 |
+
inputs = yolos_image_processor(images=image, return_tensors="pt")
|
| 19 |
+
|
| 20 |
+
# Run the Yolos model on the preprocessed image
|
| 21 |
+
outputs = yolos_model(**inputs)
|
| 22 |
+
|
| 23 |
+
# model predicts bounding boxes and corresponding COCO classes
|
| 24 |
+
logits = outputs.logits
|
| 25 |
+
pred_boxes = outputs.pred_boxes
|
| 26 |
+
|
| 27 |
+
# Post-process the object detection results
|
| 28 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 29 |
+
results = yolos_image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
|
| 30 |
+
|
| 31 |
+
# Draw bounding boxes on the image
|
| 32 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 33 |
+
image_draw = ImageDraw.Draw(image)
|
| 34 |
+
image_draw.rectangle(box.tolist(), outline="red", width=2)
|
| 35 |
+
image_draw.text((box[0], box[1]), f"{yolos_model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")
|
| 36 |
+
|
| 37 |
+
# Save the modified image to a byte stream
|
| 38 |
+
image_byte_array = io.BytesIO()
|
| 39 |
+
image.save(image_byte_array, format="PNG")
|
| 40 |
+
|
| 41 |
+
# Encode the byte stream as a base64 string
|
| 42 |
+
image_base64 = base64.b64encode(image_byte_array.getvalue()).decode("utf-8")
|
| 43 |
+
|
| 44 |
+
# Create a custom HTML response
|
| 45 |
+
html_content = f"""
|
| 46 |
+
<html>
|
| 47 |
+
<body>
|
| 48 |
+
<img src="data:image/png;base64,{image_base64}" alt="Detected Objects">
|
| 49 |
+
</body>
|
| 50 |
+
</html>
|
| 51 |
+
"""
|
| 52 |
+
return HTMLResponse(content=html_content)
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 56 |
+
|