Views
No views yet
1from transformers import AutoImageProcessor, ConditionalDetrForObjectDetection
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("microsoft/conditional-detr-resnet-50")
10model = ConditionalDetrForObjectDetection.from_pretrained("microsoft/conditional-detr-resnet-50")
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 remote with confidence 0.833 at location [38.31, 72.1, 177.63, 118.45]
Detected cat with confidence 0.831 at location [9.2, 51.38, 321.13, 469.0]
Detected cat with confidence 0.804 at location [340.3, 16.85, 642.93, 370.95]1@inproceedings{MengCFZLYS021,
2 author = {Depu Meng and
3 Xiaokang Chen and
4 Zejia Fan and
5 Gang Zeng and
6 Houqiang Li and
7 Yuhui Yuan and
8 Lei Sun and
9 Jingdong Wang},
10 title = {Conditional {DETR} for Fast Training Convergence},
11 booktitle = {2021 {IEEE/CVF} International Conference on Computer Vision, {ICCV}
12 2021, Montreal, QC, Canada, October 10-17, 2021},
13}