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)}")