Metadata-Version: 2.4
Name: acmenra-yolo
Version: 0.2.0.1
Summary: Ultralytics YOLO backend plugin for acmenra-cv
Author-email: "acmenra.studio" <hello@acmenra.ru>
License: AGPL-3.0-or-later
Project-URL: Homepage, https://acmenra.studio
Project-URL: Documentation, https://github.com/Acmenra/acmenra-yolo#readme
Project-URL: Repository, https://github.com/Acmenra/acmenra-yolo
Keywords: acmenra-cv,yolo,ultralytics,computer-vision,object-detection,backend-plugin
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: acmenra-cv>=0.3.3.1
Requires-Dist: ultralytics>=8.0.0
Requires-Dist: torch>=1.8.0
Requires-Dist: opencv-python>=4.5.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"
Dynamic: license-file

# acmenra-yolo

> Acmenra backend for YOLO models. Plugs into acmenra-cv. Ultralytics is the engine, not the product.

[![PyPI](https://img.shields.io/pypi/v/acmenra-yolo.svg)](https://pypi.org/project/acmenra-yolo/)
[![Python](https://img.shields.io/pypi/pyversions/acmenra-yolo.svg)](https://pypi.org/project/acmenra-yolo/)
[![License](https://img.shields.io/pypi/l/acmenra-yolo.svg)](https://pypi.org/project/acmenra-yolo/)

```bash
# acmenra-cv >= 0.3.3.1 is required (PrecisionType)
pip install acmenra-yolo
```

**0.2.0.1 breaks 0.1.x.** `half=True/False` is gone. Pass `precision=PrecisionType.FP32`, `PrecisionType.FP16`, or `PrecisionType.AUTO`.

---

## Overview

**acmenra-yolo** is Acmenra's YOLO backend for [`acmenra-cv`](https://pypi.org/project/acmenra-cv/). It implements the `Backend` contract on top of [Ultralytics](https://github.com/ultralytics/ultralytics): detection, instance segmentation, and oriented boxes (YOLOv8 / YOLO11).

The core stays light. Projects that run OpenVINO, TensorRT, or their own weights do not install this package and do not pull `ultralytics` or `torch`.

| Feature | What it does |
| --- | --- |
| Tasks | Detect, segment, and OBB through one backend. |
| Tracking | YOLO's own tracker (BoT-SORT) when `track=True`. OBB gets a frame-local id. |
| Masks | `refined=True` turns on `retina_masks`. |
| Precision | `PrecisionType`: AUTO, FP32, FP16. New Ultralytics gets `quantize`, old gets `half`. |
| Timing | Preprocess, inference, and postprocess from YOLO's `speed` dict. |
| Devices | `DeviceType`: CPU, CUDA, MPS, TensorRT, and the rest of the enum. |
| Contract | Returns `acmenra_cv.Result`. `Tracker` and `Drawer` do not know it is YOLO. |

---

## Key points

- Implements `acmenra_cv.Backend`. `iou`, `imgsz`, `precision`, and `refined` are checked in setters.
- `boxes`, `masks`, and `obb` are routed off the result, not off a string you pass twice.
- `refined=True` builds masks at model resolution. Expect about 20–30% slower inference. Detect and OBB ignore it.
- FP16 on CPU is stored, then `predict()` runs FP32.
- Do not pass `half` or `quantize` into `predict()`. Those keys belong to `precision`.
- `track=True` keeps ids on detect and segment. OBB does not. The backend assigns `0..n-1` for that frame and logs it.

---

## Quick start

```python
from enum import Enum
import numpy as np

from acmenra_cv import DeviceType, TaskType, Tracker, PrecisionType
from acmenra_yolo import YOLOBackend
from ultralytics import YOLO


class CocoClass(Enum):
    PERSON = 0
    CAR = 2


model = YOLO("yolov8n-seg.pt")

backend = YOLOBackend(
    model=model,
    device=DeviceType.MPS,          # CPU, CUDA_0, TENSORRT, ...
    category=CocoClass,             # the Enum class, not a member
    task_type=TaskType.SEGMENT,
    threshold=0.35,
    iou=0.7,
    imgsz=640,
    precision=PrecisionType.FP32,   # FP16, or AUTO to leave it to Ultralytics
    refined=True,
)

tracker = Tracker(id=0, backend=backend, max_length=30)

frame = np.zeros((1080, 1920, 3), dtype=np.uint8)  # BGR
tracked_objects = tracker.track(frame, enable_tracking=True)

for obj in tracked_objects:
    print(obj.id, obj.instance.label.name, f"{obj.instance.conf:.2f}")
    # obj.instance.box, obj.instance.polygon, obj.instance.obb
```

---

## API

### `class YOLOBackend`

YOLO implementation of `acmenra_cv.Backend`.

**Properties**

| Property | Type | Rule |
| --- | --- | --- |
| `model` | `YOLO` | Replacing it sets `_is_update`. |
| `iou` | `float` | `[0.0, 1.0]`. |
| `imgsz` | `int` | `> 0`. Multiple of 32 is a YOLO habit, not checked here. |
| `precision` | `PrecisionType` | `AUTO`, `FP32`, `FP16` only. Anything else in the enum raises `ValueError`. |
| `refined` | `bool` | Maps to `retina_masks`. Segment only. |

**Methods**

| Method | Returns | Notes |
| --- | --- | --- |
| `predict()` | `Result` | `max_det`, `track`, `verbose`, plus Ultralytics `*args/**kwargs`. Managed keys win: `source`, `device`, `conf`, `imgsz`, `iou`, `retina_masks`, `persist`. **Needs tests.** |
| `_process_bbox_results()` | `List[Instance]` | Detect. **Needs tests.** |
| `_process_obb_results()` | `List[Instance]` | Pixels → `[0.0, 1.0]` via `normalize_xywhr`. **Needs tests.** |
| `_process_seg_results()` | `List[Instance]` | `Polygon.from_xyn`. **Needs tests.** |
| `normalize_xywhr()` | `list[float]` | `[cx, cy, w, h, angle_rad]`. Angle stays in radians. |
| `_precision_kwargs()` | `dict` | `AUTO` → `{}`. New Ultralytics → `quantize=16/32`. Old → `half`. CPU + FP16 → FP32. |

`Result.width` / `Result.height` come from `orig_shape` as `(height, width)`.

---

## Requirements

```txt
acmenra-cv>=0.3.3.1
ultralytics>=8.0.0
torch>=1.8.0
opencv-python>=4.5.0
```

Dev:

```txt
pytest>=7.0.0
black>=23.0.0
mypy>=1.0.0
```

---

## Testing

Setters, `__str__`, and `__repr__` are covered with DDT and a mocked `YOLO`. `predict()` is not.

```bash
pytest tests/
pytest tests/inference/backends/
```

Covered:

- Type and range checks for `device`, `category`, `task_type`, `threshold`, `iou`, `imgsz`, `precision`, `refined`, `model`
- `PrecisionType` outside `YOLOBackend.supported` (skipped if the enum has no extra members)
- 90 constructor fixtures, including FP16 stored on CPU

Not covered: `predict()`, mask / box / obb conversion, and the `half` vs `quantize` branch.

---

## License

GNU Affero General Public License v3 or later (AGPL-3.0-or-later), same as acmenra-cv. See [LICENSE](LICENSE).

---

## Links

- PyPI: https://pypi.org/project/acmenra-yolo/
- acmenra-cv: https://pypi.org/project/acmenra-cv/
- Source: https://github.com/Acmenra/acmenra-yolo
- Issues: https://github.com/Acmenra/acmenra-yolo/issues

---

acmenra.studio. The box runs the model. This package only talks to it.
