from acmenra_cv.inference import YOLOBackend, Result, Timing
from acmenra_cv.tracker import Tracker
from acmenra_cv.render import Drawer, Style
from acmenra_cv.instance import DeviceType, TaskType
from ultralytics import YOLO
import cv2
from enum import Enum
# 1. Определяем категории
class CocoClass(Enum):
PERSON = 0
CAR = 2
# 2. Инициализация
model = YOLO("yolov8n.pt")
backend = YOLOBackend(
model=model,
device=DeviceType.CPU,
category=CocoClass,
task_type=TaskType.DETECT,
threshold=0.5,
iou=0.7,
imgsz=640,
)
tracker = Tracker(id=0, backend=backend, max_length=50)
style = Style(
palette=[(255, 0, 0), (0, 255, 0), (0, 0, 255)],
show=True,
)
drawer = Drawer(style=style)
# 3. Обработка кадра
frame = cv2.imread("image.jpg")
tracked_objects = tracker.track(frame, enable_tracking=True)
# 4. Создание Result контейнера
timing = Timing(preprocess=1.5, prediction=15.2, postprocess=2.1)
result = Result(
instances=[obj.instance for obj in tracked_objects],
timing=timing,
width=frame.shape[1],
height=frame.shape[0],
depth=1,
device=DeviceType.CPU,
category=CocoClass,
)
# 5. Визуализация
output = drawer.draw_instances(
frame=frame,
tracked_objects=tracked_objects,
is_box=True,
is_trajectory=True,
)
# 6. Использование Result API
print(f"Detected {len(result)} objects")
for instance in result:
print(f" {instance.label.name}: {instance.conf:.2f}")
# 7. Сериализация
result_dict = result.to_dict()
print(result_dict)
# 8. Сохранение
cv2.imwrite("output.jpg", output)