Update to image processor
This commit is contained in:
parent
cc12a6ee9d
commit
c6715f5703
15
README.md
15
README.md
|
@ -37,7 +37,7 @@ You can use the raw model for object detection. See the [model hub](https://hugg
|
||||||
Here is how to use this model:
|
Here is how to use this model:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from transformers import DetrFeatureExtractor, DetrForObjectDetection
|
from transformers import DetrImageProcessor, DetrForObjectDetection
|
||||||
import torch
|
import torch
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import requests
|
import requests
|
||||||
|
@ -45,24 +45,23 @@ import requests
|
||||||
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
||||||
image = Image.open(requests.get(url, stream=True).raw)
|
image = Image.open(requests.get(url, stream=True).raw)
|
||||||
|
|
||||||
feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-101")
|
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-101")
|
||||||
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-101")
|
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-101")
|
||||||
|
|
||||||
inputs = feature_extractor(images=image, return_tensors="pt")
|
inputs = processor(images=image, return_tensors="pt")
|
||||||
outputs = model(**inputs)
|
outputs = model(**inputs)
|
||||||
|
|
||||||
# convert outputs (bounding boxes and class logits) to COCO API
|
# convert outputs (bounding boxes and class logits) to COCO API
|
||||||
|
# let's only keep detections with score > 0.9
|
||||||
target_sizes = torch.tensor([image.size[::-1]])
|
target_sizes = torch.tensor([image.size[::-1]])
|
||||||
results = feature_extractor.post_process(outputs, target_sizes=target_sizes)[0]
|
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"]):
|
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
||||||
box = [round(i, 2) for i in box.tolist()]
|
box = [round(i, 2) for i in box.tolist()]
|
||||||
# let's only keep detections with score > 0.9
|
print(
|
||||||
if score > 0.9:
|
|
||||||
print(
|
|
||||||
f"Detected {model.config.id2label[label.item()]} with confidence "
|
f"Detected {model.config.id2label[label.item()]} with confidence "
|
||||||
f"{round(score.item(), 3)} at location {box}"
|
f"{round(score.item(), 3)} at location {box}"
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
This should output (something along the lines of):
|
This should output (something along the lines of):
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue