§
    l¬¸jãj  ã                  ó2  — 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	 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 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/d„ Z0 G d„ d¦  «        Z1dS )a9	  
Run prediction on images, videos, directories, globs, YouTube, webcam, streams, etc.

Usage - sources:
    $ yolo 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 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_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
                         yolo26n_deepx_model        # DEEPX
                         yolo26n_qnn.onnx           # Qualcomm QNN
                         yolo26n.tflite             # LiteRT
                         yolo26n_ascend_model       # Huawei Ascend
é    )ÚannotationsN)ÚThreadPoolExecutor)Údeepcopy)ÚPath)ÚAnyÚCallable)Úget_cfgÚget_save_dir)Úload_inference_source)Ú	LetterBox)ÚLoadImagesAndVideos)ÚAutoBackend)ÚDEFAULT_CFGÚLOGGERÚMACOSÚWINDOWSÚ	callbacksÚcolorstrÚops)Úcheck_imgszÚcheck_imshow)Úclass_activation_map)Ú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              #  ó  K  — t          d¬¦  «        5 }|                     t          | ¦  «        }	 	 |                     ¦   «         }n# t          $ r Y ddd¦  «         dS w xY w|                     t          | ¦  «        }|V — ŒR# 1 swxY w Y   dS )z:Yield items while loading the next one on a worker thread.é   )Úmax_workersTN)r   ÚsubmitÚnextÚresultÚStopIteration)ÚiteratorÚexecutorÚfutureÚitems       úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/engine/predictor.pyÚ	_prefetchr(   M   sã   è è € å	¨Ð	*Ñ	*Ô	*ð ¨hØ—’¥ xÑ0Ô0ˆð	ðØ—}’}‘”��øÝ ð ð ð Øðð ð ñ ô ð ð ð ð
øøøà—_’_¥T¨8Ñ4Ô4ˆFØˆJˆJˆJð	ðð ð ð øøøð ð ð ð ð ð s4   “B±AÁBÁ
AÁBÁAÁ#BÂBÂ	B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 ed¦  «        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 | Path | None): Copy of args.data, the dataset YAML AutoBackend falls back to for class names.
        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.
        speed (dict[str, float] | None): Per-image preprocess, inference and postprocess times in ms, once run.
        pixels (int | None): Mean per-image inference area in pixels, once a run completes.
        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                óz  — t          ||¦  «        | _        t          | j        ¦  «        | _        | j        j        €d| j        _        d| _        | j        j        rt          d¬¦  «        | j        _        d| _        | j        j	        | _	        d| _
        d| _        d| _        i | _        d| _        d| _        d| _        d| _        d| _        g | _        d| _        d| _        d| _        d| _        |pt1          j        ¦   «         | _        d| _        t7          j        ¦   «         | _        t1          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ÚspeedÚpixelsÚwindowsÚscreenÚbatchÚresultsÚ
transformsr   Úget_default_callbacksÚtxt_pathÚ	threadingÚLockÚ_lockÚadd_integration_callbacks)ÚselfÚcfgr+   r-   s       r'   Ú__init__zBasePredictor.__init__…   s  € õ ˜C Ñ+Ô+ˆŒ	Ý$ T¤YÑ/Ô/ˆŒØŒ9Œ>Ð!Ø!ˆDŒIŒNØ ˆÔØŒ9Œ>ð 	5Ý)¨tÐ4Ñ4Ô4ˆDŒIŒNð ˆŒ
Ø”I”NˆŒ	ØˆŒ
ØˆŒØˆŒØˆŒØˆÔØˆÔØˆŒ	ØˆŒ
ØˆŒØˆŒØˆŒØˆŒ
ØˆŒØˆŒØ#ÐH¥yÔ'FÑ'HÔ'HˆŒØˆŒÝ”^Ñ%Ô%ˆŒ
ÝÔ+¨DÑ1Ô1Ð1Ð1Ð1ó    Úimútorch.Tensor | list[np.ndarray]Úreturnútorch.Tensorc                ó6  — t          |t          j        ¦  «        �s/|                      |¦  «        }t	          |¦  «        dk    r-t          j        |d         ¦  «                             d¦  «        n%t          j        t          j        |¦  «        ¦  «        }| 	                    | j
        ¦  «        }|                     dddd¦  «        }|j        d         dk    r|                     d¦  «        }|                     ¦   «         }| j        j        r|                     ¦   «         n|                     ¦   «                              d¦  «        }nN| 	                    | j
        ¦  «        }| j        j        r|                     ¦   «         n|                     ¦   «         }|S )aÄ  Prepare input image before inference.

        Args:
            im (torch.Tensor | list[np.ndarray]): Images of shape (N, 3, H, W) for tensor, already RGB and normalized to
                0.0-1.0, or [(H, W, 3) x N] for list of BGR uint8 arrays. See
                ultralytics.data.loaders.LoadTensor._single_check for tensor input requirements.

        Returns:
            (torch.Tensor): Preprocessed image tensor of shape (N, 3, H, W).
        r   r   é   é   éÿ   )Ú
