annotator.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. from pathlib import Path
  3. from ultralytics import SAM, YOLO
  4. def auto_annotate(data, det_model="yolov8x.pt", sam_model="sam_b.pt", device="", output_dir=None):
  5. """
  6. Automatically annotates images using a YOLO object detection model and a SAM segmentation model.
  7. Args:
  8. data (str): Path to a folder containing images to be annotated.
  9. det_model (str, optional): Pre-trained YOLO detection model. Defaults to 'yolov8x.pt'.
  10. sam_model (str, optional): Pre-trained SAM segmentation model. Defaults to 'sam_b.pt'.
  11. device (str, optional): Device to run the models on. Defaults to an empty string (CPU or GPU, if available).
  12. output_dir (str | None | optional): Directory to save the annotated results.
  13. Defaults to a 'labels' folder in the same directory as 'data'.
  14. Example:
  15. ```python
  16. from ultralytics.data.annotator import auto_annotate
  17. auto_annotate(data='ultralytics/assets', det_model='yolov8n.pt', sam_model='mobile_sam.pt')
  18. ```
  19. """
  20. det_model = YOLO(det_model)
  21. sam_model = SAM(sam_model)
  22. data = Path(data)
  23. if not output_dir:
  24. output_dir = data.parent / f"{data.stem}_auto_annotate_labels"
  25. Path(output_dir).mkdir(exist_ok=True, parents=True)
  26. det_results = det_model(data, stream=True, device=device)
  27. for result in det_results:
  28. class_ids = result.boxes.cls.int().tolist() # noqa
  29. if len(class_ids):
  30. boxes = result.boxes.xyxy # Boxes object for bbox outputs
  31. sam_results = sam_model(result.orig_img, bboxes=boxes, verbose=False, save=False, device=device)
  32. segments = sam_results[0].masks.xyn # noqa
  33. with open(f"{Path(output_dir) / Path(result.path).stem}.txt", "w") as f:
  34. for i in range(len(segments)):
  35. s = segments[i]
  36. if len(s) == 0:
  37. continue
  38. segment = map(str, segments[i].reshape(-1).tolist())
  39. f.write(f"{class_ids[i]} " + " ".join(segment) + "\n")