From 4ad78becb674c9c0d82b0961a64aa9d3fefeb455 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Thu, 28 Mar 2024 16:08:05 +0100 Subject: [PATCH] initial comment --- docs/how_to/detect_and_annotate.md | 38 ++++++++++++++++++++++ supervision/detection/core.py | 51 ++++++++++++++++++++---------- 2 files changed, 73 insertions(+), 16 deletions(-) diff --git a/docs/how_to/detect_and_annotate.md b/docs/how_to/detect_and_annotate.md index 45d0487a..6aaabff3 100644 --- a/docs/how_to/detect_and_annotate.md +++ b/docs/how_to/detect_and_annotate.md @@ -362,4 +362,42 @@ that will allow you to draw masks instead of boxes. scene=annotated_image, detections=detections) ``` +=== "Transformers" + + ```python + import torch + import supervision as sv + from PIL import Image + from transformers import DetrImageProcessor, DetrForSegmentation + + processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") + model = DetrForSegmentation.from_pretrained("facebook/detr-resnet-50") + + image = Image.open() + 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) + + mask_annotator = sv.MaskAnnotator() + label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) + + labels = [ + f"{model.config.id2label[class_id]} {confidence:.2f}" + for class_id, confidence + in zip(detections.class_id, detections.confidence) + ] + + annotated_image = mask_annotator.annotate( + scene=image, detections=detections) + annotated_image = label_annotator.annotate( + scene=annotated_image, detections=detections) + ``` + ![segmentation-annotation](https://media.roboflow.com/supervision_detect_and_annotate_example_3.png) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 60ff07f3..51616335 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -396,25 +396,44 @@ class Detections: Returns: Detections: A new Detections object. - """ - boxes = transformers_results.get("boxes") - # If the boxes key is in the transformers_results then we know it's an - # object detection result. Else, we can assume it's a segmentation model - if boxes: + Example: + ```python + 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() + 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) + ``` + """ # noqa: E501 // docs + + if "boxes" in transformers_results: return cls( - xyxy=transformers_results["boxes"].cpu().numpy(), - confidence=transformers_results["scores"].cpu().numpy(), - class_id=transformers_results["labels"].cpu().numpy().astype(int), - ) - else: - masks = transformers_results["masks"].cpu().numpy().astype(bool) - return cls( - xyxy=mask_to_xyxy(masks), - mask=masks, - confidence=transformers_results["scores"].cpu().numpy(), - class_id=transformers_results["labels"].cpu().numpy().astype(int), + xyxy=transformers_results["boxes"].cpu().detach().numpy(), + confidence=transformers_results["scores"].cpu().detach().numpy(), + class_id=transformers_results["labels"].cpu().detach().numpy().astype(int), ) + masks = transformers_results["masks"].cpu().detach().numpy().astype(bool) + return cls( + xyxy=mask_to_xyxy(masks), + mask=masks, + confidence=transformers_results["scores"].cpu().detach().numpy(), + class_id=transformers_results["labels"].cpu().detach().numpy().astype(int), + ) @classmethod def from_detectron2(cls, detectron2_results) -> Detections: