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

Code before:
    def from_mediapipe(
        cls, mediapipe_results, resolution: Tuple[int, int]
    ) -> KeyPoints:
        """
        Creates a KeyPoints instance from a
            [Pose landmark detection](https://ai.google.dev/edge/mediapipe/)
                inference result.

        Args:
            mediapipe_results (mediapipe.python.solution_base.SolutionOutputs):
                The output Results from Mediapipe
            resolution (Tuple[int, int]): image resolution (w, h) since mediapipe
                only provides normalized coordinates

        Returns:
            KeyPoints: A new KeyPoints object.

        Example:
            ```python
            import cv2
            import mediapipe as mp
            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

Review comment (refactor):
Let's join this line with `mp_pose = mp.solutions.pose`. If it's too long, let's break the arguments into separate lines.

Code after:
                api_url="https://detect.roboflow.com",
                api_key=<ROBOFLOW_API_KEY>
            )

            result = client.infer(image, model_id=<POSE_MODEL_ID>)
            key_points = sv.KeyPoints.from_inference(result)
            ```
        """
        if isinstance(inference_result, list):
            raise ValueError(
                "from_inference() operates on a single result at a time."
                "You can retrieve it like so:  inference_result = model.infer(image)[0]"
            )

        with suppress(AttributeError):
            inference_result = inference_result.dict(exclude_none=True, by_alias=True)

        if not inference_result.get("predictions"):
            return cls.empty()

        xy = []
        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:
        """