Repository: roboflow/supervision
File: supervision/detection/core.py (Python)

Code before:
            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

        class_ids = transformers_results["labels"].cpu().detach().numpy().astype(int)
        data = {}
        if id2label is not None:
            class_names = np.array([id2label[class_id] for class_id in class_ids])
            data[CLASS_NAME_DATA_FIELD] = class_names
        if "boxes" in transformers_results:
            return cls(
                xyxy=transformers_results["boxes"].cpu().detach().numpy(),
                confidence=transformers_results["scores"].cpu().detach().numpy(),
                class_id=class_ids,
                data=data,
            )
        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=class_ids,
            data=data,
        )

    @classmethod
    def from_detectron2(cls, detectron2_results) -> Detections:
        """
        Create a Detections object from the
        [Detectron2](https://github.com/facebookresearch/detectron2) inference result.

        Args:
            detectron2_results: The output of a
                Detectron2 model containing instances with prediction data.

        Returns:
            (Detections): A Detections object containing the bounding boxes,
                class IDs, and confidences of the predictions.

        Example:
            ```python
            import cv2

Review comment (suggestion):
While we're at it, let's make the code more defensive.

I've reviewed the [image_processing_detr.py](https://github.com/huggingface/transformers/blob/fed27ffc7ec62837dca9bbfc83442eb3678ee026/src/transformers/models/detr/image_processing_detr.py#L744) in `transformers` and it turns out, with instance or panoptic segmentation `transformers_results` might not have `boxes`, `masks` or `labels`.

Let's add an `elif "masks" in transformers_results:` here and at the end - `raise NotImplementedError("Only object detection and semantic segmentation results are supported")`.

Code after:
            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(<SOURCE_IMAGE_PATH>)
            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(
                transformers_results=results,
                id2label=model.config.id2label
            )
            ```
        """  # noqa: E501 // docs

        class_ids = transformers_results["labels"].cpu().detach().numpy().astype(int)
        data = {}
        if id2label is not None:
            class_names = np.array([id2label[class_id] for class_id in class_ids])
            data[CLASS_NAME_DATA_FIELD] = class_names
        if "boxes" in transformers_results:
            return cls(
                xyxy=transformers_results["boxes"].cpu().detach().numpy(),
                confidence=transformers_results["scores"].cpu().detach().numpy(),
                class_id=class_ids,
                data=data,
            )
        elif "masks" in transformers_results:
            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=class_ids,
                data=data,
            )
        else:
            raise NotImplementedError(
                "Only object detection and semantic segmentation results are supported."
            )

    @classmethod