Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image post-processing for OWLv2 #30686

Merged
merged 9 commits into from
May 9, 2024
23 changes: 23 additions & 0 deletions docs/source/en/tasks/zero_shot_object_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,29 @@ boxes have the correct coordinates relative to the original image:
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_4.png" alt="Beach photo with detected objects"/>
</div>

Note that for OWLv2, if the image is not square, the coorindates in the lesser dimension are off by a ratio of the lesser dimension to the greater dimension. This is a workaround to correct the coordinates:

```py
>>> width, height = im.size
>>> width_ratio = 1
>>> height_ratio = 1

>>> if width < height:
... width_ratio = width / height
... elif height < width:
... height_ratio = height / width

>>> for box, score, label in zip(boxes, scores, labels):
... xmin, ymin, xmax, ymax = box
... xmin /= width_ratio
... ymin /= height_ratio
... xmax /= width_ratio
... ymax /= height_ratio
... draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1)
... draw.text((xmin, ymin), f"{text_queries[label]}: {round(score,2)}", fill="white")

>>> im
```
## Batch processing

You can pass multiple sets of images and text queries to search for different (or same) objects in several images.
Expand Down