Views
No views yet
1from transformers import AutoImageProcessor, DeformableDetrForObjectDetection
2import torch
3from PIL import Image
4import requests
5
6url = "http://images.cocodataset.org/val2017/000000039769.jpg"
7image = Image.open(requests.get(url, stream=True).raw)
8
9processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr")
10model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr")
11
12inputs = processor(images=image, return_tensors="pt")
13outputs = model(**inputs)
14
15# convert outputs (bounding boxes and class logits) to COCO API
16# let's only keep detections with score > 0.7
17target_sizes = torch.tensor([image.size[::-1]])
18results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
19
20for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
21 box = [round(i, 2) for i in box.tolist()]
22 print(
23 f"Detected {model.config.id2label[label.item()]} with confidence "
24 f"{round(score.item(), 3)} at location {box}"
25 )Detected cat with confidence 0.856 at location [342.19, 24.3, 640.02, 372.25]
Detected remote with confidence 0.739 at location [40.79, 72.78, 176.76, 117.25]
Detected cat with confidence 0.859 at location [16.5, 52.84, 318.25, 470.78]1@misc{https://doi.org/10.48550/arxiv.2010.04159,
2 doi = {10.48550/ARXIV.2010.04159},
3 url = {https://arxiv.org/abs/2010.04159},
4 author = {Zhu, Xizhou and Su, Weijie and Lu, Lewei and Li, Bin and Wang, Xiaogang and Dai, Jifeng},
5 keywords = {Computer Vision and Pattern Recognition (cs.CV), FOS: Computer and information sciences, FOS: Computer and information sciences},
6 title = {Deformable DETR: Deformable Transformers for End-to-End Object Detection},
7 publisher = {arXiv},
8 year = {2020},
9 copyright = {arXiv.org perpetual, non-exclusive license}
10}