
    /jZ                       d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlZddlZddlZddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZmZmZmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z( dZ) G d d          Z*dS )a  
Run prediction on images, videos, directories, globs, YouTube, webcam, streams, etc.

Usage - sources:
    $ yolo mode=predict model=yolo26n.pt source=0                               # webcam
                                                img.jpg                         # image
                                                vid.mp4                         # video
                                                screen                          # screenshot
                                                path/                           # directory
                                                list.txt                        # list of images
                                                list.streams                    # list of streams
                                                'path/*.jpg'                    # glob
                                                'https://youtu.be/LNwODJXcvt4'  # YouTube
                                                'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP, TCP stream

Usage - formats:
    $ yolo mode=predict model=yolo26n.pt                 # PyTorch
                              yolo26n.torchscript        # TorchScript
                              yolo26n.onnx               # ONNX Runtime or OpenCV DNN with dnn=True
                              yolo26n_openvino_model     # OpenVINO
                              yolo26n.engine             # TensorRT
                              yolo26n.mlpackage          # CoreML (macOS-only)
                              yolo26n_saved_model        # TensorFlow SavedModel
                              yolo26n.pb                 # TensorFlow GraphDef
                              yolo26n.tflite             # TensorFlow Lite
                              yolo26n_edgetpu.tflite     # TensorFlow Edge TPU
                              yolo26n_paddle_model       # PaddlePaddle
                              yolo26n.mnn                # MNN
                              yolo26n_ncnn_model         # NCNN
                              yolo26n_imx_model          # Sony IMX
                              yolo26n_rknn_model         # Rockchip RKNN
                              yolo26n.pte                # PyTorch Executorch
    )annotationsN)Path)AnyCallable)get_cfgget_save_dir)load_inference_source)	LetterBox)AutoBackend)DEFAULT_CFGLOGGERMACOSWINDOWS	callbackscolorstrops)check_imgszcheck_imshow)increment_path)attempt_compileselect_devicesmart_inference_modea  
Inference results will accumulate in RAM unless `stream=True` is passed, which can cause out-of-memory errors for large
sources or long-running streams and videos. See https://docs.ultralytics.com/modes/predict/ for help.

Example:
    results = model(source=..., stream=True)  # generator of Results objects
    for r in results:
        boxes = r.boxes  # Boxes object for bbox outputs
        masks = r.masks  # Masks object for segment masks outputs
        probs = r.probs  # Class probabilities for classification outputs
c                      e Zd ZdZeddfd0dZd1dZd2dZd3dZd Z	d4d5dZ
d6dZd7d8dZ e            d6d            Zd9d:dZd;d$Zd<d=d(Zd>d?d*Zd@d,ZdAd/ZdS )BBasePredictora  A base class for creating predictors.

    This class provides the foundation for prediction functionality, handling model setup, inference, and result
    processing across various input sources.

    Attributes:
        args (SimpleNamespace): Configuration for the predictor.
        save_dir (Path): Directory to save results.
        done_warmup (bool): Whether the predictor has finished setup.
        model (torch.nn.Module): Model used for prediction.
        data (str): Data configuration.
        device (torch.device): Device used for prediction.
        dataset (Dataset): Dataset used for prediction.
        vid_writer (dict[Path, cv2.VideoWriter]): Dictionary of {save_path: video_writer} for saving video output.
        plotted_img (np.ndarray): Last plotted image.
        source_type (SimpleNamespace): Type of input source.
        seen (int): Number of images processed.
        windows (list[str]): List of window names for visualization.
        batch (tuple): Current batch data.
        results (list[Any]): Current batch results.
        transforms (Callable): Image transforms for classification.
        callbacks (dict[str, list[Callable]]): Callback functions for different events.
        txt_path (Path): Path to save text results.
        _lock (threading.Lock): Lock for thread-safe inference.

    Methods:
        preprocess: Prepare input image before inference.
        inference: Run inference on a given image.
        postprocess: Process raw predictions into structured results.
        predict_cli: Run prediction for command line interface.
        setup_source: Set up input source and inference mode.
        stream_inference: Stream inference on input source.
        setup_model: Initialize and configure the model.
        write_results: Write inference results to files.
        save_predicted_images: Save prediction visualizations.
        show: Display results in a window.
        run_callbacks: Execute registered callbacks for an event.
        add_callback: Register a new callback function.
    N	overridesdict[str, Any] | None
_callbacksdict | Nonec                P   t          ||          | _        t          | j                  | _        | j        j        d| j        _        d| _        | j        j        rt          d          | j        _        d| _        | j        j	        | _	        d| _
        d| _        d| _        i | _        d| _        d| _        d| _        g | _        d| _        d| _        d| _        |pt+          j                    | _        d| _        t1          j                    | _        t+          j        |            dS )a:  Initialize the BasePredictor class.

        Args:
            cfg (str | Path | dict | SimpleNamespace): Path to a configuration file or a configuration dictionary.
            overrides (dict, optional): Configuration overrides.
            _callbacks (dict, optional): Dictionary of callback functions.
        Ng      ?FT)warnr   )r   argsr   save_dirconfdone_warmupshowr   modeldataimgszdevicedataset
vid_writerplotted_imgsource_typeseenwindowsbatchresults
transformsr   get_default_callbackstxt_path	threadingLock_lockadd_integration_callbacks)selfcfgr   r   s       a/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/engine/predictor.py__init__zBasePredictor.__init__o   s    C++	$TY//9>!!DIN 9> 	5)t444DIN 
IN	
	
#Hy'F'H'H^%%
+D11111    imtorch.Tensor | list[np.ndarray]returntorch.Tensorc                   t          |t          j                   }|rt          j        |                     |                    }|j        d         dk    r|ddddf         }|                    d          }t          j        |          }t          j	        |          }|
                    | j                  }| j        j        r|                                n|                                }|r|dz  }|S )a  Prepare input image before inference.

        Args:
            im (torch.Tensor | list[np.ndarray]): Images of shape (N, 3, H, W) for tensor, [(H, W, 3) x N] for list.

        Returns:
            (torch.Tensor): Preprocessed image tensor of shape (N, 3, H, W).
           .N)r   rD            )
isinstancetorchTensornpstackpre_transformshape	transposeascontiguousarray
from_numpytor)   r&   fp16halffloat)r9   r>   
not_tensors      r;   
preprocesszBasePredictor.preprocess   s     $B555
 	&$,,R0011Bx|q  TTrT	]l++B%b))B!"%%BUU4;*/9RWWYYYrxxzz 	#IB	r=   c                    | j         j        rH| j        j        s<t	          | j        t          | j        d         d                   j        z  d          nd} | j	        |g|R | j         j
        || j         j        d|S )zGRun inference on a given image using the specified model and arguments.r   T)mkdirF)augment	visualizeembed)r!   r[   r-   tensorr   r"   r   r0   stemr&   rZ   r\   )r9   r>   r!   kwargsr[   s        r;   	inferencezBasePredictor.inference   s     y",0,<,CN4=4
1a0@+A+A+FFdSSSS 	
 tz"ufjuudi&79TXT]Tcuuntuuur=   list[np.ndarray]c                    t          d |D                       dk    }t          | j        |oA| j        j        o5| j        j        dk    p%t          | j        dd          o| j        j        dk    | j        j                  fd|D             S )	zPre-transform input image before inference.

        Args:
            im (list[np.ndarray]): List of images with shape [(H, W, 3) x N].

        Returns:
            (list[np.ndarray]): List of transformed images.
        c                    h | ]	}|j         
S  )rN   ).0xs     r;   	<setcomp>z.BasePredictor.pre_transform.<locals>.<setcomp>   s    ///q17///r=   rE   ptdynamicFimx)autostridec                (    g | ]} |           S ))imagerd   )re   rf   	letterboxs     r;   
<listcomp>z/BasePredictor.pre_transform.<locals>.<listcomp>   s&    ///q		"""///r=   )	lenr
   r(   r!   rectr&   formatgetattrrl   )r9   r>   same_shapesro   s      @r;   rM   zBasePredictor.pre_transform   s     //B///00A5J v	v"d*twtz9e/T/T/sY]YcYjnsYs:$
 
 
	 0///B////r=   c                    |S )z6Post-process predictions for an image and return them.rd   )r9   predsimg	orig_imgss       r;   postprocesszBasePredictor.postprocess   s    r=   Fstreamboolc                v    || _         |r | j        ||g|R i |S t           | j        ||g|R i |          S )a  Perform inference on an image or stream.

        Args:
            source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor, optional):
                Source for inference.
            model (str | Path | torch.nn.Module, optional): Model for inference.
            stream (bool): Whether to stream the inference results. If True, returns a generator.
            *args (Any): Additional arguments for the inference method.
            **kwargs (Any): Additional keyword arguments for the inference method.

        Returns:
            (list[ultralytics.engine.results.Results] | generator): Results objects or generator of Results objects.
        )r{   stream_inferencelist)r9   sourcer&   r{   r!   r_   s         r;   __call__zBasePredictor.__call__   sg      	O(4(HHHHHHH--feMdMMMfMMNNNr=   c                <    |                      ||          }|D ]}dS )a<  Method used for Command Line Interface (CLI) prediction.

        This function is designed to run predictions using the CLI. It sets up the source and model, then processes the
        inputs in a streaming manner. This method ensures that no outputs accumulate in memory by consuming the
        generator without storing results.

        Args:
            source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor, optional):
                Source for inference.
            model (str | Path | torch.nn.Module, optional): Model for inference.

        Notes:
            Do not modify this function or remove the generator. The generator ensures that no outputs are
            accumulated in memory, which is critical for preventing memory issues during long-running predictions.
        N)r~   )r9   r   r&   gen_s        r;   predict_clizBasePredictor.predict_cli   s5      ##FE22 	 	A	 	r=   rl   
