Ë
    Fêñi[  ã                  ó   — 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*y)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_executorch_model   # PyTorch Executorch
                              yolo26n_axelera_model      # Axelera AI
é    )Ú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	 	 	 dd„Zdd„Zdd„Zdd„Zd„ Z	ddd„Z
dd	„Zddd
„Z e«       dd„«       Zddd„Zdd„Zddd„Zd d!d„Zd"d„Zd#d„Zy)$Ú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.
    Nc                óš  — 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| _        |xs t+        j,                  «       | _        d| _        t1        j2                  «       | _        t+        j6                  | «       y)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ÚcfgÚ	overridesÚ
_callbackss       ú^/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/engine/predictor.pyÚ__init__zBasePredictor.__init__p   sý   € ô ˜C Ó+ˆŒ	Ü$ T§Y¡YÓ/ˆŒØ�9‰9�>‰>Ð!Ø!ˆD�I‰IŒNØ ˆÔØ�9‰9�>Š>Ü)¨tÔ4ˆD�I‰IŒNð ˆŒ
Ø—I‘I—N‘NˆŒ	ØˆŒ
ØˆŒØˆŒØˆŒØˆÔØˆÔØˆŒ	ØˆŒØˆŒ
ØˆŒØˆŒØ#ÒH¤y×'FÑ'FÓ'HˆŒØˆŒÜ—^‘^Ó%ˆŒ
Ü×+Ñ+¨DÕ1ó    c                óæ  — t        |t        j                  «       }|r{t        j                  | j                  |«      «      }|j                  d   dk(  r
|dddd…f   }|j                  d«      }t        j                  |«      }t        j                  |«      }|j                  | j                  «      }| j                  j                  r|j                  «       n|j                  «       }|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   r>   é   é   éÿ   )Ú
isinstanceÚtorchÚTensorÚnpÚstackÚpre_transformÚshapeÚ	transposeÚascontiguousarrayÚ
from_numpyÚtor%   r"   Úfp16ÚhalfÚfloat)r5   ÚimÚ
not_tensors      r9   Ú
preprocesszBasePredictor.preprocess˜   sÂ   € ô $ B¬¯©Ó5Ð5ˆ
ÙÜ—‘˜$×,Ñ,¨RÓ0Ó1ˆBØ�x‰x˜‰|˜qÒ Ø˜™T˜r˜T˜	‘]�Ø—‘˜lÓ+ˆBÜ×%Ñ% bÓ)ˆBÜ×!Ñ! "Ó%ˆBà�U‰U�4—;‘;ÓˆØŸ*™*Ÿ/š/ˆR�W‰WŒY¨r¯x©x«zˆÙØ�#‰IˆBØˆ	r;   c                ó\  — | j                   j                  rS| j                  j                  s=t	        | j
                  t        | j                  d   d   «      j                  z  d¬«      nd} | j                  |g|¢­| 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   rV   r)   Útensorr   r   r   r,   Ústemr"   rU   rW   )r5   rP   r   ÚkwargsrV   s        r9   Ú	inferencezBasePredictor.inference°   s“   € ð �y‰y×"Ò"¨D×,<Ñ,<×,CÒ,Cô ˜4Ÿ=™=¬4°·
±
¸1±¸aÑ0@Ó+A×+FÑ+FÑFÈdÕSàð 	ð
 ˆt�z‰z˜"ÐuÐfjÑu d§i¡i×&7Ñ&7À9ÐTX×T]ÑT]×TcÑTcÑuÐntÑuÐur;   c                ó¶  — t        |D �ch c]  }|j                  ’Œ c}«      dk(  }t        | j                  |xre | j                  j
                  xrM | j                  j                  dk(  xs2 t        | j                  dd«      xr | j                  j                  dk7  | j                  j                  ¬«      }|D �cg c]  } ||¬«      ‘Œ c}S c c}w c c}w )zßPre-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.
        r?   ÚptÚdynamicFÚimx)ÚautoÚstride)Úimage)
ÚlenrH   r
   r$   r   Úrectr"   ÚformatÚgetattrra   )r5   rP   ÚxÚsame_shapesÚ	letterboxs        r9   rG   zBasePredictor.pre_transform¹   s¸   € ô ¨BÖ/ q˜1Ÿ7›7Ò/Ó0°AÑ5ˆÜØ�J‰JØò vØ—	‘	—‘òvà—‘×"Ñ" dÑ*Òt¬w°t·z±zÀ9ÈeÓ/TÒ/sÐY]×YcÑYc×YjÑYjÐnsÑYsØ—:‘:×$Ñ$ô
