Views
No views yet
1from transformers import YolosImageProcessor, YolosForObjectDetection
2from PIL import Image
3import torch
4import requests
5url = "http://images.cocodataset.org/val2017/000000039769.jpg"
6image = Image.open(requests.get(url, stream=True).raw)
7model = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny')
8image_processor = YolosImageProcessor.from_pretrained("hustvl/yolos-tiny")
9inputs = image_processor(images=image, return_tensors="pt")
10outputs = model(**inputs)
11# model predicts bounding boxes and corresponding COCO classes
12logits = outputs.logits
13bboxes = outputs.pred_boxes
14# print results
15target_sizes = torch.tensor([image.size[::-1]])
16results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
17for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
18 box = [round(i, 2) for i in box.tolist()]
19 print(
20 f"Detected {model.config.id2label[label.item()]} with confidence "
21 f"{round(score.item(), 3)} at location {box}"
22 )1@article{DBLP:journals/corr/abs-2106-00666,
2 author = {Yuxin Fang and
3 Bencheng Liao and
4 Xinggang Wang and
5 Jiemin Fang and
6 Jiyang Qi and
7 Rui Wu and
8 Jianwei Niu and
9 Wenyu Liu},
10 title = {You Only Look at One Sequence: Rethinking Transformer in Vision through
11 Object Detection},
12 journal = {CoRR},
13 volume = {abs/2106.00666},
14 year = {2021},
15 url = {https://arxiv.org/abs/2106.00666},
16 eprinttype = {arXiv},
17 eprint = {2106.00666},
18 timestamp = {Fri, 29 Apr 2022 19:49:16 +0200},
19 biburl = {https://dblp.org/rec/journals/corr/abs-2106-00666.bib},
20 bibsource = {dblp computer science bibliography, https://dblp.org}
21}