supervision
supervision copied to clipboard
[Detections] extend `from_transformers` with `class_names` support
Description
Currently, Supervision supports class name extraction exclusively for Inference and Ultralytics libraries. Let's enhance from_transformers by incorporating support for Transformers. Take a look at from_inference and from_ultralytics for more reference.
API
The code below should annotate the resulting image with class names.
def from_transformers(cls, transformers_results: dict, id2label: Dict[int, str]) -> Detections:
pass
import torch
import supervision as sv
from PIL import Image
from transformers import DetrImageProcessor, DetrForObjectDetection
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
image = Image.open(<PATH TO IMAGE>)
inputs = processor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
width, height = image.size
target_size = torch.tensor([[height, width]])
results = processor.post_process_object_detection(
outputs=outputs, target_sizes=target_size)[0]
detections = sv.Detections.from_transformers(results, id2label=model.config.id2label)
bounding_box_annotator = sv.BoundingBoxAnnotator()
label_annotator = sv.LabelAnnotator()
annotated_image = bounding_box_annotator.annotate(
scene=image, detections=detections)
annotated_image = label_annotator.annotate(
scene=annotated_image, detections=detections)

Additional
- Transformers DETR Docs
- Note: Please share a Google Colab with minimal code to test the new feature. We know it's additional work, but it will speed up the review process. The reviewer must test each change. Setting up a local environment to do this is time-consuming. Please ensure that Google Colab can be accessed without any issues (make it public). Thank you! 🙏🏻
I'd like to try doing this
@jeslinpjames I assigned to you and thanks for PR already
@onuralpszr Thank you! Excited to contribute further.
Change implemented via https://github.com/roboflow/supervision/pull/1046. We are closing the issue.