Update README.md
Browse files
README.md
CHANGED
@@ -34,7 +34,7 @@ You can use the raw model for object detection. See the [model hub](https://hugg
|
|
34 |
Here is how to use this model:
|
35 |
|
36 |
```python
|
37 |
-
from transformers import
|
38 |
import torch
|
39 |
from PIL import Image
|
40 |
import requests
|
@@ -42,24 +42,23 @@ import requests
|
|
42 |
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
43 |
image = Image.open(requests.get(url, stream=True).raw)
|
44 |
|
45 |
-
|
46 |
model = ConditionalDetrForObjectDetection.from_pretrained("microsoft/conditional-detr-resnet-50")
|
47 |
|
48 |
-
inputs =
|
49 |
outputs = model(**inputs)
|
50 |
|
51 |
# convert outputs (bounding boxes and class logits) to COCO API
|
|
|
52 |
target_sizes = torch.tensor([image.size[::-1]])
|
53 |
-
results =
|
54 |
|
55 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
56 |
box = [round(i, 2) for i in box.tolist()]
|
57 |
-
|
58 |
-
if score > 0.7:
|
59 |
-
print(
|
60 |
f"Detected {model.config.id2label[label.item()]} with confidence "
|
61 |
f"{round(score.item(), 3)} at location {box}"
|
62 |
-
|
63 |
```
|
64 |
This should output:
|
65 |
```
|
|
|
34 |
Here is how to use this model:
|
35 |
|
36 |
```python
|
37 |
+
from transformers import AutoImageProcessor, ConditionalDetrForObjectDetection
|
38 |
import torch
|
39 |
from PIL import Image
|
40 |
import requests
|
|
|
42 |
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
43 |
image = Image.open(requests.get(url, stream=True).raw)
|
44 |
|
45 |
+
processor = AutoImageProcessor.from_pretrained("microsoft/conditional-detr-resnet-50")
|
46 |
model = ConditionalDetrForObjectDetection.from_pretrained("microsoft/conditional-detr-resnet-50")
|
47 |
|
48 |
+
inputs = processor(images=image, return_tensors="pt")
|
49 |
outputs = model(**inputs)
|
50 |
|
51 |
# convert outputs (bounding boxes and class logits) to COCO API
|
52 |
+
# let's only keep detections with score > 0.7
|
53 |
target_sizes = torch.tensor([image.size[::-1]])
|
54 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
|
55 |
|
56 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
57 |
box = [round(i, 2) for i in box.tolist()]
|
58 |
+
print(
|
|
|
|
|
59 |
f"Detected {model.config.id2label[label.item()]} with confidence "
|
60 |
f"{round(score.item(), 3)} at location {box}"
|
61 |
+
)
|
62 |
```
|
63 |
This should output:
|
64 |
```
|