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

Code before:
        """
        Creates a Detections instance from a [YOLOv8](https://github.com/ultralytics/ultralytics) inference result.

        Args:
            yolov8_results (ultralytics.yolo.engine.results.Results): The output Results instance from YOLOv8

        Returns:
            Detections: A new Detections object.

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

            >>> image = cv2.imread(SOURCE_IMAGE_PATH)
            >>> model = YOLO('yolov8s.pt')
            >>> result = model(image)[0]
            >>> detections = sv.Detections.from_yolov8(result)
            ```
        """
        masks = None
        if yolov8_results.masks:
            masks = yolov8_results.masks.masks.cpu().numpy()  # masks, (N, H, W)
            masks = np.moveaxis(masks, 0, -1)  # masks, (H, W, N)
            masks = cv2.resize(masks, (yolov8_results.masks.orig_shape[1], yolov8_results.masks.orig_shape[0]))
            masks = np.moveaxis(masks, -1, 0)  # masks, (N, H, W)
        return cls(
            xyxy=yolov8_results.boxes.xyxy.cpu().numpy(),
            confidence=yolov8_results.boxes.conf.cpu().numpy(),
            class_id=yolov8_results.boxes.cls.cpu().numpy().astype(int),
            mask=masks
        )

    @classmethod
    def from_yolo_nas(cls, yolo_nas_results) -> Detections:
        """
        Creates a Detections instance from a [YOLO-NAS](https://github.com/Deci-AI/super-gradients/blob/master/YOLONAS.md) inference result.

        Args:
            yolo_nas_results (super_gradients.training.models.prediction_results.ImageDetectionPrediction): The output Results instance from YOLO-NAS

        Returns:
            Detections: A new Detections object.

        Example:
            ```python
            >>> import cv2
            >>> from super_gradients.training import models
            >>> import supervision as sv


Review comment (suggestion):
When I run the code I get:

```
0: 640x384 1 person, 1 car, 1 dog, 1 backpack, 1 handbag, 153.3ms
Speed: 15.4ms preprocess, 153.3ms inference, 37.7ms postprocess per image at shape (1, 3, 640, 640)
WARNING ⚠️ 'Masks.masks' is deprecated. Use 'Masks.data' instead.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
[<ipython-input-10-6c1542ea3727>](https://localhost:8080/#) in <cell line: 2>()
      1 results = model.predict(source=image)[0]
----> 2 detections = sv.Detections.from_yolov8(results)

[/content/supervision/supervision/detection/core.py](https://localhost:8080/#) in from_yolov8(cls, yolov8_results)
    192             masks = yolov8_results.masks.masks.cpu().numpy()  # masks, (N, H, W)
    193             masks = np.moveaxis(masks, 0, -1)  # masks, (H, W, N)
--> 194             masks = cv2.resize(masks, (yolov8_results.masks.orig_shape[1], yolov8_results.masks.orig_shape[0]))
    195             masks = np.moveaxis(masks, -1, 0)  # masks, (N, H, W)
    196         return cls(

NameError: name 'cv2' is not defined
```

Code after:
        """
        Creates a Detections instance from a [YOLOv8](https://github.com/ultralytics/ultralytics) inference result.

        Args:
            yolov8_results (ultralytics.yolo.engine.results.Results): The output Results instance from YOLOv8

        Returns:
            Detections: A new Detections object.

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

            >>> image = cv2.imread(SOURCE_IMAGE_PATH)
            >>> model = YOLO('yolov8s.pt')
            >>> result = model(image)[0]
            >>> detections = sv.Detections.from_yolov8(result)
            ```
        """
        mask_maps = None
        if yolov8_results.masks:
            orig_shape = yolov8_results.orig_shape
            inference_shape = tuple(yolov8_results.masks.data.shape[1:])
            masks = yolov8_results.masks.data.cpu().numpy()
            # calculate pad and gain
            pad = (0, 0)
            gain = 0
            if inference_shape != orig_shape:
                gain = min(inference_shape[0] / orig_shape[0], inference_shape[1] / orig_shape[1])  # gain  = old / new
                pad = (inference_shape[1] - orig_shape[1] * gain) / 2, (inference_shape[0] - orig_shape[0] * gain) / 2

            top, left = int(pad[1]), int(pad[0])  # y, x
            bottom, right = int(inference_shape[0] - pad[1]), int(inference_shape[1] - pad[0])

            mask_maps = []
            for i in range(masks.shape[0]):
                mask = masks[i]
                mask = mask[top:bottom, left:right]
                mask = cv2.resize(mask, (orig_shape[1], orig_shape[0]))
                mask_maps.append(mask)
            mask_maps = np.asarray(mask_maps)
        return cls(
            xyxy=yolov8_results.boxes.xyxy.cpu().numpy(),
            confidence=yolov8_results.boxes.conf.cpu().numpy(),
            class_id=yolov8_results.boxes.cls.cpu().numpy().astype(int),
            mask=mask_maps
        )

    @classmethod