File size: 2,152 Bytes
dff1f36
95f629f
d63daaa
95f629f
1a68d24
 
95f629f
 
1a68d24
d63daaa
b98c932
1a68d24
 
b98c932
 
 
 
95f629f
1a68d24
95f629f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a552163
 
95f629f
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from fastapi import FastAPI, HTTPException, Response
from fastapi.responses import HTMLResponse
from transformers import pipeline, YolosForObjectDetection, YolosImageProcessor
from PIL import Image, ImageDraw
import torch
import requests
import io
import base64


# Create a new FastAPI app instance
app = FastAPI()

# Initialize the Yolos model and image processor
yolos_model = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny')
yolos_image_processor = YolosImageProcessor.from_pretrained("hustvl/yolos-tiny")

@app.get("/detect-objects", response_class=HTMLResponse)
def detect_objects(url: str):
    try:
        # Download the image from the specified URL
        image = Image.open(requests.get(url, stream=True).raw)

        # Preprocess the image using the Yolos image processor
        inputs = yolos_image_processor(images=image, return_tensors="pt")

        # Run the Yolos model on the preprocessed image
        outputs = yolos_model(**inputs)

        # model predicts bounding boxes and corresponding COCO classes
        logits = outputs.logits
        pred_boxes = outputs.pred_boxes

        # Post-process the object detection results
        target_sizes = torch.tensor([image.size[::-1]])
        results = yolos_image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]

        # Draw bounding boxes on the image
        for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
            image_draw = ImageDraw.Draw(image)
            image_draw.rectangle(box.tolist(), outline="red", width=2)
            image_draw.text((box[0], box[1]), f"{yolos_model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")

        # Save the modified image to a byte stream
        image_byte_array = io.BytesIO()
        image.save(image_byte_array, format="PNG")

        # Return the image as a Response with content type "image/png"
        return Response(content=image_byte_array.getvalue(), media_type="image/png")

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")