Repository: roboflow/supervision
File: supervision/dataset/formats/yolo.py (Python)

Code before:
    lines: List[str], resolution_wh: Tuple[int, int], with_masks: bool, is_obb: bool
) -> Detections:
    if len(lines) == 0:
        return Detections.empty()

    class_id, relative_xyxy, relative_polygon, relative_xyxyxyxy = [], [], [], []
    w, h = resolution_wh
    for line in lines:
        values = line.split()
        class_id.append(int(values[0]))
        if len(values) == 5:
            box = _parse_box(values=values[1:])
            relative_xyxy.append(box)
            if with_masks:
                relative_polygon.append(_box_to_polygon(box=box))
        elif len(values) > 5:
            if is_obb:
                polygon = _parse_polygon(values=values[1:])
                relative_xyxyxyxy.append(np.array(values[1:]))
                if with_masks:
                    relative_polygon.append(polygon)
            else:
                polygon = _parse_polygon(values=values[1:])
                relative_xyxy.append(polygon_to_xyxy(polygon=polygon))
                if with_masks:
                    relative_polygon.append(polygon)

    class_id = np.array(class_id, dtype=int)
    relative_xyxy = np.array(relative_xyxy, dtype=np.float32)
    relative_xyxyxyxy = np.array(relative_xyxyxyxy, dtype=np.float32)
    xyxy = relative_xyxy * np.array([w, h, w, h], dtype=np.float32)
    xyxyxyxy = relative_xyxyxyxy * np.array([w, h, w, h], dtype=np.float32)

    data = {ORIENTED_BOX_COORDINATES: xyxyxyxy}

    if not with_masks:
        if is_obb:
            return Detections(class_id=class_id, data=data)
        return Detections(class_id=class_id, xyxy=xyxy)

    polygons = [
        (polygon * np.array(resolution_wh)).astype(int) for polygon in relative_polygon
    ]
    mask = _polygons_to_masks(polygons=polygons, resolution_wh=resolution_wh)
    return (
        Detections(class_id=class_id, data=data, mask=mask)
        if is_obb
        else Detections(class_id=class_id, xyxy=xyxy, mask=mask)
    )



Review comment (suggestion):
If we're doing the same operations inside both `if` branches, let's move them outside.

```
polygon = _parse_polygon(values=values[1:])
if with_masks:
        relative_polygon.append(polygon)
```

can be moved above `if is_obb`


Code after:
    lines: List[str],
    resolution_wh: Tuple[int, int],
    with_masks: bool,
    is_obb: bool = False,
) -> Detections:
    if len(lines) == 0:
        return Detections.empty()

    class_id, relative_xyxy, relative_polygon, relative_xyxyxyxy = [], [], [], []
    w, h = resolution_wh
    for line in lines:
        values = line.split()
        class_id.append(int(values[0]))
        if len(values) == 5:
            box = _parse_box(values=values[1:])
            relative_xyxy.append(box)
            if with_masks:
                relative_polygon.append(_box_to_polygon(box=box))
        elif len(values) > 5:
            polygon = _parse_polygon(values=values[1:])
            relative_xyxy.append(polygon_to_xyxy(polygon=polygon))
            if is_obb:
                relative_xyxyxyxy.append(np.array(values[1:]))
            if with_masks:
                relative_polygon.append(polygon)

    class_id = np.array(class_id, dtype=int)
    relative_xyxy = np.array(relative_xyxy, dtype=np.float32)
    xyxy = relative_xyxy * np.array([w, h, w, h], dtype=np.float32)
    data = {}

    if is_obb:
        relative_xyxyxyxy = np.array(relative_xyxyxyxy, dtype=np.float32)
        xyxyxyxy = relative_xyxyxyxy.reshape(-1, 4, 2)
        xyxyxyxy *= np.array([w, h], dtype=np.float32)
        data[ORIENTED_BOX_COORDINATES] = xyxyxyxy

    if not with_masks:
        return Detections(class_id=class_id, xyxy=xyxy, data=data)

    polygons = [
        (polygon * np.array(resolution_wh)).astype(int) for polygon in relative_polygon
    ]
    mask = _polygons_to_masks(polygons=polygons, resolution_wh=resolution_wh)
    return Detections(class_id=class_id, xyxy=xyxy, data=data, mask=mask)


def load_yolo_annotations(
    images_directory_path: str,
    annotations_directory_path: str,
    data_yaml_path: str,