ˆ	ð -/Ö/ q‘	 Ö"Ò/Ð/ùò 0ùò 0s   ŠCÂ>Cc                ó   — |S )z6Post-process predictions for an image and return them.© )r5   ÚpredsÚimgÚ	orig_imgss       r9   ÚpostprocesszBasePredictor.postprocessÌ   s   € àˆr;   c                ó‚   — || _         |r | j                  ||g|¢­i |¤ŽS t         | j                  ||g|¢­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.
        )ÚstreamÚstream_inferenceÚlist)r5   Úsourcer"   rq   r   rZ   s         r9   Ú__call__zBasePredictor.__call__Ð   sR   € ð ˆŒÙØ(�4×(Ñ(¨°ÐH¸ÒHÀÑHÐHäÐ-˜×-Ñ-¨f°eÐM¸dÒMÀfÑMÓNÐNr;   c                ó6   — | j                  ||«      }|D ]  }Œ y)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)rr   )r5   rt   r"   ÚgenÚ_s        r9   Úpredict_clizBasePredictor.predict_cliä   s(   € ð  ×#Ñ# F¨EÓ2ˆØò 	ˆAØñ	r;   c                ó°  — t        | j                  j                  |xs | j                  j                  d¬«      | _        t        || j                  j                  | j                  j                  | j                  j                  t        | j                  dd«      ¬«      | _
        | j                  j                  | _        | j                  j                  sO| j                  j                  s9t        | j                  «      dkD  s!t        t        | j                  ddg«      «      r*d	d
l}t        | dd«      st#        j$                  t&        «       i | _        y
)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.
        r@   )ra   Úmin_dimÚchannelsr>   )rt   r,   Ú
