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

Code before:
            import supervision as sv

            mp_pose = mp.solutions.pose
            image = cv2.imread(<SOURCE_IMAGE_PATH>)
            pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
            results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
            keypoints = sv.KeyPoints.from_mediapipe(results)
            ```
        """
        if mediapipe_results.pose_landmarks is None:
            return cls.empty()

        prediction_xy = []
        prediction_confidence = []

        for landmark in mediapipe_results.pose_landmarks.landmark:
            prediction_xy.append(
                [
                    min(math.floor(landmark.x * resolution[0]), resolution[0] - 1),
                    min(math.floor(landmark.y * resolution[1]), resolution[1] - 1),
                ]
            )
            prediction_confidence.append(landmark.visibility)

        return cls(
            xy=np.array([prediction_xy], dtype=np.float32),
            confidence=np.array([prediction_confidence], dtype=np.float32),
        )

    @classmethod
    def from_ultralytics(cls, ultralytics_results) -> KeyPoints:
        """
        Creates a KeyPoints instance from a
            [YOLOv8](https://github.com/ultralytics/ultralytics) inference result.

        Args:
            ultralytics_results (ultralytics.engine.results.Keypoints):
                The output Results instance from YOLOv8

        Returns:
            KeyPoints: A new KeyPoints object.

        Example:
            ```python
            import cv2
            import supervision as sv
            from ultralytics import YOLO

            image = cv2.imread(<SOURCE_IMAGE_PATH>)
            model = YOLO('yolov8s-pose.pt')
            result = model(image)[0]

Review comment (suggestion):
Let's make sure that when no predictions are returned, the empty array is still shaped `(0, -2)`.

For example:

```
if len(prediction_xy) == 0:
  return cls(
    xy=np.empty((1, 0, 2), dtype=np.float32),
    confidence=np.empty((1, 0,), dtype=np.float32)
)
```

Code after:
        confidence = []
        class_id = []
        class_names = []

        for prediction in inference_result["predictions"]:
            prediction_xy = []
            prediction_confidence = []
            for keypoint in prediction["keypoints"]:
                prediction_xy.append([keypoint["x"], keypoint["y"]])
                prediction_confidence.append(keypoint["confidence"])
            xy.append(prediction_xy)
            confidence.append(prediction_confidence)

            class_id.append(prediction["class_id"])
            class_names.append(prediction["class"])

        data = {CLASS_NAME_DATA_FIELD: np.array(class_names)}

        return cls(
            xy=np.array(xy, dtype=np.float32),
            confidence=np.array(confidence, dtype=np.float32),
            class_id=np.array(class_id, dtype=int),
            data=data,
        )

    @classmethod
    def from_mediapipe(
        cls, mediapipe_results, resolution_wh: Tuple[int, int]
    ) -> KeyPoints:
        """
        Creates a `sv.KeyPoints` instance from a
        [MediaPipe](https://github.com/google-ai-edge/mediapipe)
        pose landmark detection inference result.

        Args:
            mediapipe_results (Union[PoseLandmarkerResult, SolutionOutputs]):
                The output results from Mediapipe. It supports both: the inference
                result `PoseLandmarker` and the legacy one from `Pose`.
            resolution_wh (Tuple[int, int]): A tuple of the form `(width, height)`
                representing the resolution of the frame.

        Returns:
            A `sv.KeyPoints` object containing the keypoint coordinates and
                confidences of each keypoint.

        !!! tip
            Before you start, download model bundles from the
            [MediaPipe website](https://ai.google.dev/edge/mediapipe/solutions/vision/pose_landmarker/index#models).

        Examples:
            ```python