detr-doc-table-detection/README.md

28 lines
1.0 KiB
Markdown
Raw Normal View History

2022-03-12 12:04:43 +00:00
```python
2022-12-16 20:06:59 +00:00
from transformers import DetrImageProcessor, DetrForObjectDetection
import torch
2022-03-12 12:04:43 +00:00
from PIL import Image
2022-12-16 20:06:59 +00:00
import requests
2022-03-12 12:07:32 +00:00
2022-12-16 20:06:59 +00:00
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
2022-03-12 12:07:32 +00:00
2022-12-16 20:06:59 +00:00
processor = DetrImageProcessor.from_pretrained("TahaDouaji/detr-doc-table-detection")
model = DetrForObjectDetection.from_pretrained("TahaDouaji/detr-doc-table-detection")
2022-03-12 12:07:32 +00:00
2022-12-16 20:06:59 +00:00
inputs = processor(images=image, return_tensors="pt")
2022-03-12 12:04:43 +00:00
outputs = model(**inputs)
2022-03-12 12:07:32 +00:00
2022-08-05 13:31:07 +00:00
# convert outputs (bounding boxes and class logits) to COCO API
2022-12-16 20:06:59 +00:00
# let's only keep detections with score > 0.9
2022-08-05 13:31:07 +00:00
target_sizes = torch.tensor([image.size[::-1]])
2022-12-16 20:06:59 +00:00
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
box = [round(i, 2) for i in box.tolist()]
print(
f"Detected {model.config.id2label[label.item()]} with confidence "
f"{round(score.item(), 3)} at location {box}"
)
2023-01-03 06:44:07 +00:00
```
</details>