Views
No views yet
1from transformers import AutoImageProcessor, DeformableDetrForObjectDetection
2import torch
3from PIL import Image
4import requests
5
6url = "https://huggingface.co/Aryn/deformable-detr-DocLayNet/resolve/main/examples/doclaynet_example_1.png"
7image = Image.open(requests.get(url, stream=True).raw)
8
9processor = AutoImageProcessor.from_pretrained("Aryn/deformable-detr-DocLayNet")
10model = DeformableDetrForObjectDetection.from_pretrained("Aryn/deformable-detr-DocLayNet")
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 )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}