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

Code before:

        if "labels" in transformers_results:
            class_ids = (
                transformers_results["labels"].cpu().detach().numpy().astype(int)
            )
            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,
            )
        elif "segments_info" in transformers_results:
            segments_info = transformers_results["segments_info"]

            if "segmentation" in transformers_results:
                scores = np.array([segment["score"] for segment in segments_info])
                class_ids = np.array([segment["label_id"] for segment in segments_info])
                segmentation_array = (
                    transformers_results["segmentation"].cpu().detach().numpy()
                )
                masks = np.array(
                    [
                        (segmentation_array == segment["id"]).astype(bool)
                        for segment in segments_info
                    ]
                )

                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

                return cls(
                    xyxy=mask_to_xyxy(masks),
                    mask=masks,
                    confidence=scores,

Review comment (refactor):
Personally, I would move `segments_info = transformers_results["segments_info"]` extraction to ` if "segmentation" in transformers_results:` and `elif "png_string" in transformers_results:` blocks. We will get one indention less.

Code after:
        [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
            from detectron2.engine import DefaultPredictor
            from detectron2.config import get_cfg


            image = cv2.imread(<SOURCE_IMAGE_PATH>)
            cfg = get_cfg()
            cfg.merge_from_file(<CONFIG_PATH>)
            cfg.MODEL.WEIGHTS = <WEIGHTS_PATH>
            predictor = DefaultPredictor(cfg)

            result = predictor(image)
            detections = sv.Detections.from_detectron2(result)
            ```
        """

        return cls(
            xyxy=detectron2_results["instances"].pred_boxes.tensor.cpu().numpy(),
            confidence=detectron2_results["instances"].scores.cpu().numpy(),
            mask=detectron2_results["instances"].pred_masks.cpu().numpy()
            if hasattr(detectron2_results["instances"], "pred_masks")
            else None,
            class_id=detectron2_results["instances"]
            .pred_classes.cpu()
            .numpy()
            .astype(int),
        )

    @classmethod
    def from_inference(cls, roboflow_result: Union[dict, Any]) -> Detections:
        """
        Create a `sv.Detections` object from the [Roboflow](https://roboflow.com/)
        API inference result or the [Inference](https://inference.roboflow.com/)
        package results. This method extracts bounding boxes, class IDs,
        confidences, and class names from the Roboflow API result and encapsulates
        them into a Detections object.

        Args: