import os import logging from label_studio_ml.model import LabelStudioMLBase from label_studio_ml.response import ModelResponse from control_models.base import ControlModel from control_models.choices import ChoicesModel from control_models.rectangle_labels import RectangleLabelsModel from control_models.rectangle_labels_obb import RectangleLabelsObbModel from control_models.polygon_labels import PolygonLabelsModel from control_models.brush_labels import BrushLabelsModel from control_models.keypoint_labels import KeypointLabelsModel from control_models.video_rectangle import VideoRectangleModel from control_models.timeline_labels import TimelineLabelsModel from typing import List, Dict, Optional logger = logging.getLogger(__name__) if not os.getenv("LOG_LEVEL"): logger.setLevel(logging.INFO) # Register available model classes available_model_classes = [ ChoicesModel, RectangleLabelsModel, RectangleLabelsObbModel, PolygonLabelsModel, BrushLabelsModel, KeypointLabelsModel, VideoRectangleModel, TimelineLabelsModel, ] class YOLO(LabelStudioMLBase): """Label Studio ML Backend based on Ultralytics YOLO""" def setup(self): """Configure any parameters of your model here""" self.set("model_version", "yolo") def detect_control_models(self) -> List[ControlModel]: """Detect control models based on the labeling config. Control models are used to predict regions for different control tags in the labeling config. """ control_models = [] for control in self.label_interface.controls: # skipping tags without toName if not control.to_name: logger.warning( f'{control.tag} {control.name} has no "toName" attribute, skipping it' ) continue # match control tag with available control models for model_class in available_model_classes: if model_class.is_control_matched(control): instance = model_class.create(self, control) if not instance: logger.debug( f"No instance created for {control.tag} {control.name}" ) continue if not instance.label_map: logger.error( f"No label map built for the '{control.tag}' control tag '{instance.from_name}'.\n" f"This indicates that your Label Studio config labels do not match the model's labels.\n" f"To fix this, ensure that the 'value' or 'predicted_values' attribute " f"in your Label Studio config matches one or more of these model labels.\n" f"If you don't want to use this control tag for predictions, " f'add `model_skip="true"` to it.\n' f"Examples:\n" f'