isinstanceÚtorchÚTensorÚpre_transformÚlenÚ
from_numpyÚ	unsqueezeÚnpÚstackÚtor9   ÚpermuteÚshapeÚflipÚ
contiguousr6   Úfp16ÚhalfÚfloatÚdiv_)rL   rP   s     r'   Ú
preprocesszBasePredictor.preprocess°   s;  € õ ˜"�eœlÑ+Ô+ñ 	>Ø×#Ò# BÑ'Ô'ˆBå9<¸R¹¼ÀAº¸•Ô! " Q¤%Ñ(Ô(×2Ò2°1Ñ5Ô5Ð5Í5ÔK[Õ\^Ô\dÐegÑ\hÔ\hÑKiÔKiˆBØ—’�t”{Ñ#Ô#ˆBØ—’˜A˜q ! QÑ'Ô'ˆBØŒx˜Œ{˜aÒÐØ—W’W˜Q‘Z”Z�Ø—’‘”ˆBØ#œzœÐ>�"—'’'‘)”)�)°B·H²H±J´J×DÒDÀSÑIÔIˆBˆBà—’�t”{Ñ#Ô#ˆBØ"œjœoÐ=�—’‘”�°2·8²8±:´:ˆBØˆ	rO   c                ó`  — | j         j        p| j        j        p| j        j        }| j        j        rWt          | j        dd¦  «        rA|s?t          | j        || j	        d         | j
        g|¢R | j        j        | j        j        dœ|¤ŽS  | j        |g|¢R | j        j        | j        j        dœ|¤ŽS )zGRun inference on a given image using the specified model and arguments.Ú
base_modelTr   )r3   Úclasses)ÚaugmentÚembed)r=   Útensorr1   rn   ro   Ú	visualizeÚgetattrr6   r   rC   r2   r3   rm   )rL   rP   r1   ÚkwargsÚskips        r'   Ú	inferencezBasePredictor.inferenceÊ   sÛ   € àÔÔ&ÐN¨$¬)Ô*;ÐN¸t¼y¼ˆØŒ9Ôð 
	¥7¨4¬:°|ÀTÑ#JÔ#Jð 
	ÐSWð 
	Ý'Ø”
ØØ”
˜1”Ø”ð		ð
 ð	ð 	ð ”Y”^Øœ	Ô)ð	ð 	ð ð	ð 	ð 	ð ˆtŒz˜"Ð`˜tÐ`Ð`¨T¬YÔ->ÀdÄiÄoÐ`Ð`ÐY_Ð`Ð`Ð`rO   ú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 )	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.
        c                ó   — h | ]	}|j         ’Œ
S © )rc   )Ú.0Úxs     r'   ú	<setcomp>z.BasePredictor.pre_transform.<locals>.<setcomp>ã   s   € Ð/Ð/Ð/ q˜1œ7Ð/Ð/Ð/rO   r   ÚptÚdynamicFÚimx)ÚautoÚstridec                ó(   •— g | ]} ‰|¬ ¦  «        ‘ŒS ))Úimagery   )rz   r{   Ú	letterboxs     €r'   ú
<listcomp>z/BasePredictor.pre_transform.<locals>.<listcomp>ë   s&   ø€ Ð/Ð/Ð/ q�	�	 Ð"Ñ"Ô"Ð/Ð/Ð/rO   )	r\   r   r8   r1   Úrectr6   Úformatrr   r�   )rL   rP   Úsame_shapesr„   s      @r'   r[   zBasePredictor.pre_transformÚ   s«   ø€ õ Ð/Ð/¨BÐ/Ñ/Ô/Ñ0Ô0°AÒ5ˆÝØŒJØð vØ”	”ðvà”Ô" dÒ*Ðt­w°t´zÀ9ÈeÑ/TÔ/TÐ/sÐY]ÔYcÔYjÐnsÒYsØ”:Ô$ð
ñ 
ô 
ˆ	ð 0Ð/Ð/Ð/¨BÐ/Ñ/Ô/Ð/rO   c                ó   — |S )z6Post-process predictions for an image and return them.ry   )rL   ÚpredsÚimgÚ	orig_imgss       r'   ÚpostprocesszBasePredictor.postprocessí   s   € àˆrO   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)rL   Úsourcer6   rŽ   r1   rs   s         r'   Ú__call__zBasePredictor.__call__ñ   sg   € ð ˆŒØð 	OØ(�4Ô(¨°ÐH¸ÐHÐHÐHÀÐHÐHÐHåÐ-˜Ô-¨f°eÐM¸dÐMÐMÐMÀfÐMÐMÑNÔNÐNrO   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‘   )rL   r“   r6   ÚgenÚ_s        r'   Úpredict_clizBasePredictor.predict_cli  s5   € ð  ×#Ò# F¨EÑ2Ô2ˆØð 	ð 	ˆAØð	ð 	rO   r�   ú
int | Nonec                ó¬  — t          | j        d¦  «        r,t          | j        dd¦  «        s| j        j        | j        _        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.
        r8   r~   FrV   )r�   Úmin_dimÚchannelsrU   )r“   rC   Ú