vid_strideÚbufferr|   iè  Ú
video_flagFr   Nrq   T)r   r   r$   r"   ra   r	   r,   r}   Ústream_bufferrf   r&   r)   rq   Ú
screenshotrc   ÚanyÚtorchvisionr   ÚwarningÚSTREAM_WARNINGr'   )r5   rt   ra   rƒ   s       r9   Úsetup_sourcezBasePredictor.setup_sourceø   sì   € ô ! §¡§¡¸Ò9TÀ4Ç:Á:×CTÑCTÐ^_Ô`ˆŒ
Ü,ØØ—)‘)—/‘/Ø—y‘y×+Ñ+Ø—9‘9×*Ñ*Ü˜TŸZ™Z¨°QÓ7ô
ˆŒð  Ÿ<™<×3Ñ3ˆÔà×Ñ×#Ò#Ø×Ñ×*Ò*Ü�4—<‘<Ó  4Ò'Ü”7˜4Ÿ<™<¨¸°wÓ?Ô@ãä˜4 ¨4Ô0Ü—‘œ~Ô.Øˆ�r;   c              /  óN  ‡ K  — ‰ j                   j                  rt        j                  d«       ‰ j                  s‰ j                  |«       ‰ j                  5  ‰ j                  |�|n‰ j                   j                  «       ‰ j                   j                  s‰ j                   j                  rB‰ j                   j                  r‰ j                  dz  n‰ j                  j                  dd¬«       ‰ j                  st‰ j                  j                  ‰ j                  j                  dv rdn‰ j                   j"                  ‰ j                  j$                  g‰ j&                  ¢­¬«       d‰ _        d	g dc‰ _        ‰ _        ‰ _        t/        j0                  ‰ j2                  ¬
«      t/        j0                  ‰ j2                  ¬
«      t/        j0                  ‰ j2                  ¬
«      f}‰ j5                  d«       ‰ j                   D �]S  }|‰ _        ‰ j5                  d«       ‰ j,                  \  }}}	|d	   5  ‰ j7                  |«      }
ddd«       |d   5   ‰ j8                  
g|¢­i |¤Ž}‰ j                   j:                  r1t=        |t>        j@                  «      r|gn|E d{  –—†  	 ddd«       Œ®	 ddd«       |d   5  ‰ jC                  
|«      ‰ _"        ddd«       ‰ j5                  d«       tG        |«      }	 tI        |«      D ]ì  }‰ xj(                  dz  c_        |d	   jJ                  dz  |z  |d   jJ                  dz  |z  |d   jJ                  dz  |z  dœ‰ jD                  |   _&        ‰ j                   j                  sC‰ j                   j                  s-‰ j                   j                  s‰ j                   jN                  sŒÂ|	|xx   ‰ jQ                  |tS        ||   «      
|	«      z  cc<   Œî 	 ‰ j                   j                  r$t        j                  djW                  |	«      «       ‰ j5                  d«       ‰ jD                  E d{  –—†  �ŒV ddd«       ‰ jX                  j[                  «       D ]-  }t=        |t\        j^                  «      sŒ|ja                  «        Œ/ ‰ j                   jN                  rt]        jb                  «        ‰ j                   j                  rŠ‰ j(                  r~te        ˆ fd„D «       «      }t        j                  dtg        ‰ j                   j,                  ‰ j(                  «      ti        ‰ j                  dd«      g
jj                  dd ¢­› �|z  «       ‰ j                   j                  s,‰ j                   j                  s‰ j                   jl                  r‘tG        to        ‰ j                  jq                  d«      «      «      }‰ j                   j                  rd|› dd|dkD  z  › d‰ j                  dz  › �nd}	t        j                  dts        d‰ j                  «      › |	› �«       ‰ j5                  d«       y# 1 sw Y   �ŒxY w7 �ŒÆ# 1 sw Y   �Œ»xY w# 1 sw Y   �Œ¢xY w# tT        $ r Y  �Œ8w xY w7 �ŒD# 1 sw Y   �ŒAxY w­w)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>   r]   Útritonr?   )r$   r   )r%   Úon_predict_startÚon_predict_batch_startr@   Úon_predict_postprocess_endç     @�@)rR   r[   ro   ú
Úon_predict_batch_endc              3  óV   •K  — | ]   }|j                   ‰j                  z  d z  –— Œ" y­w)r‘   N)Útr*   )Ú.0rg   r5   s     €r9   ú	<genexpr>z1BasePredictor.stream_inference.<locals>.<genexpr>y  s"   øè ø€ Ò?°�a—c‘c˜DŸI™I‘o¨Õ+Ñ?ùs   ƒ&)zRSpeed: %.1fms preprocess, %.1fms inference, %.1fms postprocess per image at shape r|   r>   zlabels/*.txtz labelÚsz
 saved to zResults saved to ÚboldÚon_predict_end):r   Úverboser   Úinfor"   Úsetup_modelr3   r†   rt   ÚsaveÚsave_txtr   rT   r    Úwarmupre   r&   Úbsr|   r$   r*   r+   r,   r   ÚProfiler%   Úrun_callbacksrR   r[   rW   rB   rC   rD   ro   r-   rc   ÚrangeÚdtÚspeedr!   Úwrite_resultsr   ÚStopIterationÚjoinr'   ÚvaluesÚcv2ÚVideoWriterÚreleaseÚdestroyAllWindowsÚtupleÚminrf   rH   Ú	save_croprs   Úglobr   )r5   rt   r"   r   rZ   Ú	profilersr,   ÚpathsÚim0sr˜   rP   rl   ÚnÚiÚvr•   Únls   `                r9   rr   zBasePredictor.stream_inference  sf  øè ø€ ð �9‰9×ÒÜ�K‰K˜ŒOð �zŠzØ×Ñ˜UÔ#à�Z‰Zñ C	(à×Ñ¨Ð(:™fÀÇ	Á	×@PÑ@PÔQð �y‰y�~Š~ §¡×!3Ò!3Ø-1¯Y©Y×-?Ò-?�—‘ Ò)ÀTÇ]Á]×YÑYÐbfÐquÐYÔvð ×#Ò#Ø—
‘
×!Ñ!à!ŸZ™Z×.Ñ.Ð2BÑB™ÈÏÉÏÉØŸ
™
×+Ñ+ðð Ÿ™ñð "ô ð $(�Ô à23°R¸Ð/ˆDŒI�t”| T¤Zä—‘ 4§;¡;Ô/Ü—‘ 4§;¡;Ô/Ü—‘ 4§;¡;Ô/ðˆIð
 ×ÑÐ1Ô2ØŸ™ó )(�Ø"�”
Ø×"Ñ"Ð#;Ô<Ø!%§¡‘��t˜Qð ˜q‘\ñ /ØŸ™¨Ó.�B÷/ð ˜q‘\ñ !Ø*˜DŸN™N¨2Ð?°Ò?¸Ñ?�EØ—y‘y—’Ü.8¸ÄÇÁÔ.M E¡7ÐSX×XÐXØ ÷	!ð !à&÷!ð ˜q‘\ñ EØ#'×#3Ñ#3°E¸2¸tÓ#D�D”L÷Eà×"Ñ"Ð#?Ô@ô ˜“I�ðÜ" 1›Xò Q˜ØŸ	š	 Q™�	à*3°A©,¯/©/¸CÑ*?À!Ñ*CØ)2°1©¯©¸3Ñ)>ÀÑ)BØ+4°Q©<¯?©?¸SÑ+@À1Ñ+Dñ1˜Ÿ™ Q™Ô-ð
  Ÿ9™9×,Ò,°·	±	·²À$Ç)Á)×BTÒBTÐX\×XaÑXa×XfÓXfØ˜a›D D×$6Ñ$6°q¼$¸uÀQ¹x».È"ÈaÓ$PÑPœDñQð —9‘9×$Ò$Ü—K‘K §	¡	¨!£Ô-à×"Ñ"Ð#9Ô:ØŸ<™<×'Ò'ðS)(÷5C	(ðL —‘×'Ñ'Ó)ò 	ˆAÜ˜!œSŸ_™_Õ-Ø—	‘	•ð	ð �9‰9�>Š>Ü×!Ñ!Ô#ð �9‰9×Ò §¢ÜÓ?°YÔ?Ó?ˆAÜ�K‰KØdÜ˜Ÿ	™	Ÿ™¨¯©Ó3´W¸T¿Z¹ZÈÐUVÓ5WÐgÐZ\×ZbÑZbÐcdÐceÐZfÑgÐhðjØlmñnôð �9‰9�>Š>˜TŸY™Y×/Ò/°4·9±9×3FÒ3FÜ”T˜$Ÿ-™-×,Ñ,¨^Ó<Ó=Ó>ˆBØW[×W`ÑW`×WiÒWi�"�R�D˜˜s b¨1¡f™~Ð.¨j¸¿¹ÈÑ9QÐ8RÑSÐoqˆAÜ�K‰KÐ+¬H°V¸T¿]¹]Ó,KÐ+LÈQÈCÐPÔQØ×ÑÐ+Õ,÷q/ñ /úð Yù÷!ñ !ú÷Eñ Eûô  %ò Ûðúð (ù÷GC	(ñ C	(üs¿   ƒAZ%ÁGZÈ3YÉZÉAY*Ê!Y'
Ê"Y*Ê'	ZÊ1ZÊ>Y7Ë$ZË<CZÏ	-ZÏ6AZÑZÑZÑ?Z%ÒGZ%ÙY$ÙZÙ'Y*Ù*Y4Ù/ZÙ7ZÙ<ZÚ	ZÚZÚZÚZÚZ"ÚZ%c           	     óî  — t        |d«      rx| j                  j                  �| j                  j                  |_        |j                  r;|j                  | j                  j                  | j                  j
                  ¬«       t        |xs | 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                  j!                  «        t#        | j                  | j                  | j                  j$                  ¬
«      | _        y)zçInitialize 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#   rM   Úfuser›   r$   r^   F)r%   Úmode)Úhasattrr   r»   Úset_head_attrr¼   r½   r   r"   r   r%   r¾   r#   rN   rM   rf   r$   Úevalr   Úcompile)r5   r"   r›   s      r9   r�   zBasePredictor.setup_model„  s<  € ô �5˜)Ô$Ø�y‰y× Ñ Ð,Ø $§	¡	× 1Ñ 1�”Ø�}Š}Ø×#Ñ#¨D¯I©I×,=Ñ,=ÈDÏIÉI×LbÑLbÐ#ÔcÜ ØÒ*˜4Ÿ9™9Ÿ?™?Ü  §¡×!1Ñ!1¸7ÔCØ—	‘	—‘Ø—‘—‘Ø—‘—‘ØØô
ˆŒ
ð —j‘j×'Ñ'ˆŒØŸ™Ÿ™ˆ�	‰	ŒÜ�4—:‘:˜wÔ'´¸¿
¹
ÀIÈuÔ0UØ"Ÿj™j×.Ñ.ˆD�I‰IŒOØ�
‰
�‰ÔÜ$ T§Z¡Z¸¿¹È$Ï)É)×J[ÑJ[Ô\ˆ�
r;   c                ó0  — d}t        |j                  «      dk(  r|d   }| j                  j                  s,| j                  j                  s| j                  j
                  r||› d�z  }| j                  j                  }n+t        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#                  «       |_        ||j%                  «       › |j&                  d   d›d�z  }| j(                  j*                  s| j(                  j,                  r†|j/                  | j(                  j0                  | j(                  j2                  | j(                  j4                  | j(                  j6                  | j(                  j8                  rdn||   ¬«      | _        | j(                  j<                  r4|j=                  | j                  › d�| j(                  j>                  ¬«       | j(                  j@                  r4|jA                  | j                  dz  | j                  j                  ¬«       | j(                  j,                  r| j-                  tC        |«      «       | j(                  j*                  r)| jE                  | j                  |jF                  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ˆ   r>   Nz: zframe (\d+)/r?   r‰   rb   rx   z
{:g}x{:g} r@   r[   z.1fÚms)Ú
line_widthÚboxesr   r‰   Úim_gpuz.txt)Ú	save_confÚcrops)r   Ú	file_name)$rc   rH   r)   rq   Úfrom_imgrX   r&   ÚcountÚreÚsearchÚintr   rY   rÀ   r0   re   r-   Ú__str__r›   r¦   r   rž   r!   ÚplotrÇ   Ú
show_boxesÚ	show_confÚshow_labelsÚretina_masksr(   rŸ   rÊ   r±   ÚstrÚsave_predicted_imagesÚname)	r5   r·   ÚprP   r˜   ÚstringÚframeÚmatchÚresults	            r9   r§   zBasePredictor.write_results¡  s^  € ð ˆÜˆr�x‰x‹=˜AÒØ�D‘ˆBØ×Ñ×"Ò" d×&6Ñ&6×&?Ò&?À4×CSÑCS×CZÒCZØ˜˜˜2�hÑˆFØ—L‘L×&Ñ&‰Eä—I‘I˜o¨q°©tÓ4ˆEÙ%*”C˜˜a™”M°ˆEàŸ™¨Ñ0°A·F±FÀDÇLÁL×DUÑDUÐY`ÒD`¹bÐhiÐjoÐipÐfqÑ4rÑsˆŒØÐ%�,×%Ñ% r§x¡x°° |Ð4Ñ4ˆØ—‘˜a‘ˆØŸ-™-×/Ñ/Ó1ˆŒØ�V—^‘^Ó%Ð& v§|¡|°KÑ'@ÀÐ&EÀRÐHÑHˆð �9‰9�>Š>˜TŸY™YŸ^š^Ø%Ÿ{™{ØŸ9™9×/Ñ/Ø—i‘i×*Ñ*Ø—Y‘Y×(Ñ(Ø—y‘y×,Ñ,Ø#Ÿy™y×5Ò5‘t¸2¸a¹5ð  +ó  ˆDÔð �9‰9×ÒØ�O‰O˜tŸ}™}˜o¨TÐ2¸d¿i¹i×>QÑ>QˆOÔRØ�9‰9×ÒØ×Ñ d§m¡m°gÑ&=ÈÏÉ×I[ÑI[ÐÔ\Ø�9‰9�>Š>Ø�I‰I”c˜!“fÔØ�9‰9�>Š>Ø×&Ñ& t§}¡}°q·v±vÑ'=¸uÔEàˆr;   c                ó’  — | j                   }| j                  j                  dv �rt| j                  j                  dk(  r| j                  j                  nd}| j                  |j
                  › d�z  }|| j                  vrº| j                  j                  rt        |«      j                  dd¬«       t        rdn	t        rdnd	\  }}t        j                  t        t        |«      j!                  |«      «      t        j"                  |Ž ||j$                  d
   |j$                  d   f¬«      | j                  |<   | j                  |   j'                  |«       | j                  j                  r*t        j(                  |› d|j
                  › d|› d�|«       yyt        j(                  t        |j!                  d«      «      |«       y)zËSave 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.
        >   Úvideorq   rá   é   Ú_framesTrŠ   )z.mp4Úavc1)ú.aviÚWMV2)rå   ÚMJPGr?   r   )ÚfilenameÚfourccÚfpsÚ	frameSizeú/rx   z.jpgN)r(   r&   rÀ   rê   r   rY   r'   r   Úsave_framesr   rT   r   r   r«   r¬   rØ   Úwith_suffixÚVideoWriter_fourccrH   ÚwriteÚimwrite)r5   Ú	save_pathrÝ   rP   rê   Úframes_pathÚsuffixré   s           r9   rÙ   z#BasePredictor.save_predicted_imagesÓ  sn  € ð ×Ñˆð �<‰<×ÑÐ 3Ò3Ø&*§l¡l×&7Ñ&7¸7Ò&B�$—,‘,×"Ò"ÈˆCØŸ-™-¨Y¯^©^Ð,<¸GÐ*DÑDˆKØ §¡Ñ/Ø—9‘9×(Ò(Ü˜Ó%×+Ñ+°DÀ4Ð+ÔHÝ5:Ñ!1ÕT[Ñ@PÐaq‘�˜Ü-0¯_©_Ü ¤ i£×!<Ñ!<¸VÓ!DÓEÜ×1Ñ1°6Ð:ØØ!Ÿx™x¨™{¨B¯H©H°Q©KÐ8ô	.�—‘ 	Ñ*ð �O‰O˜IÑ&×,Ñ,¨RÔ0Ø�y‰y×$Ò$Ü—‘˜{˜m¨1¨Y¯^©^Ð,<¸A¸e¸WÀDÐIÈ2ÕNð %ô
 �K‰Kœ˜I×1Ñ1°&Ó9Ó:¸BÕ?r;   c                ó  — | j                   }t        j                  «       dk(  r�|| j                  vr�| j                  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         ‚y)	zDisplay an image in a window.ÚLinuxr?   r   rb   i,  rA   ÚqN)r(   ÚplatformÚsystemr+   Úappendr«   ÚnamedWindowÚWINDOW_NORMALÚWINDOW_KEEPRATIOÚresizeWindowrH   ÚimshowÚwaitKeyr&   rÀ   Úordr¨   )r5   rÛ   rP   s      r9   r!   zBasePredictor.showô  sÂ   € à×ÑˆÜ�?‰?Ó Ò'¨A°T·\±\Ñ,AØ�L‰L×Ñ Ô"Ü�O‰O˜Aœs×0Ñ0´3×3GÑ3GÑGÔHÜ×Ñ˜Q §¡¨¡¨R¯X©X°a©[Ô9Ü�
‰
�1�bÔÜ�;‰;˜dŸl™l×/Ñ/°7Ò:‘sÀÓBÀTÑIÌSÐQTËXÒUÜÐð Vr;   c                óV   — | j                   j                  |g «      D ]
  } || «       Œ y)z2Run all registered callbacks for a specific event.N)r   Úget)r5   ÚeventÚcallbacks      r9   r£   zBasePredictor.run_callbacksÿ  s)   € àŸ™×*Ñ*¨5°"Ó5ò 	ˆHÙ�T�Nñ	r;   c                ó@   — | j                   |   j                  |«       y)z-Add a callback function for a specific event.N)r   rú   )r5   r  Úfuncs      r9   Úadd_callbackzBasePredictor.add_callback  s   € à�‰�uÑ×$Ñ$ TÕ*r;   )r7   zdict[str, Any] | Noner8   zdict | None)rP   ztorch.Tensor | list[np.ndarray]Úreturnútorch.Tensor)rP   r
  )rP   úlist[np.ndarray]r	  r  )NNF)rq   Úbool)NN)N)ra   z
int | None)T)r›   r  )
r·   rÑ   rÛ   r   rP   r
  r˜   z	list[str]r	  rØ   )r   )rò   r   rÝ   rÑ   )rˆ   )rÛ   rØ   )r  rØ   )r  rØ   r  r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r:   rR   r[   rG   ro   ru   ry   r†   r   rr   r�   r§   rÙ   r!   r£   r  rk   r;   r9   r   r   G   sŽ   „ ñ&ðT Ø+/Ø"&ð	&2ð )ð&2ð  ó	&2óPó0vó0ò&ôOó(ô(ñ: Óòl-ó ðl-ô\]ó:0ôd@ôB	 óô
+r;   r   )+r  Ú
__future__r   rø   rÏ   r1   Úpathlibr   Útypingr   r   r«   ÚnumpyrE   rC   Ú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   rk   r;   r9   ú<module>r     s\   ðñ!õF #ã Û 	Û Ý ß  ã 
Û Û ç 1Ý 2Ý .Ý 2ß [× [Ñ [ß >Ý 2ß ^Ñ ^ð
€÷+ò +r;   