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

Code before:

            >>> detections = sv.Detections.from_roboflow(roboflow_result, class_list)
            ```
        """
        xyxy = []
        confidence = []
        class_id = []
        masks = []

        for prediction in roboflow_result["predictions"]:
            x = prediction["x"]
            y = prediction["y"]
            width = prediction["width"]
            height = prediction["height"]
            x_min = x - width / 2
            y_min = y - height / 2
            x_max = x_min + width
            y_max = y_min + height
            xyxy.append([x_min, y_min, x_max, y_max])
            class_id.append(class_list.index(prediction["class"]))
            confidence.append(prediction["confidence"])

            if prediction.get("points"):
                points = prediction["points"]

                mask = np.zeros((int(height), int(width)), dtype=np.uint8)
                points = np.array(
                    [(p["x"], p["y"]) for p in points], dtype=np.int32
                ).reshape((-1, 1, 2))

                cv2.fillPoly(mask, [points], 1)

                mask = mask.astype(bool)

                masks.append(mask)

        if len(mask) > 0:
            return Detections(
                xyxy=np.array(xyxy),
                confidence=np.array(confidence),
                class_id=np.array(class_id).astype(int),
                mask=np.array(mask),
            )

        return Detections(
            xyxy=np.array(xyxy),
            confidence=np.array(confidence),
            class_id=np.array(class_id).astype(int),
        )

    @classmethod

Review comment (suggestion):
Most of that logic can be replaced with `polygon_to_mask` function that can create a mask given `polygon`. Please use it. You can find it in `supervision.detection.utils`.

Code after:
            ...             "confidence": 0.9
            ...         },
            ...         # ... more predictions ...
            ...     ]
            ... }
            >>> class_list = ["person", "car", "dog"]

            >>> detections = sv.Detections.from_roboflow(roboflow_result, class_list)
            ```
        """
        xyxy, confidence, class_id, masks = process_roboflow_result(
            roboflow_result=roboflow_result, class_list=class_list
        )
        return Detections(
            xyxy=xyxy,
            confidence=confidence,
            class_id=class_id,
            mask=masks,
        )

    @classmethod
    def from_sam(cls, sam_result: List[dict]) -> Detections:
        """
        Creates a Detections instance from [Segment Anything Model](https://github.com/facebookresearch/segment-anything) inference result.

        Args:
            sam_result (List[dict]): The output Results instance from SAM

        Returns:
            Detections: A new Detections object.

        Example:
            ```python
            >>> import supervision as sv
            >>> from segment_anything import sam_model_registry, SamAutomaticMaskGenerator

            >>> sam = sam_model_registry[MODEL_TYPE](checkpoint=CHECKPOINT_PATH).to(device=DEVICE)
            >>> mask_generator = SamAutomaticMaskGenerator(sam)
            >>> sam_result = mask_generator.generate(IMAGE)
            >>> detections = sv.Detections.from_sam(sam_result=sam_result)
            ```
        """
        sorted_generated_masks = sorted(
            sam_result, key=lambda x: x["area"], reverse=True
        )

        xywh = np.array([mask["bbox"] for mask in sorted_generated_masks])
        mask = np.array([mask["segmentation"] for mask in sorted_generated_masks])

        return Detections(xyxy=xywh_to_xyxy(boxes_xywh=xywh), mask=mask)