vid_strideÚbufferrœ   iè  Ú
video_flagr   NrŽ   T)Úhasattrr6   rr   r8   r1   r   r�   r   rC   r�   Ústream_bufferr:   r=   rŽ   Ú
screenshotr\   ÚanyÚtorchvisionr   ÚwarningÚSTREAM_WARNINGr;   )rL   r“   r�   r¤   s       r'   Úsetup_sourcezBasePredictor.setup_source  s@  € õ �4”:˜wÑ'Ô'ð 	/µ¸¼
ÀIÈuÑ0UÔ0Uð 	/Ø"œjÔ.ˆDŒIŒOÝ  ¤¤¸Ð9TÀ4Ä:ÔCTÐ^_Ð`Ñ`Ô`ˆŒ
Ý,ØØ”)”/Ø”yÔ+Ø”9Ô*Ý˜TœZ¨°QÑ7Ô7ð
ñ 
ô 
ˆŒð  œ<Ô3ˆÔàÔÔ#ð		/àÔÔ*ð		/õ �4”<Ñ Ô  4Ò'Ð'Ý•7˜4œ<¨¸°wÑ?Ô?Ñ@Ô@ð (ð ÐÐÐå˜4 ¨4Ñ0Ô0ð /Ý”�~Ñ.Ô.Ð.ØˆŒˆˆrO   c              /  ó¤  ‡ ‡K  — ‰ j         j        rt          j        d¦  «         ‰ j        €‰                      |¦  «         t          ‰ j        dd¦  «        sXˆ fd„dD ¦   «         x}rHt          j        |› d‰ j        j        › d�¦  «         d	\  ‰ j         _	        ‰ j         _
        ‰ j         _        ‰ j        5  ‰ j        j        d
k    rO‰ j        j        rC‰ j        j                             t          ‰ j         j        d¦  «        ‰ j         j        ¬¦  «         ‰                      |�|n‰ j         j        ¦  «         ‰ j         j        s‰ j         j        r2‰ j         j        r
‰ j        dz  n‰ j                             dd¬¦  «         dddg df\  ‰ _        ‰ _        ‰ _        ‰ _        ‰ _        d}t;          j        ‰ j        ¬¦  «        t;          j        ‰ j        ¬¦  «        t;          j        ‰ j        ¬¦  «        f}‰                       d¦  «         tC          ‰ j"        ¦  «        }‰ j        j#        dk    r[tI          ‰ j"        tJ          ¦  «        rA‰ j"        j&        ‰ j"        j'        k    r'tQ          ‰ j"        ¦  «        dk    rtS          |¦  «        }|D �]¾}	|	‰ _        ‰                       d¦  «         ‰ j        \  }
}}|d         5  ‰  *                    |¦  «        }ddd¦  «         n# 1 swxY w Y   ‰ j+        s"‰ j         ,                    |¬¦  «         d‰ _+        |d         5   ‰ j-        |g|¢R i |¤Ž}‰ j         j
        r2tI          |t\          j/        ¦  «        r|gn|E d{V —† 	 ddd¦  «         Œà	 ddd¦  «         n# 1 swxY w Y   |d         5  ‰  0                    |||¦  «        ‰ _1        ddd¦  «         n# 1 swxY w Y   ‰                       d¦  «         tQ          |¦  «        }	 te          |¦  «        D ]î}‰ xj        dz  c_        ||j3        d         |j3        d         z  z  }|d         j4        dz  |z  |d         j4        dz  |z  |d         j4        dz  |z  dœ‰ j1        |         _        ‰ j         j        s0‰ j         j        s$‰ j         j        s‰ j         j5        s‰ j         j6        r9||xx         ‰  7                    |tq          |
|         ¦  «        ||¦  «        z  cc<   Œïn# tr          $ r Y  n[w xY w‰ j         j        r't          j        d :                    |¦  «        ¦  «         ‰                       d¦  «         ‰ j1        E d{V —† �ŒÀ‰ j        xŠr¸tw          ˆfd„|D ¦   «         ¦  «        }ty          t{          d|¦  «        ¦  «        ‰ _        t}          |‰z  ¦  «        ‰ _        ‰ j         j        rXt          j        dt          ‰ j         j        ‰¦  «        t          ‰ j        dd¦  «        g|j3        dd…         ¢R › �|z  ¦  «         ddd¦  «         n# 1 swxY w Y   ‰ j@         A                    ¦   «         D ]0}tI          |t„          jC        ¦  «        r| D                    ¦   «          Œ1‰ j         j6        rt…          jE        ¦   «          ‰ j         j        s‰ j         j        s‰ j         j5        rˆtQ          t�          ‰ j         G                    d ¦  «        ¦  «        ¦  «        }‰ j         j        rd|› d!d"|dk    z  › d#‰ j        dz  › �nd}t          j        d$t‘          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.
        Ú Nrl   Tc                ó>   •— g | ]}t          ‰j        |¦  «        ¯|‘ŒS ry   )rr   r1   )rz   ÚkrL   s     €r'   r…   z2BasePredictor.stream_inference.<locals>.<listcomp>M  s-   ø€ ÐbÐbÐb !ÍGÐTXÔT]Ð_`ÑLaÔLaÐb˜AÐbÐbÐbrO   )rn   ro   rq   z& not supported by this model (format='z'), ignoring.)FNFr}   é,  )Úmax_detÚagnostic_nmsÚlabels©ÚparentsÚexist_okr   )r9   Úon_predict_startÚcudar   Úon_predict_batch_start)rP   rV   Úon_predict_postprocess_endrU   ç     @�@)rj   ru   r�   ú
Úon_predict_batch_endc              3  ó2   •K  — | ]}|j         ‰z  d z  V — ŒdS )r·   N)Út)rz   r{   r>   s     €r'   ú	<genexpr>z1BasePredictor.stream_inference.<locals>.<genexpr>¦  s.   øè è € Ð>Ð>¨q˜!œ# ™* sÑ*Ð>Ð>Ð>Ð>Ð>Ð>rO   zRSpeed: %.1fms preprocess, %.1fms inference, %.1fms postprocess per image at shape rœ   zlabels/*.txtz labelÚsz
 saved to zResults saved to ÚboldÚon_predict_end)Ir1   Úverboser   Úinfor6   Úsetup_modelrr   r¥   r‡   rn   ro   rq   rJ   Úend2endÚset_head_attrÚmaxr­   r®   r§   r“   ÚsaveÚsave_txtr2   Úmkdirr>   r?   r@   rA   rC   r   ÚProfiler9   Úrun_callbacksÚiterr:   ÚtyperX   r   ÚniÚnfr\   r(   rj   r4   Úwarmupru   rY   rZ   r�   rD   Úrangerc   ÚdtÚ	save_cropr5   Úwrite_resultsr   r"   ÚjoinÚtupleÚdictÚzipÚroundÚminr;   ÚvaluesÚcv2ÚVideoWriterÚreleaseÚdestroyAllWindowsr’   Úglobr   )rL   r“   r6   r1   rs   ÚunsupportedÚpxÚ	profilersÚbatchesrC   ÚpathsÚim0sr½   rP   rŠ   ÚnÚir»   ÚvÚnlr>   s   `                   @r'   r‘   zBasePredictor.stream_inference8  sè  øøè è € ð Œ9Ôð 	ÝŒK˜‰OŒOˆOð Œ:ÐØ×Ò˜UÑ#Ô#Ð#Ý�t”z <°Ñ6Ô6ð 	YØbÐbÐbÐbÐ'HÐbÑbÔbÐbˆKð	Yõ ŒN˜kÐqÐqÐQUÔQ[ÔQbÐqÐqÐqÑrÔrÐrØFXÑCˆDŒIÔ˜tœyœ°´	Ô0CàŒZð [	ð [	ØŒzÔ  DÒ(Ð(¨T¬ZÔ-?Ð(à”
Ô ×.Ò.µs¸4¼9Ô;LÈcÑ7RÔ7RÐaeÔajÔawÐ.ÑxÔxÐxà×Ò¨Ð(:˜f˜fÀÄ	Ô@PÑQÔQÐQð ŒyŒ~ð w ¤Ô!3ð wØ-1¬YÔ-?ÐR�” Ñ)Ð)ÀTÄ]×YÒYÐbfÐquÐYÑvÔvÐvàKLÈdÐTXÐZ\Ð^bÐKbÑHˆDŒI�t”z 4¤;°´¸d¼jØˆBå” 4¤;Ð/Ñ/Ô/Ý” 4¤;Ð/Ñ/Ô/Ý” 4¤;Ð/Ñ/Ô/ðˆIð
 ×ÒÐ1Ñ2Ô2Ð2Ý˜4œ<Ñ(Ô(ˆGà”Ô  FÒ*Ð*Ý˜tœ|Õ-@ÑAÔAð +à”L”O t¤|¤Ò6Ð6Ý˜œÑ%Ô%¨Ò)Ð)å# GÑ,Ô,�Ø ð 4(ñ 4(�Ø"�”
