Download app.py from sarim/SwiftUI_object_deteaction: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/sarim/SwiftUI_object_deteaction/resolve/main/app.py
- Command line
-
hf download hf://spaces/sarim/SwiftUI_object_deteaction/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sarim/SwiftUI_object_deteaction/resolve/main/app.py
1.61 kB
| 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") | |
| def read_image(image_file: bytes = File(...)): | |
| image = Image.open(BytesIO(image_file)) | |
| # url = "http://images.cocodataset.org/val2017/000000039769.jpg" | |
| # image = Image.open(requests.get(url, stream=True).raw) | |
| 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") | |
| response = {} | |
| for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): | |
| box = [round(i, 2) for i in box.tolist()] | |
| # response['scores'] = model.config.id2label[label.item()] | |
| # response['labels'] = score.item() | |
| # response['boxes'] = box | |
| print(model.config.id2label[label.item()]) | |
| print(score.item()) | |
| print(box) | |
| # print( | |
| # f"Detected {model.config.id2label[label.item()]} with confidence " | |
| # f"{round(score.item(), 3)} at location {box}" | |
| # ) | |
| return response | |