supervision icon indicating copy to clipboard operation
supervision copied to clipboard

[Detections] extend `from_transformers` with `class_names` support

Open SkalskiP opened this issue 1 year ago • 3 comments

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)

basic-annotation

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! 🙏🏻

SkalskiP avatar Mar 25 '24 13:03 SkalskiP

I'd like to try doing this

jeslinpjames avatar Mar 25 '24 17:03 jeslinpjames

@jeslinpjames I assigned to you and thanks for PR already

onuralpszr avatar Mar 25 '24 21:03 onuralpszr

@onuralpszr Thank you! Excited to contribute further.

jeslinpjames avatar Mar 26 '24 01:03 jeslinpjames

Change implemented via https://github.com/roboflow/supervision/pull/1046. We are closing the issue.

SkalskiP avatar Apr 05 '24 10:04 SkalskiP