Ø×"Ò"Ð#;Ñ<Ô<Ð<Ø!%¤‘��t˜Qð ˜q”\ð /ð /ØŸš¨Ñ.Ô.�Bð/ð /ð /ñ /ô /ð /ð /ð /ð /ð /ð /øøøð /ð /ð /ð /ð Ô'ð ,Ø”J×%Ò%¨Ð%Ñ,Ô,Ð,Ø'+�DÔ$ð ˜q”\ð !ð !Ø*˜DœN¨2Ð?°Ð?Ð?Ð?¸Ð?Ð?�EØ”y”ð !Ý.8¸ÅÄÑ.MÔ.MÐ#X E 7 7ÐSXÐXÐXÐXÐXÐXÐXÐXØ ð	!ð !ð !ñ !ô !ð !ð !ð!ð!ð !ð !ñ !ô !ð !ð !ð !ð !ð !ð !øøøð !ð !ð !ð !ð ˜q”\ð Eð EØ#'×#3Ò#3°E¸2¸tÑ#DÔ#D�D”LðEð Eð Eñ Eô Eð Eð Eð Eð Eð Eð Eøøøð Eð Eð Eð Eà×"Ò"Ð#?Ñ@Ô@Ð@õ ˜‘I”I�ðÝ" 1™XœXð Qð Q˜Ø˜	œ	 Q™˜	œ	Ø˜bœh qœk¨B¬H°Q¬KÑ7Ñ7˜à*3°A¬,¬/¸CÑ*?À!Ñ*CØ)2°1¬¬¸3Ñ)>ÀÑ)BØ+4°Q¬<¬?¸SÑ+@À1Ñ+Dð1ð 1˜œ QœÔ-ð !œIÔ-ðQà#œyœ~ðQð  $œyÔ1ðQð  $œyÔ2ð	Qð
  $œyœ~ðQð ˜a˜D˜DœD D×$6Ò$6°q½$¸uÀQ¼x¹.¼.È"ÈaÑ$PÔ$PÑP˜D˜D™DøðQøõ  %ð ð ð Ø�E�Eðøøøð ”9Ô$ð .Ý”K §	¢	¨!¡¤Ñ-Ô-Ð-à×"Ò"Ð#9Ñ:Ô:Ð:Øœ<Ð'Ð'Ð'Ð'Ð'Ð'Ð'Ñ'ð ”yÐ ˆtð ÝÐ>Ð>Ð>Ð>°IÐ>Ñ>Ô>Ñ>Ô>�Ý!¥#Ð&PÐRSÑ"TÔ"TÑUÔU�”
