
    hX                        S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
r
SSKrSSKrSSKJrJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJrJrJrJrJr  SSKJ r J!r!  SSK"J#r#  SSK$J%r%J&r&J'r'  Sr( " S S5      r)g)ar  
Run prediction on images, videos, directories, globs, YouTube, webcam, streams, etc.

Usage - sources:
    $ yolo mode=predict model=yolo11n.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=yolo11n.pt                 # PyTorch
                              yolo11n.torchscript        # TorchScript
                              yolo11n.onnx               # ONNX Runtime or OpenCV DNN with dnn=True
                              yolo11n_openvino_model     # OpenVINO
                              yolo11n.engine             # TensorRT
                              yolo11n.mlpackage          # CoreML (macOS-only)
                              yolo11n_saved_model        # TensorFlow SavedModel
                              yolo11n.pb                 # TensorFlow GraphDef
                              yolo11n.tflite             # TensorFlow Lite
                              yolo11n_edgetpu.tflite     # TensorFlow Edge TPU
                              yolo11n_paddle_model       # PaddlePaddle
                              yolo11n.mnn                # MNN
                              yolo11n_ncnn_model         # NCNN
                              yolo11n_imx_model          # Sony IMX
                              yolo11n_rknn_model         # Rockchip RKNN
    )annotationsN)Path)Any)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, causing potential 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                      \ rS rSrSr\SS4   SS jjrSS jrSS jrSS jr	S r
