annotator.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. from pathlib import Path
  3. from ultralytics import SAM, YOLO
  4. def auto_annotate(
  5. data,
  6. det_model="yolo11x.pt",
  7. sam_model="sam_b.pt",
  8. device="",
  9. conf=0.25,
  10. iou=0.45,
  11. imgsz=640,
  12. max_det=300,
  13. classes=None,
  14. output_dir=None,
  15. ):
  16. """
  17. Automatically annotates images using a YOLO object detection model and a SAM segmentation model.
  18. This function processes images in a specified directory, detects objects using a YOLO model, and then generates
  19. segmentation masks using a SAM model. The resulting annotations are saved as text files.
  20. Args:
  21. data (str): Path to a folder containing images to be annotated.
  22. det_model (str): Path or name of the pre-trained YOLO detection model.
  23. sam_model (str): Path or name of the pre-trained SAM segmentation model.
  24. device (str): Device to run the models on (e.g., 'cpu', 'cuda', '0').
  25. conf (float): Confidence threshold for detection model; default is 0.25.
  26. iou (float): IoU threshold for filtering overlapping boxes in detection results; default is 0.45.
  27. imgsz (int): Input image resize dimension; default is 640.
  28. max_det (int): Limits detections per image to control outputs in dense scenes.
  29. classes (list): Filters predictions to specified class IDs, returning only relevant detections.
  30. output_dir (str | None): Directory to save the annotated results. If None, a default directory is created.
  31. Examples:
  32. >>> from ultralytics.data.annotator import auto_annotate
  33. >>> auto_annotate(data="ultralytics/assets", det_model="yolo11n.pt", sam_model="mobile_sam.pt")
  34. Notes:
  35. - The function creates a new directory for output if not specified.
  36. - Annotation results are saved as text files with the same names as the input images.
  37. - Each line in the output text file represents a detected object with its class ID and segmentation points.
  38. """
  39. det_model = YOLO(det_model)
  40. sam_model = SAM(sam_model)
  41. data = Path(data)
  42. if not output_dir:
  43. output_dir = data.parent / f"{data.stem}_auto_annotate_labels"
  44. Path(output_dir).mkdir(exist_ok=True, parents=True)
  45. det_results = det_model(
  46. data, stream=True, device=device, conf=conf, iou=iou, imgsz=imgsz, max_det=max_det, classes=classes
  47. )
  48. for result in det_results:
  49. class_ids = result.boxes.cls.int().tolist() # noqa
  50. if len(class_ids):
  51. boxes = result.boxes.xyxy # Boxes object for bbox outputs
  52. sam_results = sam_model(result.orig_img, bboxes=boxes, verbose=False, save=False, device=device)
  53. segments = sam_results[0].masks.xyn # noqa
  54. with open(f"{Path(output_dir) / Path(result.path).stem}.txt", "w") as f:
  55. for i in range(len(segments)):
  56. s = segments[i]
  57. if len(s) == 0:
  58. continue
  59. segment = map(str, segments[i].reshape(-1).tolist())
  60. f.write(f"{class_ids[i]} " + " ".join(segment) + "\n")