Ý# B¨¡IÑ.Ô.�”Ø”9Ô$ð Ý”KðmÝ ¤	¤°Ñ6Ô6½ÀÄ
ÈJÐXYÑ8ZÔ8ZÐjÐ]_Ô]eÐfgÐfhÐfhÔ]iÐjÐjðmð mØopñqñô ð ðq[	ð [	ð [	ñ [	ô [	ð [	ð [	ð [	ð [	ð [	ð [	øøøð [	ð [	ð [	ð [	ð| ”×'Ò'Ñ)Ô)ð 	ð 	ˆAÝ˜!�Sœ_Ñ-Ô-ð Ø—	’	‘”�øàŒ9Œ>ð 	$ÝÔ!Ñ#Ô#Ð#àŒ9Œ>ð 	R˜TœYÔ/ð 	R°4´9Ô3Fð 	RÝ•T˜$œ-×,Ò,¨^Ñ<Ô<Ñ=Ô=Ñ>Ô>ˆBØW[ÔW`ÔWiÐqÐS�RÐSÐS˜s b¨1¢f™~ÐSÐS¸¼ÈÑ9QÐSÐSÐSÐoqˆAÝŒKÐP­H°V¸T¼]Ñ,KÔ,KÐPÈQÐPÐPÑQÔQÐQØ×ÒÐ+Ñ,Ô,Ð,Ð,Ð,s¬   Â6H
XË K"ËXË"K&Ë&XË)K&Ë*4XÌAM;Í"XÍ/XÍ;M?Í?XÎM?ÎXÎN7Î+XÎ7N;Î;XÎ>N;Î?'XÏ'C>S&Ó%XÓ&
S4Ó0XÓ3S4Ó4DXØXØ"XTrÀ   c                óÂ  — t          |¦  «        }t          |p| j        j        t	          | j        j        |¬¦  «        | j        j        | j        j        | j        j        dk    | j        j	        d|| j        j
        du ¬¦	  «	        | _        | j        j        | _        | j                             ¦   «          t          | j        | j        | j        j        ¬¦  «        | _        dS )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.
        )rÀ   é   TF)	r6   r9   Údnnr7   rf   Úchannels_lastÚfuserÀ   rÃ   )r9   ÚmodeN)r   r   r1   r6   r   r9   rì   r7   Úquantizerí   ÚnmsÚevalr   Úcompile)rL   r6   rÀ   s      r'   rÂ   zBasePredictor.setup_model½  s¾   € õ ˜‘”ˆÝ ØÐ*˜4œ9œ?Ý  ¤Ô!1¸7ÐCÑCÔCØ”	”Ø””Ø”Ô# rÒ)Øœ)Ô1ØØØ”I”M UÐ*ð

ñ 

ô 

ˆŒ
ð ”jÔ'ˆŒØŒ
�ŠÑÔÐÝ$ T¤Z¸¼È$Ì)ÔJ[Ð\Ñ\Ô\ˆŒ
ˆ
ˆ
rO   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        rF|                     | j        j        | j        j        | j        j        | j        j        ¬¦  «        | _        | j        j        r)|                     | j        › d�| j        j        ¬¦  «         | j        j        r)|                     | j        dz  | j        j        ¬¦  «         | j        j        r"|                      tA          |¦  «        ¦  «         | 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©   rU   Nz: zframe (\d+)/r   r¯   rƒ   r—   z
{:g}x{:g} rV   ru   z.1fÚms)Ú
line_widthÚboxesr3   r¯   z.txt)Ú	save_confÚcrops)r2   Ú	file_name)#r\   rc   r=   rŽ   Úfrom_imgrp   r:   ÚcountÚreÚsearchrô   r2   Ústemrï   rG   r‡   rD   Ú__str__rÀ   r?   r1   rÆ   r5   Úplotrú   Ú
show_boxesÚ	show_confÚshow_labelsr<   rÇ   rü   rÒ   r÷   Úsave_predicted_imagesÚname)	rL   rç   rõ   rP   r½   ÚstringÚframeÚmatchr!   s	            r'   rÓ   zBasePredictor.write_resultsÖ  sK  € ð ˆÝˆrŒx‰=Œ=˜AÒÐØ�D”ˆBØÔÔ"ð 	5 dÔ&6Ô&?ð 	5À4ÔCSÔCZð 	5Ø˜�h�h�hÑˆFØ”LÔ&ˆEˆEå”I˜o¨q°¬tÑ4Ô4ˆEØ%*Ð4•C˜˜aœ‘M”M�M°ˆEàœ¨Ñ0°A´FÀDÄLÔDUÐY`ÒD`ÐD`¸b¸bÐfqÐjoÐfqÐfqÑ4rÑsˆŒØÐ%�,Ô% r¤x°°°¤|Ð4Ñ4ˆØ”˜a”ˆØœ-×/Ò/Ñ1Ô1ˆŒØ�V—^’^Ñ%Ô%ÐH v¤|°KÔ'@ÐHÐHÐHÐHÑHˆð Œ9Œ>ð 	˜TœYœ^ð 	Ø%Ÿ{š{Øœ9Ô/Ø”iÔ*Ø”YÔ(Ø”yÔ,ð	  +ñ  ô  ˆDÔð Œ9Ôð 	SØ�OŠO˜tœ}Ð2Ð2Ð2¸d¼iÔ>QˆOÑRÔRÐRØŒ9Ôð 	]Ø×Ò d¤m°gÑ&=ÈÌÔI[ÐÑ\Ô\Ð\ØŒ9Œ>ð 	Ø�IŠI•c˜!‘f”fÑÔÐØŒ9Œ>ð 	FØ×&Ò& t¤}°q´vÑ'=¸uÑEÔEÐEàˆrO   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 )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.
        >   ÚvideorŽ   r  é   Ú_framesTr°   )z.mp4Úavc1)ú.aviÚWMV2)r  ÚMJPGr   r   )ÚfilenameÚfourccÚfpsÚ	frameSizeú/r—   z.jpgN)r<   r:   rï   r  r2   r  r;   r1   Úsave_framesr   rÈ   r   r   rÛ   rÜ   r÷   Úwith_suffixÚVideoWriter_fourccrc   ÚwriteÚimwrite)rL   r  r  rP   r  Úframes_pathÚsuffixr  s           r'   r	  z#BasePredictor.save_predicted_images  s©  € ð Ôˆð Œ<ÔÐ 3Ð3Ñ3Ø&*¤lÔ&7¸7Ò&BÐ&B�$”,Ô"Ð"ÈˆCØœ-¨Y¬^Ð*DÐ*DÐ*DÑDˆKØ ¤Ð/Ð/Ø”9Ô(ð IÝ˜Ñ%Ô%×+Ò+°DÀ4Ð+ÑHÔHÐHÝ5:Ð!qÐ!1Ð!1ÕT[Ð@qÐ@PÐ@PÐaq‘�˜Ý-0¬_Ý ¥ i¡¤×!<Ò!<¸VÑ!DÔ!DÑEÔEÝÔ1°6Ð:ØØ!œx¨œ{¨B¬H°Q¬KÐ8ð	.ñ .ô .�” 	Ñ*ð ŒO˜IÔ&×,Ò,¨RÑ0Ô0Ð0ØŒyÔ$ð OÝ”˜{ÐIÐI¨Y¬^ÐIÐI¸eÐIÐIÐIÈ2ÑNÔNÐNÐNÐNðOð Oõ
 ŒK�˜I×1Ò1°&Ñ9Ô9Ñ:Ô:¸BÑ?Ô?Ð?Ð?Ð?rO   r©   c                óR  — | j         }t          j        ¦   «         dv �r³|| j        v�r©| j                             |¦  «         |                     d¦  «                             ¦   «         }t          j        |t          j	        t          j
        z  ¦  «         |j        dd…         \  }}	 | j        €|t          d¦  «                             ¦   «         }|                     ¦   «          d|                     ¦   «         z  d|                     ¦   «         z  f| _        |                     ¦   «          t'          | j        d         |z  | j        d         |z  d	¦  «        }t          j        |t+          dt-          ||z  ¦  «        ¦  «        t+          dt-          ||z  ¦  «        ¦  «        ¦  «         n&# t.          $ r t          j        |||¦  «         Y nw xY wt          j        ||¦  «         t          j        | j        j        d
k    rdnd¦  «        dz  t9          d¦  «        k    rt:          ‚dS )zDisplay an image in a window.>   ÚLinuxÚWindowsÚunicode_escapeNrV   ÚtkintergÍÌÌÌÌÌì?r   r   g      ð?rƒ   r¬   rW   Úq)r<   ÚplatformÚsystemrA   ÚappendÚencodeÚdecoderÛ   ÚnamedWindowÚWINDOW_NORMALÚWINDOW_KEEPRATIOrc   rB   Ú
__import__ÚTkÚwithdrawÚwinfo_screenwidthÚwinfo_screenheightÚdestroyrÙ   ÚresizeWindowrÅ   rô   Ú	ExceptionÚimshowÚwaitKeyr:   rï   Úordr"   )rL   rõ   rP   r
  ÚhÚwÚrootÚrs           r'   r5   zBasePredictor.show(  sî  € àÔˆÝŒ?ÑÔÐ 4Ð4Ñ4¸À$Ä,Ð9NÑ9NØŒL×Ò Ñ"Ô"Ð"Ø—8’8Ð,Ñ-Ô-×4Ò4Ñ6Ô6ˆDÝŒO˜D¥#Ô"3µcÔ6JÑ"JÑKÔKÐKØ”8˜B˜Q˜B”<‰DˆAˆqð	-Ø”;Ð&Ý% iÑ0Ô0×3Ò3Ñ5Ô5�DØ—M’M‘O”O�OØ"%¨×(>Ò(>Ñ(@Ô(@Ñ"@À#È×H_ÒH_ÑHaÔHaÑBaÐ"a�D”KØ—L’L‘N”N�NÝ˜œ Aœ¨Ñ*¨D¬K¸¬N¸QÑ,>ÀÑDÔD�ÝÔ  ¥s¨1­c°!°a±%©j¬jÑ'9Ô'9½3¸qÅ#ÀaÈ!ÁeÁ*Ä*Ñ;MÔ;MÑNÔNÐNÐNøÝð -ð -ð -ÝÔ   q¨!Ñ,Ô,Ð,Ð,Ð,ð-øøøåŒ