SSS	 jjrSS
 jrS r\" 5       SS j5       rSSS jjrSS jrSSS jjrS S!S jjrS"S jrS#S jrSrg)$BasePredictorE   a|  
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 (dict): Data configuration.
    device (torch.device): Device used for prediction.
    dataset (Dataset): Dataset used for prediction.
    vid_writer (dict[str, 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                   [        X5      U l        [        U R                  5      U l        U R                  R                  c  SU R                  l        SU l        U R                  R                  (       a  [        SS9U R                  l        SU l        U R                  R                  U l	        SU l
        SU l        SU l        0 U l        SU l        SU l        SU l        / U l        SU l        SU l        SU l        U=(       d    [*        R,                  " 5       U l        SU l        [0        R2                  " 5       U l        [*        R6                  " U 5        g)z
Initialize the BasePredictor class.

Args:
    cfg (str | dict): 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       V/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/engine/predictor.py__init__BasePredictor.__init__o   s     C+	$TYY/99>>!!DIIN 99>>)t4DIIN 
IINN	
	
#Hy'F'F'H^^%
++D1    c                   [        U[        R                  5      (       + nU(       a  [        R                  " U R                  U5      5      nUR                  S   S:X  a
  USSSS24   nUR                  S5      n[        R                  " U5      n[        R                  " U5      nUR                  U R                  5      nU R                  R                  (       a  UR                  5       OUR                  5       nU(       a  US-  nU$ )z
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   
preprocessBasePredictor.preprocess   s     $B55
$,,R01Bxx|q TrT	]l+B%%b)B!!"%BUU4;;**//RWWYrxxz#IB	r<   c                h   U R                   R                  (       aU  U R                  R                  (       d:  [	        U R
                  [        U R                  S   S   5      R                  -  SS9OSnU R                  " U/UQ7U R                   R                  X@R                   R                  S.UD6$ )zGRun inference on a given image using the specified model and arguments.r   T)mkdirF)augment	visualizeembed)r   rX   r)   tensorr   r   r   r,   stemr"   rW   rY   )r5   rQ   r   kwargsrX   s        r9   	inferenceBasePredictor.inference   s     yy""D,<,<,C,C 4==4

1a0@+A+F+FFdS 	
 zz"ufjudii&7&79T]T]TcTcuntuur<   c                   [        U Vs1 s H  o"R                  iM     sn5      S:H  n[        U R                  U=(       as    U R                  R
                  =(       aV    U R                  R                  =(       d9    [        U R                  SS5      =(       a    U R                  R                  (       + U R                  R                  S9nU Vs/ s H  o$" US9PM
     sn$ s  snf s  snf )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@   dynamicF)autostride)image)lenrI   r	   r$   r   rectr"   ptgetattrimxrb   )r5   rQ   xsame_shapes	letterboxs        r9   rH   BasePredictor.pre_transform   s     B/Bq77B/0A5JJ b		b`74::y%#H#_QUQ[Q[Q_Q_M_::$$
	 -//Bq	"B// 0 0s   C"C'c                    U$ )z6Post-process predictions for an image and return them. )r5   predsimg	orig_imgss       r9   postprocessBasePredictor.postprocess   s    r<   c                    X0l         U(       a  U R                  " X/UQ70 UD6$ [        U R                  " X/UQ70 UD65      $ )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"   ru   r   r\   s         r9   __call__BasePredictor.__call__   sG     ((HHHH--fMdMfMNNr<   c                8    U R                  X5      nU H  nM     g)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.

Note:
    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)rv   )r5   rx   r"   gen_s        r9   predict_cliBasePredictor.predict_cli   s!    " ##F2A r<   c                   [        U R                  R                  U R                  R                  SS9U l        [        UU R                  R                  U R                  R                  U R                  R                  [        U R                  SS5      S9U l
        U R                  R                  U l        U R                  R                  =(       d]    U R                  R                  =(       d@    [        U R                  5      S:  =(       d!    [        [        U R                  SS/5      5      nU(       a0  S	S
Kn[        U SS5      (       d  ["        R$                  " [&        5        0 U l        g
)z
Set up source and inference mode.

Args:
    source (str | Path | list[str] | list[Path] | list[np.ndarray] | np.ndarray | torch.Tensor):
        Source for inference.
rA   )rb   min_dimchr?   )rx   r,   
vid_stridebufferchannelsi  
video_flagFr   Nru   T)r   r   r$   r"   rb   r   r,   r   stream_bufferrg   r&   r)   ru   
screenshotrd   anytorchvisionr   warningSTREAM_WARNINGr'   )r5   rx   long_sequencer   s       r9   setup_sourceBasePredictor.setup_source   s    !9J9JTUV
,))//yy++99**TZZq1
  <<33## A**A4<< 4'A 74<<w?@	 	 4400~.r<   c              /    ^ #    T R                   R                  (       a  [        R                  " S5        T R                  (       d  T R                  U5        T R                     T R                  Ub  UOT R                   R                  5        T R                   R                  (       d  T R                   R                  (       aD  T R                   R                  (       a  T R                  S-  OT R                  R                  SSS9  T R                  (       d  T R                  R                  T R                  R                  (       d  T R                  R                   (       a  SOT R"                  R$                  T R                  R&                  /T R(                  Q7S9  ST l        S/ SsT l        T l        T l        [0        R2                  " T R4                  S	9[0        R2                  " T R4                  S	9[0        R2                  " T R4                  S	94nT R7                  S
5        T R"                   GHx  T l        T R7                  S5        T R.                  u  pgnUS      T R9                  U5      n	SSS5        US      T R:                  " W	/UQ70 UD6n
T R                   R<                  (       a7  [?        U
[@        RB                  5      (       a  U
/OU
 Sh  vN    SSS5        M   SSS5        US      T RE                  W
W	U5      T l#        SSS5        T R7                  S5        [I        U5      n [K        U5       GH  nT =R*                  S-  sl        US   RL                  S-  U-  US   RL                  S-  U-  US   RL                  S-  U-  S.T RF                  U   l'        T R                   R                  (       dS  T R                   R                  (       d8  T R                   R                  (       d  T R                   RP                  (       d  M  X==   T RS                  U[U        Xl   5      W	U5      -  ss'   GM     T R                   R                  (       a%  [        R                  " SRY                  U5      5        T R7                  S5        T RF                   Sh  vN   GM{     SSS5        T RZ                  R]                  5        H4  n[?        U[^        R`                  5      (       d  M$  URc                  5         M6     T R                   RP                  (       a  [^        Rd                  " 5         T R                   R                  (       a  T R*                  (       a  [g        U 4S jW 5       5      n[        R                  " S[i        T R                   R.                  T R*                  5      [k        T R                  SS5      /W	Rl                  SS Q7 3U-  5        T R                   R                  (       d6  T R                   R                  (       d  T R                   Rn                  (       a  [I        [q        T R                  Rs                  S5      5      5      nT R                   R                  (       a  SU SSUS:  -   ST R                  S-   3OSn[        R                  " S[u        ST R                  5       U 35        T R7                  S5        g! , (       d  f       GNt= f GN! , (       d  f       GN= f! , (       d  f       GN= f! [V         a       GMx  f = f GN! , (       d  f       GN= f7f)a  
Stream real-time inference on camera feed 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_okr@   )r$   r   )r%   on_predict_starton_predict_batch_startrA   on_predict_postprocess_end     @@)rS   r]   rr   
on_predict_batch_endc              3  Z   >#    U  H   oR                   TR                  -  S -  v   M"     g7f)r   N)tr*   ).0ri   r5   s     r9   	<genexpr>1BasePredictor.stream_inference.<locals>.<genexpr>z  s      ?YccDIIo+Ys   (+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   rx   savesave_txtr   rV   r    warmuprf   tritonr&   bsr   r$   r*   r+   r,   r   Profiler%   run_callbacksrS   r]   rY   rC   rD   rE   rr   r-   rd   rangedtspeedr!   write_resultsr   StopIterationjoinr'   valuescv2VideoWriterreleasedestroyAllWindowstupleminrg   rI   	save_croprw   globr   )r5   rx   r"   r   r\   	profilerspathsim0sr   rQ   ro   nivr   nls   `               r9   rv   BasePredictor.stream_inference  s$     99KKO zzU#ZZ(:f		@P@PQ yy~~!3!3-1YY-?-?)T]]YYbfquYv ##

!! $

1B1B1Y]YcYcYfYftimisist "  $( 23R/DIt|TZ4;;/4;;/4;;/I
 12"ll
""#;<!%Q q\.B " q\ NN2???Eyy.8.M.ME7SXXX 	 "\& " q\#'#3#3E2t#DDL """#?@ I"1X		Q	*3A,//C*?!*C)213)>)B+4Q<??S+@1+D1Q-
  99,,		$))BTBTX\XaXaXfXfXfDD$6$6q$ux."a$PPD & 99$$KK		!-""#9:<<''Q +- B '')A!S__--		 * 99>>!!# 99?Y??AKKd		3WTZZq5QaTVT\T\]^]_T`abdfgh 99>>TYY//4993F3FT$--,,^<=>BW[W`W`WiWi"RDsb1f~.j9Q8RSoqAKK+HVT]],K+LQCPQ+,q "\ Y "\ "\  %  (} Zs   A ]#G?\0"[!4\0A[6[3
[6 
\0+\08\$\06C\.\A \0'\-(	\01A]7G*]!
[0+\03[66
\ \0
\\0
\*$\0)\**\00
\?:]c           
        [        U=(       d    U R                  R                  [        U R                  R                  US9U R                  R
                  U R                  R                  U R                  R                  SUS9U l        U R                  R                  U l        U R                  R                  U R                  l        [        U R                  S5      (       aA  [        U R                  SS5      (       d%  U R                  R                  U R                  l        U R                  R                  5         [        U R                  U R                  U R                  R                  S9U l        g)	z
Initialize YOLO model with given parameters and set it to evaluation mode.

Args:
    model (str | Path | torch.nn.Module, optional): Model to load or use.
    verbose (bool): Whether to print verbose output.
)r   T)r"   r%   dnnr#   rN   fuser   r$   r`   F)r%   modeN)r
   r   r"   r   r%   r   r#   rO   rN   hasattrrg   r$   evalr   compile)r5   r"   r   s      r9   r   BasePredictor.setup_model  s     !*499?? !1!17C		

 jj''		4::w''

Iu0U0U"jj..DIIO

$TZZ$))J[J[\
r<   c                   Sn[        UR                  5      S:X  a  US   nU R                  R                  (       d6  U R                  R                  (       d  U R                  R
                  (       a  XQ S3-  nU R                  R                  nO0[        R                  " SXA   5      nU(       a  [        US   5      OSnU R                  S-  UR                  U R                  R                  S:X  a  SOS	U 3-   -  U l        US
R                  " UR                  SS 6 -  nU R                   U   nU R                  R#                  5       Ul        XXR%                  5        UR&                  S   S S3-  nU R(                  R*                  (       d  U R(                  R,                  (       a  UR/                  U R(                  R0                  U R(                  R2                  U R(                  R4                  U R(                  R6                  U R(                  R8                  (       a  SOX1   S9U l        U R(                  R<                  (       a1  UR=                  U R                   S3U R(                  R>                  S9  U R(                  R@                  (       a1  URA                  U R                  S-  U R                  R                  S9  U R(                  R,                  (       a  U R-                  [C        U5      5        U R(                  R*                  (       a)  U RE                  U R                  URF                  -  U5        U$ )a)  
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   rc   r}   z
{:g}x{:g} rA   r]   z.1fms)
line_widthboxesr   r   im_gpuz.txt)	save_confcrops)r   	file_name)$rd   rI   r)   ru   from_imgrZ   r&   countresearchintr   r[   r   r0   formatr-   __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   prQ   r   stringframematchresults	            r9   r   BasePredictor.write_results  sS    rxx=ADB""d&6&6&?&?4CSCSCZCZ2hFLL&&EIIoqt4E%*CaME0AFFDLLDUDUY`D`bhijoipfq4rs,%%rxx|44a--//1^^%&v||K'@&ERHH 99>>TYY^^%{{99//ii**YY((yy,,#yy55t25  +  D 99OOt}}oT2dii>Q>QOR99dmmg&=I[I[\99>>IIc!f99>>&&t}}qvv'=uEr<   c                   U R                   nU R                  R                  S;   Ga  U R                  R                  S:X  a  U R                  R                  OSnU R                  UR
                   S3-  nXR                  ;  a  U R                  R                  (       a  [        U5      R                  SSS9  [        (       a  SO[        (       a  SOS	u  pg[        R                  " [        [        U5      R!                  U5      5      [        R"                  " U6 UUR$                  S
   UR$                  S   4S9U R                  U'   U R                  U   R'                  U5        U R                  R                  (       a+  [        R(                  " U SUR
                   SU S3U5        gg[        R(                  " [        UR!                  S5      5      U5        g)z
Save video predictions as mp4 or images as jpg at specified path.

Args:
    save_path (Path): Path to save the results.
    frame (int): Frame number for video mode.
>   videoru   r      _framesTr   )z.mp4avc1).aviWMV2)r   MJPGr@   r   )filenamefourccfps	frameSize/r}   z.jpgN)r(   r&   r   r   r   r[   r'   r   save_framesr   rV   r   r   r   r   r   with_suffixVideoWriter_fourccrI   writeimwrite)r5   	save_pathr   rQ   r   frames_pathsuffixr   s           r9   r   #BasePredictor.save_predicted_images  so     << 33&*ll&7&77&B$,,""C--Y^^,<G*DDK/99((%++D4+H5:U!1T[T[@Paq-0__ i!<!<V!DE116:!xx{BHHQK8	.	* OOI&,,R0yy$${m1Y^^,<AeWDI2N %
 KKI11&9:B?r<   c                ,   U R                   n[        R                  " 5       S:X  a  XR                  ;  a  U R                  R	                  U5        [
        R                  " U[
        R                  [
        R                  -  5        [
        R                  " XR                  S   UR                  S   5        [
        R                  " X5        [
        R                  " U R                  R                  S:X  a  SOS5      S-  [        S5      :X  a  [         eg)	zDisplay an image in a window.Linuxr@   r   rc   i,  rB   qN)r(   platformsystemr+   appendr   namedWindowWINDOW_NORMALWINDOW_KEEPRATIOresizeWindowrI   imshowwaitKeyr&   r   ordr   )r5   r   rQ   s      r9   r!   BasePredictor.show  s    ??'A\\,ALL"OOAs0033G3GGHQRXXa[9

1;;dll//7:sBTISQTXU Vr<   c                Z    U R                   R                  U/ 5       H  nU" U 5        M     g)z2Run all registered callbacks for a specific event.N)r   get)r5   eventcallbacks      r9   r   BasePredictor.run_callbacks  s$    **5"5HTN 6r<   c                @    U R                   U   R                  U5        g)z-Add a callback function for a specific event.N)r   r  )r5   r  funcs      r9   add_callbackBasePredictor.add_callback  s    u$$T*r<   )r3   r   r,   r   r#   r&   r%   r    r$   r"   r(   r-   r   r*   r)   ru   r.   r0   r'   r+   )r7   zdict[str, Any] | Noner8   z dict[str, list[callable]] | None)rQ   ztorch.Tensor | list[np.ndarray]returntorch.Tensor)rQ   r  )rQ   list[np.ndarray]r  r  )NNF)ru   bool)NN)T)r   r  )
r   r   r   r   rQ   r  r   z	list[str]r  r   )r   )r   r   r   r   )r   )r   r   )r  r   )r  r   r  callable)__name__
__module____qualname____firstlineno____doc__r   r:   rS   r]   rH   rr   ry   r~   r   r   rv   r   r   r   r!   r   r  __static_attributes__rn   r<   r9   r   r   E   s    'V +/7;	'2 )'2 5	'2R2v0(O**< h- h-T]21f @D	 
+r<   r   )*r   
__future__r   r  r   r1   pathlibr   typingr   r   numpyrF   rD   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   rn   r<   r9   <module>r.     s\   B #  	    
   1 2 . 2 [ [ [ > 2 ^ ^
@+ @+r<   