int | Nonec                *   t          | j        j        |p| j        j        d          | _        t          || j        j        | j        j        | j        j        t          | j        dd                    | _
        | j
        j        | _        | j        j        sH| j        j        s<t          | j
                  dk    s$t          t          | j
        ddg                    r.d	d
l}t          | dd          st#          j        t&                     i | _        d
S )a  Set up source and inference mode.

        Args:
            source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor): Source for
                inference.
            stride (int, optional): Model stride for image size checking.
        rF   )rl   min_dimchannelsrD   )r   r0   
vid_stridebufferr   i  
video_flagFr   Nr{   T)r   r!   r(   r&   rl   r	   r0   r   stream_bufferrt   r*   r-   r{   
screenshotrq   anytorchvisionr   warningSTREAM_WARNINGr+   )r9   r   rl   r   s       r;   setup_sourcezBasePredictor.setup_source   s	    !9T4:CT^_```
,)/y+9*TZQ77
 
 
  <3#		/*		/ 4<  4''74<w??@@ ( 4400 /~...r=   c              /     K    j         j        rt          j        d            j        s                     |            j        5                       ||n j         j                    j         j	        s j         j
        r2 j         j
        r
 j        dz  n j                            dd            j        sP j                             j        j        dv rdn j        j         j        j        g j        R            d _        d	g dc _         _         _        t/          j         j        
          t/          j         j        
          t/          j         j        
          f}                     d            j        D ]k}| _                             d            j        \  }}}	|d	         5                       |          }
ddd           n# 1 swxY w Y   |d         5    j        |
g|R i |} j         j        r2t=          |t>          j                   r|gn|E d{V  	 ddd           	 ddd           n# 1 swxY w Y   |d         5   !                    ||
|           _"        ddd           n# 1 swxY w Y                        d           tG          |          }	 tI          |          D ]} xj        dz  c_        |d	         j%        dz  |z  |d         j%        dz  |z  |d         j%        dz  |z  d j"        |         _&         j         j        s$ j         j	        s j         j
        s j         j'        r9|	|xx          (                    |tS          ||                   |
|	          z  cc<   n# tT          $ r Y  n[w xY w j         j        r't          j        d+                    |	                                          d            j"        E d{V  mddd           n# 1 swxY w Y    j,        -                                D ]0}t=          |t\          j/                  r|0                                 1 j         j'        rt]          j1                      j         j        r j        rxte           fd|D                       }t          j        dtg           j         j         j                  ti           j        dd          g|
j5        dd         R  |z              j         j	        s j         j
        s j         j6        rtG          to           j        8                    d                              } j         j
        rd| dd|dk    z   d j        dz   nd}	t          j        dts          d j                   |	                                 d           dS )a+  Stream inference on input source and save results to file.

        Args:
            source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor, optional):
                Source for inference.
            model (str | Path | torch.nn.Module, optional): Model for inference.
            *args (Any): Additional arguments for the inference method.
            **kwargs (Any): Additional keyword arguments for the inference method.

        Yields:
            (ultralytics.engine.results.Results): Results objects.
         NlabelsTparentsexist_ok>   rh   tritonrE   )r(   r   )r)   on_predict_starton_predict_batch_startrF   on_predict_postprocess_end     @@)rW   r`   rz   
on_predict_batch_endc              3  <   K   | ]}|j         j        z  d z  V  dS )r   N)tr.   )re   rf   r9   s     r;   	<genexpr>z1BasePredictor.stream_inference.<locals>.<genexpr>x  s0      ??acDIo+??????r=   zRSpeed: %.1fms preprocess, %.1fms inference, %.1fms postprocess per image at shape r   rD   zlabels/*.txtz labelsz
 saved to zResults saved to boldon_predict_end):r!   verboser   infor&   setup_modelr7   r   r   savesave_txtr"   rY   r$   warmuprs   r*   bsr   r(   r.   r/   r0   r   Profiler)   run_callbacksrW   r`   r\   rH   rI   rJ   rz   r1   rq   rangedtspeedr%   write_resultsr   StopIterationjoinr+   valuescv2VideoWriterreleasedestroyAllWindowstupleminrt   rN   	save_cropr   globr   )r9   r   r&   r!   r_   	profilersr0   pathsim0sr   r>   rw   nivr   nls   `                r;   r~   zBasePredictor.stream_inference  s      9 	KOOO z 	$U###Z C	( C	((:ff	@PQQQ y~ w!3 w-1Y-?R))T]YYbfquYvvv # (
!!!Z.2BBB
+   "    $( 23R/DIt|TZ4;///4;///4;///I
 1222 )( )("
""#;<<<!%tQ q\ / /..B/ / / / / / / / / / / / / / / q\ ! !*DN2??????Ey !.8.M.M#XE77SXXXXXXXX 	! ! ! ! ! ! !!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! q\ E E#'#3#3E2t#D#DDLE E E E E E E E E E E E E E E""#?@@@ II"1XX Q Q		Q		*3A,/C*?!*C)213)>)B+4Q<?S+@1+D1 1Q-
  9, Q	 Q$)BT QX\XaXf QaDDDD$6$6q$uQx.."a$P$PPDDDQ %   EE 9$ .K		!---""#9:::<''''''''GC	( C	( C	( C	( C	( C	( C	( C	( C	( C	( C	( C	( C	( C	( C	(L '')) 	 	A!S_-- 		9> 	$!### 9 	 	????Y?????AKj	33WTZUV5W5WgZ\ZbcdceceZfggj jlmn   9> 	RTY/ 	R493F 	RT$-,,^<<==>>BW[W`WiqSRSSsb1f~SS9QSSSoqAKPHVT],K,KPQPPQQQ+,,,,,s   E6P=GPG##P&G#'P2AI6PPIPIP"J?PJPJ'P;CNP
NPNAPPPTr   c           	        t          |d          rO| j        j        | j        j        |_        |j        r+|                    | j        j        | j        j                   t          |p| j        j        t          | j        j	        |          | j        j
        | j        j        | j        j        d|          | _        | j        j	        | _	        | j        j        | j        _        t          | j        d          r,t          | j        dd	          s| j        j        | j        _        | j                                         t#          | j        | j	        | j        j        
          | _        dS )zInitialize YOLO model with given parameters and set it to evaluation mode.

        Args:
            model (str | Path | torch.nn.Module): Model to load or use.
            verbose (bool): Whether to print verbose output.
        end2endN)max_detagnostic_nms)r   T)r&   r)   dnnr'   rS   fuser   r(   ri   F)r)   mode)hasattrr!   r   set_head_attrr   r   r   r&   r   r)   r   r'   rT   rS   rt   r(   evalr   compile)r9   r&   r   s      r;   r   zBasePredictor.setup_model  s5    5)$$ 	dy , $	 1} d##DI,=DILb#ccc *49? !17CCC	
 
 

 j'	4:w'' 	/
Iu0U0U 	/"j.DIO
$TZ$)J[\\\


r=   r   intpr   r   	list[str]strc                   d}t          |j                  dk    r|d         }| j        j        s| j        j        s| j        j        r|| dz  }| j        j        }n4t          j	        d||                   }|rt          |d                   nd}| j        dz  |j        | j        j        dk    rdnd	| z   z  | _        | d
j        |j        dd          z  }| j        |         }| j                                        |_        ||                                 |j        d         ddz  }| j        j        s| j        j        r[|                    | j        j        | j        j        | j        j        | j        j        | j        j        rdn||                   | _        | j        j        r)|                    | j         d| j        j                   | j        j         r)|                     | j        dz  | j        j                   | j        j        r"|                     tC          |                     | j        j        r#| "                    | j        |j#        z  |           |S )ah  Write inference results to a file or directory.

        Args:
            i (int): Index of the current image in the batch.
            p (Path): Path to the current image.
            im (torch.Tensor): Preprocessed image tensor.
            s (list[str]): List of result strings.

        Returns:
            (str): String with result information.
        r   rD   Nz: zframe (\d+)/rE   r   rn   r   z
{:g}x{:g} rF   r`   z.1fms)
line_widthboxesr#   r   im_gpuz.txt)	save_confcrops)r"   	file_name)$rq   rN   r-   r{   from_imgr]   r*   countresearchr   r"   r^   r   r4   rs   r1   __str__r   r   r!   r   r%   plotr   
show_boxes	show_confshow_labelsretina_masksr,   r   r   r   r   save_predicted_imagesname)	r9   r   r   r>   r   stringframematchresults	            r;   r   zBasePredictor.write_results  s`    rx==ADB" 	5d&6&? 	54CSCZ 	5hhhFL&EEIoqt44E%*4CaMMME0AFDLDUY`D`D`bbfqjofqfq4rs%,%rx|44a-//11V^^%%Hv|K'@HHHHH 9> 	TY^ 	%{{9/i*Y(y,#y5@tt2a5  +    D 9 	SOOt}222di>QORRR9 	]dmg&=I[\\\9> 	IIc!ff9> 	F&&t}qv'=uEEEr=   r   	save_pathr   c                $   | j         }| j        j        dv rD| j        j        dk    r| j        j        nd}| j        |j         dz  }|| j        vr| j        j        r$t          |          
                    dd           t          rdn
t          rdnd	\  }}t          j        t          t          |                              |                    t          j        | ||j        d
         |j        d         f          | j        |<   | j        |                             |           | j        j        r%t          j        | d|j         d| d|           dS dS t          j        t          |                    d                    |           dS )zSave video predictions as mp4/avi or images as jpg at specified path.

        Args:
            save_path (Path): Path to save the results.
            frame (int): Frame number for video mode.
        >   videor{   r      _framesTr   )z.mp4avc1).aviWMV2)r   MJPGrE   r   )filenamefourccfps	frameSize/r   z.jpgN)r,   r*   r   r   r"   r^   r+   r!   save_framesr   rY   r   r   r   r   r   with_suffixVideoWriter_fourccrN   writeimwrite)r9   r   r   r>   r   frames_pathsuffixr   s           r;   r   z#BasePredictor.save_predicted_images  s     < 333&*l&77&B&B$,""C-Y^*D*D*DDK//9( I%%++D4+HHH5:!q!1!1T[@q@P@Paq-0_ i!<!<V!D!DEE16:!x{BHQK8	. . .	* OI&,,R000y$ O{IIY^IIeIII2NNNNNO O
 KI11&99::B?????r=   r   c                   | j         }t          j                    dk    r{|| j        vrr| j                            |           t          j        |t
          j        t
          j        z             t          j	        ||j
        d         |j
        d                    t          j        ||           t          j        | j        j        dk    rdnd          dz  t          d          k    rt           dS )	zDisplay an image in a window.LinuxrE   r   rn   i,  rG   qN)r,   platformsystemr/   appendr   namedWindowWINDOW_NORMALWINDOW_KEEPRATIOresizeWindowrN   imshowwaitKeyr*   r   ordr   )r9   r   r>   s      r;   r%   zBasePredictor.show  s    ?''AT\,A,AL"""OAs033GGHHHQRXa[999
1b;dl/7::ssBBTISQTXXUU VUr=   eventc                X    | j                             |g           D ]} ||            dS )z2Run all registered callbacks for a specific event.N)r   get)r9   r  callbacks      r;   r   zBasePredictor.run_callbacks  s<    **5"55 	 	HHTNNNN	 	r=   funcr   c                F    | j         |                             |           dS )z-Add a callback function for a specific event.N)r   r  )r9   r  r  s      r;   add_callbackzBasePredictor.add_callback  s#    u$$T*****r=   )r   r   r   r   )r>   r?   r@   rA   )r>   rA   )r>   ra   r@   ra   )NNF)r{   r|   )NN)N)rl   r   )T)r   r|   )
r   r   r   r   r>   rA   r   r   r@   r   )r   )r   r   r   r   )r   )r   r   )r  r   )r  r   r  r   )__name__
__module____qualname____doc__r   r<   rW   r`   rM   rz   r   r   r   r   r~   r   r   r   r%   r   r  rd   r=   r;   r   r   F   s       & &T +/"&	&2 &2 &2 &2 &2P   0v v v v0 0 0 0&  O O O O O(   (    : l- l- l- l-\] ] ] ] ]:0 0 0 0d@ @ @ @ @B	  	  	  	  	    
+ + + + + +r=   r   )+r  
__future__r   r  r   r5   pathlibr   typingr   r   r   numpyrK   rI   ultralytics.cfgr   r   ultralytics.datar	   ultralytics.data.augmentr
   ultralytics.nn.autobackendr   ultralytics.utilsr   r   r   r   r   r   r   ultralytics.utils.checksr   r   ultralytics.utils.filesr   ultralytics.utils.torch_utilsr   r   r   r   r   rd   r=   r;   <module>r&     s     D # " " " " "  				                           



      1 1 1 1 1 1 1 1 2 2 2 2 2 2 . . . . . . 2 2 2 2 2 2 [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ > > > > > > > > 2 2 2 2 2 2 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
+ + + + + + + + + +r=   