|
from typing import Union |
|
|
|
from fastapi import FastAPI,File |
|
from PIL import Image |
|
from io import BytesIO |
|
from transformers import DetrImageProcessor, DetrForObjectDetection |
|
import torch |
|
import requests |
|
|
|
app = FastAPI(title="Object Detection", |
|
docs_url="/", |
|
description="Object detection in Image") |
|
|
|
|
|
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") |
|
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") |
|
|
|
|
|
|
|
@app.post('/image') |
|
def read_image(image_file: bytes = File(...)): |
|
image = Image.open(BytesIO(image_file)) |
|
|
|
|
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
print("image loaded") |
|
outputs = model(**inputs) |
|
target_sizes = torch.tensor([image.size[::-1]]) |
|
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0] |
|
print("results pushed") |
|
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): |
|
box = [round(i, 2) for i in box.tolist()] |
|
print( |
|
f"Detected {model.config.id2label[label.item()]} with confidence " |
|
f"{round(score.item(), 3)} at location {box}" |
|
) |
|
print(results["scores"]) |
|
print(results["labels"]) |
|
print(results["boxes"]) |
|
response = {} |
|
response['scores'] = results['scores'] |
|
response['labels'] = results['labels'] |
|
response['boxes'] = results['boxes'] |
|
return response |
|
|
|
|
|
|