�1�bÑÔÐÝŒ;˜dœlÔ/°7Ò:Ð:�s�sÀÑBÔBÀTÑIÍSÐQTÉXÌXÒUÐUÝÐð VÐUs   Â(DF+ Æ+ GÇGÚeventc                óX   — | j                              |g ¦  «        D ]} || ¦  «         ŒdS )z2Run all registered callbacks for a specific event.N)r   Úget)rL   r@  Úcallbacks      r'   rÊ   zBasePredictor.run_callbacks>  s<   € àœ×*Ò*¨5°"Ñ5Ô5ð 	ð 	ˆHØˆH�T‰NŒNˆNˆNð	ð 	rO   Úfuncr   c                óF   — | j         |                              |¦  «         dS )z-Add a callback function for a specific event.N)r   r+  )rL   r@  rD  s      r'   Úadd_callbackzBasePredictor.add_callbackC  s#   € àŒ�uÔ×$Ò$ TÑ*Ô*Ð*Ð*Ð*rO   )r+   r,   r-   r.   )rP   rQ   rR   rS   )rP   rS   )rP   rv   rR   rv   )NNF)rŽ   r�   )NN)N)r�   r™   )T)rÀ   r�   )
rç   rô   rõ   r   rP   rS   r½   rö   rR   r÷   )r   )r  r   r  rô   )r©   )rõ   r÷   )r@  r÷   )r@  r÷   rD  r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rN   rj   ru   r[   r�   r”   r˜   r§   r   r‘   rÂ   rÓ   r	  r5   rÊ   rF  ry   rO   r'   r*   r*   Z   s¸  € € € € € ð(ð (ðX Ø+/Ø"&ð	)2ð )2ð )2ð )2ð )2ðVð ð ð ð4að að að að 0ð 0ð 0ð 0ð&ð ð ðOð Oð Oð Oð Oð(ð ð ð ð(ð ð ð ð ð> ÐÑÔðB-ð B-ð B-ñ ÔðB-ðH Ð˜%Ñ Ô ð]ð ]ð ]ð ]ñ !Ô ð]ð0/ð /ð /ð /ðb@ð @ð @ð @ð @ðB ð  ð  ð  ð  ð,ð ð ð ð
+ð +ð +ð +ð +ð +rO   r*   )2rJ  Ú
__future__r   r)  r  rH   Úconcurrent.futuresr   Úcopyr   Úpathlibr   Útypingr   r   rÛ   Únumpyr_   rY   Úultralytics.cfgr	   r
   Úultralytics.datar   Úultralytics.data.augmentr   Úultralytics.data.loadersr   Úultralytics.nn.autobackendr   Úultralytics.utilsr   r   r   r   r   r   r   Úultralytics.utils.checksr   r   Úultralytics.utils.plottingr   Úultralytics.utils.torch_utilsr   r   r   r¦   r(   r*   ry   rO   r'   ú<module>rZ     sÞ  ðð$ð $ðL #Ð "Ð "Ð "Ð "Ð "à €€€Ø 	€	€	€	Ø Ð Ð Ð Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à 
€
€
€
Ø Ð Ð Ð Ø €€€à 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø .Ð .Ð .Ð .Ð .Ð .Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ð [Ø >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^ð
€ð
ð 
ð 
ðk+ð k+ð k+ð k+ð k+ñ k+ô k+ð k+ð k+ð k+rO   