Skip to content

Référence pour ultralytics/data/annotator.py

Note

Ce fichier est disponible à l'adresse https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/data/annotator .py. Si tu repères un problème, aide à le corriger en contribuant à une Pull Request 🛠️. Merci 🙏 !



ultralytics.data.annotator.auto_annotate(data, det_model='yolov8x.pt', sam_model='sam_b.pt', device='', output_dir=None)

Annote automatiquement les images à l'aide d'un modèle de détection d'objets YOLO et d'un modèle de segmentation SAM .

Paramètres :

Nom Type Description DĂ©faut
data str

Chemin d'accès à un dossier contenant des images à annoter.

requis
det_model str

Modèle de détection de YOLO pré-entraîné. La valeur par défaut est 'yolov8x.pt'.

'yolov8x.pt'
sam_model str

Modèle de segmentation pré-entraîné SAM . La valeur par défaut est 'sam_b.pt'.

'sam_b.pt'
device str

Périphérique sur lequel les modèles sont exécutés. La valeur par défaut est une chaîne vide (CPU ou GPU, si disponible).

''
output_dir str | None | optional

Répertoire où enregistrer les résultats annotés. Par défaut, il s'agit d'un dossier "labels" dans le même répertoire que "data".

None
Exemple
from ultralytics.data.annotator import auto_annotate

auto_annotate(data='ultralytics/assets', det_model='yolov8n.pt', sam_model='mobile_sam.pt')
Code source dans ultralytics/data/annotator.py
def auto_annotate(data, det_model="yolov8x.pt", sam_model="sam_b.pt", device="", output_dir=None):
    """
    Automatically annotates images using a YOLO object detection model and a SAM segmentation model.

    Args:
        data (str): Path to a folder containing images to be annotated.
        det_model (str, optional): Pre-trained YOLO detection model. Defaults to 'yolov8x.pt'.
        sam_model (str, optional): Pre-trained SAM segmentation model. Defaults to 'sam_b.pt'.
        device (str, optional): Device to run the models on. Defaults to an empty string (CPU or GPU, if available).
        output_dir (str | None | optional): Directory to save the annotated results.
            Defaults to a 'labels' folder in the same directory as 'data'.

    Example:
        ```python
        from ultralytics.data.annotator import auto_annotate

        auto_annotate(data='ultralytics/assets', det_model='yolov8n.pt', sam_model='mobile_sam.pt')
        ```
    """
    det_model = YOLO(det_model)
    sam_model = SAM(sam_model)

    data = Path(data)
    if not output_dir:
        output_dir = data.parent / f"{data.stem}_auto_annotate_labels"
    Path(output_dir).mkdir(exist_ok=True, parents=True)

    det_results = det_model(data, stream=True, device=device)

    for result in det_results:
        class_ids = result.boxes.cls.int().tolist()  # noqa
        if len(class_ids):
            boxes = result.boxes.xyxy  # Boxes object for bbox outputs
            sam_results = sam_model(result.orig_img, bboxes=boxes, verbose=False, save=False, device=device)
            segments = sam_results[0].masks.xyn  # noqa

            with open(f"{Path(output_dir) / Path(result.path).stem}.txt", "w") as f:
                for i in range(len(segments)):
                    s = segments[i]
                    if len(s) == 0:
                        continue
                    segment = map(str, segments[i].reshape(-1).tolist())
                    f.write(f"{class_ids[i]} " + " ".join(segment) + "\n")





Créé le 2023-11-12, Mis à jour le 2024-05-08
Auteurs : Burhan-Q (1), glenn-jocher (3)