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

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

Review comment (suggestion):
Let's keep a single return and make it a bit more compact:

```python
return Detections(
    xyxy=np.array(xyxy),
    confidence=np.array(confidence),
    class_id=np.array(class_id).astype(int),
    mask=mask if mask else None,
)
```

Code after:
            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)

    @classmethod
    @deprecated(
        "Dataset loading and saving is going to be executed by supervision.dataset.core.Dataset"
    )
    def from_coco_annotations(cls, coco_annotation: dict) -> Detections:
        xyxy, class_id = [], []

        for annotation in coco_annotation:
            x_min, y_min, width, height = annotation["bbox"]
            xyxy.append([x_min, y_min, x_min + width, y_min + height])
            class_id.append(annotation["category_id"])