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

Code before:

        return cls(
            xyxy=mmdet_results.pred_instances.bboxes.cpu().numpy(),
            confidence=mmdet_results.pred_instances.scores.cpu().numpy(),
            class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int),
        )

    @classmethod
    def from_transformers(cls, transformers_results: dict) -> Detections:
        """
        Creates a Detections instance from object detection or segmentation
        [transformer](https://github.com/huggingface/transformers) inference result.

        Returns:
            Detections: A new Detections object.
        """
        boxes = transformers_results.get("boxes")

        if boxes:
            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()
            return cls(
                xyxy=mask_to_xyxy(masks),
                mask=masks,
                confidence=transformers_results["scores"].cpu().numpy(),
                class_id=transformers_results["labels"].cpu().numpy().astype(int),
            )

    @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
            import supervision as sv

Review comment (suggestion):
```suggestion
            masks = transformers_results["masks"].cpu().numpy().astype(bool)
```

Code after:

        return cls(
            xyxy=mmdet_results.pred_instances.bboxes.cpu().numpy(),
            confidence=mmdet_results.pred_instances.scores.cpu().numpy(),
            class_id=mmdet_results.pred_instances.labels.cpu().numpy().astype(int),
        )

    @classmethod
    def from_transformers(cls, transformers_results: dict) -> Detections:
        """
        Creates a Detections instance from object detection or segmentation
        [transformer](https://github.com/huggingface/transformers) inference result.

        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:
            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),
            )

    @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