
    j                    J    d dl mZ d dlmZ d dlmZmZ 	 	 	 	 	 	 	 	 	 dd dZdS )!    )annotations)Path)SAMYOLO
yolo26x.ptsam_b.pt       ??  ,  Ndata
str | Path	det_modelstr	sam_modeldeviceconffloatiouimgszintmax_detclasseslist[int] | None
output_dirstr | Path | NonereturnNonec
           
        t          |          }t          |          }t          |           } |	s| j        | j         dz  }	t          |	                              dd            || d||||||          }
|
D ]X}|j        j                                        	                                x}r"|j        j
        } ||j        |dd|          }|d         j        j        }t          t          |	          t          |j                  j        z   dd	d
          5 }t!          |          D ]\  }}|                                rqt%          t&          |                    d          	                                          }|                    ||          dd                    |          z   dz              	 ddd           n# 1 swxY w Y   ZdS )a  Automatically annotate images using a YOLO object detection model and a SAM segmentation model.

    This function processes images in a specified directory, detects objects using a YOLO model, and then generates
    segmentation masks using a SAM model. The resulting annotations are saved as text files in YOLO format.

    Args:
        data (str | Path): Path to a folder containing images to be annotated.
        det_model (str): Path or name of the pre-trained YOLO detection model.
        sam_model (str): Path or name of the pre-trained SAM segmentation model.
        device (str): Device to run the models on (e.g., 'cpu', 'cuda', '0'). Empty string for auto-selection.
        conf (float): Confidence threshold for detection model.
        iou (float): IoU threshold for filtering overlapping boxes in detection results.
        imgsz (int): Input image resize dimension.
        max_det (int): Maximum number of detections per image.
        classes (list[int], optional): Filter predictions to specified class IDs, returning only relevant detections.
        output_dir (str | Path, optional): Directory to save the annotated results. If None, creates a default directory
            based on the input data path.

    Examples:
        >>> from ultralytics.data.annotator import auto_annotate
        >>> auto_annotate(data="ultralytics/assets", det_model="yolo26n.pt", sam_model="mobile_sam.pt")
    _auto_annotate_labelsT)exist_okparents)streamr   r   r   r   r   r   F)bboxesverbosesaver   r   z.txtwzutf-8)encoding 
N)r   r   r   parentstemmkdirboxesclsr   tolistxyxyorig_imgmasksxynopenpath	enumerateanymapr   reshapewritejoin)r   r   r   r   r   r   r   r   r   r   det_resultsresult	class_idsr0   sam_resultssegmentsfissegments                       _/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/data/annotator.pyauto_annotaterI   
   s=   D YIII::D G[di#F#F#FF
D$777)T&tESZdk  K  
O 
O(,,..557779 		OL%E#)FOE5W\eklllK"1~+/Hj))D,=,=,BBHHH#X_``` Ode%h// O ODAquuww O"%c199R==+?+?+A+A"B"B9Q< 2 2 2SXXg5F5F F MNNNOO O O O O O O O O O O O O O O
O 
Os    BGG	G	)	r   r   r	   r
   r   r   r   NN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )
__future__r   pathlibr   ultralyticsr   r   rI        rH   <module>rO      s    # " " " " "       ! ! ! ! ! ! ! !
 " $$(8O 8O 8O 8O 8O 8O 8OrN   