
    |h{                        d dl Z d dl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mZmZ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mZ d dlmZmZmZmZ d d	l m!Z! d d
l"m#Z# e G d d             Z$ G d d      Z% G d d      Z& G d d      Z' G d d      Z( G d d      Z)dee   deeej,                  ejT                  f      fdZ+dde,de,dee,   fdZ-e%e(e'e&fZ.y)    N)	dataclass)Path)Thread)AnyListOptionalTupleUnion)Image)FORMATS_HELP_MSGIMG_FORMATSVID_FORMATS)IS_COLAB	IS_KAGGLELOGGERops)check_requirements)imreadc                   J    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   y)SourceTypesa  
    Class to represent various types of input sources for predictions.

    This class uses dataclass to define boolean flags for different types of input sources that can be used for
    making predictions with YOLO models.

    Attributes:
        stream (bool): Flag indicating if the input source is a video stream.
        screenshot (bool): Flag indicating if the input source is a screenshot.
        from_img (bool): Flag indicating if the input source is an image file.
        tensor (bool): Flag indicating if the input source is a tensor.

    Examples:
        >>> source_types = SourceTypes(stream=True, screenshot=False, from_img=False)
        >>> print(source_types.stream)
        True
        >>> print(source_types.from_img)
        False
    Fstream
screenshotfrom_imgtensorN)
__name__
__module____qualname____doc__r   bool__annotations__r   r   r        W/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/data/loaders.pyr   r      s1    ( FDJHdFDr"   r   c            	           e Zd ZdZddedededefdZdedej                  d	efd
Z
d Zd Zdeee   eej                      ee   f   fdZdefdZy)LoadStreamsa  
    Stream Loader for various types of video streams.

    Supports RTSP, RTMP, HTTP, and TCP streams. This class handles the loading and processing of multiple video
    streams simultaneously, making it suitable for real-time video analysis tasks.

    Attributes:
        sources (List[str]): The source input paths or URLs for the video streams.
        vid_stride (int): Video frame-rate stride.
        buffer (bool): Whether to buffer input streams.
        running (bool): Flag to indicate if the streaming thread is running.
        mode (str): Set to 'stream' indicating real-time capture.
        imgs (List[List[np.ndarray]]): List of image frames for each stream.
        fps (List[float]): List of FPS for each stream.
        frames (List[int]): List of total frames for each stream.
        threads (List[Thread]): List of threads for each stream.
        shape (List[Tuple[int, int, int]]): List of shapes for each stream.
        caps (List[cv2.VideoCapture]): List of cv2.VideoCapture objects for each stream.
        bs (int): Batch size for processing.
        cv2_flag (int): OpenCV flag for image reading (grayscale or RGB).

    Methods:
        update: Read stream frames in daemon thread.
        close: Close stream loader and release resources.
        __iter__: Returns an iterator object for the class.
        __next__: Returns source paths, transformed, and original images for processing.
        __len__: Return the length of the sources object.

    Examples:
        >>> stream_loader = LoadStreams("rtsp://example.com/stream1.mp4")
        >>> for sources, imgs, _ in stream_loader:
        ...     # Process the images
        ...     pass
        >>> stream_loader.close()

    Notes:
        - The class uses threading to efficiently load frames from multiple streams simultaneously.
        - It automatically handles YouTube links, converting them to the best available stream URL.
        - The class implements a buffer system to manage frame storage and retrieval.
    sources
vid_stridebufferchannelsc                 	   dt         j                  j                  _        || _        d| _        d| _        || _        |dk(  rt        j                  nt        j                  | _        t        j                  j                  |      r't        |      j!                         j#                         n|g}t%        |      }|| _        dg|z  | _        dg|z  | _        dg|z  | _        dg|z  | _        t1        |      D cg c]  }g  c}| _        t1        |      D cg c]  }g  c}| _        |D cg c]5  }t7        j8                  |      j;                  t        j<                  d      7 c}| _        tA        |      D ]:  \  }}	|dz    d| d|	 d	}
tB        jD                  jG                  |	      jH                  d
v rtK        |	      }	|	jM                         rtO        |	      n|	}	|	dk(  rtP        stR        rtU        d      t        jV                  |	      | j.                  |<   | j.                  |   jY                         st[        |
 d|	       t]        | j.                  |   j_                  t        j`                              }t]        | j.                  |   j_                  t        jb                              }| j.                  |   j_                  t        jd                        }tg        t]        | j.                  |   j_                  t        jh                              d      xs tk        d      | j*                  |<   tg        tm        jn                  |      r|nddz  d      xs d| j(                  |<   | j.                  |   jq                         \  }}| j                  t        j                  k(  r't        jr                  |t        jt                        d   n|}|r|t[        |
 d|	       | j2                  |   jw                  |       |j4                  | j4                  |<   ty        | jz                  || j.                  |   |	gd      | j,                  |<   t}        j~                  |
 d| j*                  |    d| d| d| j(                  |   dd
       | j,                  |   j                          = t}        j~                  d       yc c}w c c}w c c}w )a  
        Initialize stream loader for multiple video sources, supporting various stream types.

        Args:
            sources (str): Path to streams file or single stream URL.
            vid_stride (int): Video frame-rate stride.
            buffer (bool): Whether to buffer input streams.
            channels (int): Number of image channels (1 for grayscale, 3 for RGB).
        Tr      r   N_/: z... >   youtu.beyoutube.comwww.youtube.comzm'source=0' webcam not supported in Colab and Kaggle notebooks. Try running 'source=0' in a local environment.zFailed to open infd      .NzFailed to read images from )targetargsdaemonu   Success ✅ (z frames of shape xz at z.2fz FPS) )Atorchbackendscudnn	benchmarkr(   runningmoder'   cv2IMREAD_GRAYSCALEIMREAD_COLORcv2_flagospathisfiler   	read_textrsplitlenbsfpsframesthreadscapsrangeimgsshaper   	clean_strreplacesepr&   	enumerateurllibparseurlparsehostnameget_best_youtube_url	isnumericevalr   r   NotImplementedErrorVideoCaptureisOpenedConnectionErrorintgetCAP_PROP_FRAME_WIDTHCAP_PROP_FRAME_HEIGHTCAP_PROP_FPSmaxCAP_PROP_FRAME_COUNTfloatmathisfinitereadcvtColorCOLOR_BGR2GRAYappendr   updater   infostart)selfr&   r'   r(   r)   nr,   r9   isstwhrL   successims                   r#   __init__zLoadStreams.__init__^   s    *.&	$08A,,3CSCS8:w8O$w-))+224V]U^L37cAgvzFQJ	!&q*AR*	"'(+Qb+
GNO!a(00=Og& 	$DAqE7!A3b4(B||$$Q'004bb(+;;=QaAAv8y)E  ++A.DIIaL99Q<((*%OA3&?@@DIIaL$$S%=%=>?ADIIaL$$S%>%>?@A))A,""3#3#34C TYYq\%5%5c6N6N%O!PRST X]YDKKN dmmC&8sa3FJPbDHHQK))A,++-KGRDHMMUXUiUiDib#"4"45i@oqBbj%,Gs&KLLIIaL#HHDJJqM$DKK499Q<QR?S]abDLLOKK2$mDKKN+;;LQCqQRPSSWX\X`X`abXcdgWhhmnoLLO!!#?	$@ 	BG ++Os   :	S	S-:Sru   capr   c                 Z   d| j                   |   }}| j                  r|j                         r{||dz
  k  rqt        | j                  |         dk  r|dz  }|j                          || j                  z  dk(  r|j                         \  }}| j                  t        j                  k(  r't        j                  |t        j                        d   n|}|sXt        j                  | j                  |   t        j                         }t#        j$                  d       |j'                  |       | j(                  r| j                  |   j+                  |       n&|g| j                  |<   nt-        j.                  d       | j                  r|j                         r||dz
  k  rnyyyyyy)	z<Read stream frames in daemon thread and update image buffer.r   r+   r4   r5   dtypezBVideo stream unresponsive, please check your IP camera connection.g{Gz?N)rM   r?   r`   rJ   rQ   grabr'   retrieverD   rA   rB   rm   rn   npzerosrR   uint8r   warningopenr(   ro   timesleep)rs   ru   r}   r   rt   frz   r{   s           r#   rp   zLoadStreams.update   s=   $++a.1lls||~!q1u+499Q< 2%Q
t&!+"%,,.KGRKO==\_\p\pKpR););<YGvx  #XXdjjm288D'kl({{		!++B/(*t		!

4 % lls||~!q1u+~l+~lr"   c                    d| _         | j                  D ]%  }|j                         s|j                  d       ' | j                  D ]  }	 |j                           y# t        $ r"}t        j                  d|        Y d}~=d}~ww xY w)zKTerminate stream loader, stop threads, and release video capture resources.F   )timeoutz'Could not release VideoCapture object: N)	r?   rN   is_alivejoinrO   release	Exceptionr   r   )rs   threadr}   es       r#   closezLoadStreams.close   s    ll 	'F A&	' 99 	NCN	N  N!HLMMNs   A	B
(BB
c                     d| _         | S )zAIterate through YOLO image feed and re-open unresponsive streams.countrs   s    r#   __iter__zLoadStreams.__iter__   s    
r"   returnc                    | xj                   dz  c_         g }t        | j                        D ]#  \  }}|s| j                  |   j	                         s| j                          t        t        j                  dt        | j                        z         | j                  |   }|st        j                  d|        |s| j                  r!|j                  |j                  d             |j                  |r|j                  d      n1t!        j"                  | j$                  |   t         j&                               |j)                          & | j*                  |dg| j,                  z  fS )zKReturn the next batch of frames from multiple video streams for processing.r+   zWaiting for stream r   r   r   r:   )r   rV   rQ   rN   r   r   StopIterationr   r   minrL   r   r   r(   ro   popr   r   rR   r   clearr&   rK   )rs   imagesru   r9   s       r#   __next__zLoadStreams.__next__   s   

a
dii( 	DAq||A//1JJL''

1s488},-IIaLNN%8#<=  {{aeeAh' 1aeeBi"((4::a=PRPXPX2YZ	%	( ||VbTDGG^33r"   c                     | j                   S )z=Return the number of video streams in the LoadStreams object.rK   r   s    r#   __len__zLoadStreams.__len__       wwr"   N)zfile.streamsr+   F   )r   r   r   r   strrb   r   r|   rA   r_   rp   r   r   r	   r   r   ndarrayr   r   r!   r"   r#   r%   r%   4   s    'R; ;# ;SW ;kn ;z! !#"2"2 !C !.
N
4%S	4

+;T#Y FG 46 r"   r%   c                   f    e Zd ZdZd	dedefdZd Zdee	e   e	e
j                     e	e   f   fdZy)
LoadScreenshotsac  
    Ultralytics screenshot dataloader for capturing and processing screen images.

    This class manages the loading of screenshot images for processing with YOLO. It is suitable for use with
    `yolo predict source=screen`.

    Attributes:
        source (str): The source input indicating which screen to capture.
        screen (int): The screen number to capture.
        left (int): The left coordinate for screen capture area.
        top (int): The top coordinate for screen capture area.
        width (int): The width of the screen capture area.
        height (int): The height of the screen capture area.
        mode (str): Set to 'stream' indicating real-time capture.
        frame (int): Counter for captured frames.
        sct (mss.mss): Screen capture object from `mss` library.
        bs (int): Batch size, set to 1.
        fps (int): Frames per second, set to 30.
        monitor (Dict[str, int]): Monitor configuration details.
        cv2_flag (int): OpenCV flag for image reading (grayscale or RGB).

    Methods:
        __iter__: Returns an iterator object.
        __next__: Captures the next screenshot and returns it.

    Examples:
        >>> loader = LoadScreenshots("0 100 100 640 480")  # screen 0, top-left (100,100), 640x480
        >>> for source, im, im0s, vid_cap, s in loader:
        ...     print(f"Captured frame: {im.shape}")
    sourcer)   c                    t        d       ddl}|j                         ^}}d\  | _        }}}}t	        |      dk(  rt        |d         | _        n?t	        |      dk(  rd |D        \  }}}}n"t	        |      dk(  rd	 |D        \  | _        }}}}d
| _        d| _        |j                         | _        d| _	        d| _
        |dk(  rt        j                  nt        j                  | _        | j                  j                  | j                     }	||	d   n|	d   |z   | _        ||	d   n|	d   |z   | _        |xs |	d   | _        |xs |	d   | _        | j"                  | j                   | j$                  | j&                  d| _        y)a#  
        Initialize screenshot capture with specified screen and region parameters.

        Args:
            source (str): Screen capture source string in format "screen_num left top width height".
            channels (int): Number of image channels (1 for grayscale, 3 for RGB).
        mssr   N)r   NNNNr+      c              3   2   K   | ]  }t        |        y wNrb   .0r9   s     r#   	<genexpr>z+LoadScreenshots.__init__.<locals>.<genexpr>  s     '?1A'?   r   c              3   2   K   | ]  }t        |        y wr   r   r   s     r#   r   z+LoadScreenshots.__init__.<locals>.<genexpr>  s     4LSV4Lr   r   r4   topleftwidthheight)r   r   r   r   )r   r   splitscreenrJ   rb   r@   framesctrK   rL   rA   rB   rC   rD   monitorsr   r   r   r   monitor)
rs   r   r)   r   paramsr   r   r   r   r   s
             r#   r|   zLoadScreenshots.__init__  sd    	5! ,,.0I-T3vv;!fQi.DK[A'?'?$D#uf[A4LV4L1DKsE6	
77908A,,3CSCS ((##DKK0%([75>wu~7K'+|GFO'&/D:P	.gg.
1 1 $		$((TZZ[_[f[fgr"   c                     | S )zSYield the next screenshot image from the specified screen or region for processing.r!   r   s    r#   r   zLoadScreenshots.__iter__%  s    r"   r   c                    t        j                  | j                  j                  | j                              ddddddf   }| j
                  t        j                  k(  r't        j                  |t        j                        d   n|}d| j                   d| j                   d| j                   d| j                   d| j                   d}| xj                  dz  c_        t!        | j                        g|g|gfS )	zNCapture and return the next screenshot as a numpy array using the mss library.Nr   r5   zscreen z	 (LTWH): ,r.   r+   )r   asarrayr   r   r   rD   rA   rB   rm   rn   r   r   r   r   r   r   r   )rs   im0rv   s      r#   r   zLoadScreenshots.__next__)  s    jjt||45aBQBh?BF--SVSgSgBgcll3 2 23I>mpdkk])DII;az4::,aPTP[P[}\^_

a
DKK !C51#--r"   Nr   )r   r   r   r   r   rb   r|   r   r	   r   r   r   r   r!   r"   r#   r   r      sL    > hs  hc  hD.%S	4

+;T#Y FG .r"   r   c            	           e Zd ZdZddeeeef   dededefdZ	d Z
deee   eej                     ee   f   fd	Zdefd
ZdefdZy)LoadImagesAndVideosa"  
    A class for loading and processing images and videos for YOLO object detection.

    This class manages the loading and pre-processing of image and video data from various sources, including
    single image files, video files, and lists of image and video paths.

    Attributes:
        files (List[str]): List of image and video file paths.
        nf (int): Total number of files (images and videos).
        video_flag (List[bool]): Flags indicating whether a file is a video (True) or an image (False).
        mode (str): Current mode, 'image' or 'video'.
        vid_stride (int): Stride for video frame-rate.
        bs (int): Batch size.
        cap (cv2.VideoCapture): Video capture object for OpenCV.
        frame (int): Frame counter for video.
        frames (int): Total number of frames in the video.
        count (int): Counter for iteration, initialized at 0 during __iter__().
        ni (int): Number of images.
        cv2_flag (int): OpenCV flag for image reading (grayscale or RGB).

    Methods:
        __init__: Initialize the LoadImagesAndVideos object.
        __iter__: Returns an iterator object for VideoStream or ImageFolder.
        __next__: Returns the next batch of images or video frames along with their paths and metadata.
        _new_video: Creates a new video capture object for the given path.
        __len__: Returns the number of batches in the object.

    Examples:
        >>> loader = LoadImagesAndVideos("path/to/data", batch=32, vid_stride=1)
        >>> for paths, imgs, info in loader:
        ...     # Process batch of images or video frames
        ...     pass

    Notes:
        - Supports various image formats including HEIC.
        - Handles both local files and directories.
        - Can read from a text file containing paths to images and videos.
    rF   batchr'   r)   c                    d}t        |t              rTt        |      j                  dk(  r<t        |      j                  }t        |      j                         j                         }g }t        |t        t        f      rt        |      n|gD ]C  }t        t        |      j                               }d|v r0|j                  t        t        j                  |d                   Zt        j                  j                  |      rL|j                  t        t        j                  t        j                  j!                  |d                         t        j                  j#                  |      r|j%                  |       |r@||z  j'                         r-|j%                  t        ||z  j                                      8t)        | d       g g }
}	|D ]X  }|j+                  d      d	   j-                         }|t.        v r|	j%                  |       ?|t0        v sH|
j%                  |       Z t3        |	      t3        |
      }}|	|
z   | _        ||z   | _        || _        d
g|z  dg|z  z   | _        |dk(  rdnd| _        || _        || _         |dk(  rtB        jD                  ntB        jF                  | _$        tK        |
      r| jM                  |
d          nd| _'        | j6                  dk(  rt)        d dtP               y)a  
        Initialize dataloader for images and videos, supporting various input formats.

        Args:
            path (str | Path | List): Path to images/videos, directory, or list of paths.
            batch (int): Batch size for processing.
            vid_stride (int): Video frame-rate stride.
            channels (int): Number of image channels (1 for grayscale, 3 for RGB).
        Nz.txt*T)	recursivez*.*z does not exist.r   Fr   videoimager+   zNo images or videos found in z. ))
isinstancer   r   suffixparentrH   
splitlineslisttuplesortedabsoluteextendglobrE   rF   isdirr   rG   ro   is_fileFileNotFoundError
rpartitionlowerr   r   rJ   filesnfni
video_flagr@   r'   rK   rA   rB   rC   rD   any
_new_videor}   r   )rs   rF   r   r'   r)   r   r   par   videosr   r   r   nvs                  r#   r|   zLoadImagesAndVideos.__init__[  sn    dC T$Z%6%6&%@$Z&&F:'')446D!+D4-!@tf 	?ADG$$&'AaxVDIIa4$@ABq!VDIIbggll1e.D$EFG"QVaZ002S&1*!6!6!89:'1#_(=>>	? R 	!A\\#&r*002F$a ;&a 	! Vc&kBf_
r' 'B,$"4!QwGG	$08A,,3CSCSv;OOF1I&DH77a<#&CA3bIYHZ$[\\ r"   c                     d| _         | S )zOIterate through image/video files, yielding source paths, images, and metadata.r   r   r   s    r#   r   zLoadImagesAndVideos.__iter__      
r"   r   c                 B   g g g }}}t        |      | j                  k  r| j                  | j                  k\  r|r|||fS t        | j
                  | j                     }| j                  | j                     r.d| _        | j                  r| j                  j                         s| j                  |       d}t        | j                        D ]   }| j                  j                         }|r  n |r4| j                  j                         \  }}| j                  t         j"                  k(  r't!        j$                  |t         j&                        d   n|}|r| xj(                  dz  c_        |j+                  |       |j+                  |       |j+                  d| j                  dz    d| j                   d| j(                   d| j,                   d| d	       | j(                  | j,                  k(  r| xj                  dz  c_        | j                  j/                          n| xj                  dz  c_        | j                  r| j                  j/                          | j                  | j                  k  ry| j                  | j
                  | j                            nOd
| _        |j1                  d      d   j3                         dk(  rnt5        d       ddlm}  |        t;        j<                  |      5 }	t!        j$                  t?        j@                  |	      t         jB                        }ddd       ntE        || j                        }tG        jH                  d|        nT|j+                  |       |j+                  |       |j+                  d| j                  dz    d| j                   d| d	       | xj                  dz  c_        | j                  | jJ                  k\  rnt        |      | j                  k  r|||fS # 1 sw Y   xY w)zNReturn the next batch of images or video frames with their paths and metadata.r   Fr5   r+   zvideo r-   z (frame z) r.   r   r   r   heiczpillow-heifr   )register_heif_openerN)flagszImage Read Error zimage  )&rJ   rK   r   r   r   r   r   r@   r}   r`   r   rP   r'   r   r   rD   rA   rB   rm   rn   r   ro   rM   r   r   r   r   pillow_heifr   r   r   r   r   COLOR_RGB2BGRr   r   r   r   )
rs   pathsrQ   rq   rF   rz   r,   r   r   imgs
             r#   r   zLoadImagesAndVideos.__next__  s=   BTt$i$''!zzTWW$ $,,''::djj)Dtzz*#	xxtxx'8'8':OOD)t/ A"hhmmoG"
 #'88#4#4#6LGS  ==C,@,@@ S#*<*<=iH  
 

a
T*C(fTZZ!^,<AdggYhtzzlZ[\`\g\g[hhjkojppr$st::4 JJ!OJ HH,,. JJ!OJxx((*zzDGG+

4::(>? $	??3'+113v=&}5@(*D) OS!ll2::c?C<M<MNO O !T]];C;NN%6tf#=>LL&KK$KK&a(8$''!D6 LM

a
::( $i$''!B dD  O Os    8PPc                    d| _         t        j                  |      | _        t	        | j                  j                  t        j                              | _        | j                  j                         st        d|       t	        | j                  j                  t        j                        | j                  z        | _        y)z]Create a new video capture object for the given path and initialize video-related attributes.r   zFailed to open video N)r   rA   r_   r}   rb   rc   rf   rL   r`   r   rh   r'   rM   )rs   rF   s     r#   r   zLoadImagesAndVideos._new_video  s    
##D)txx||C$4$456xx  "#&;D6$BCC$((,,s'?'?@4??RSr"   c                 Z    t        j                  | j                  | j                  z        S )z>Return the number of files (images and videos) in the dataset.)rj   ceilr   rK   r   s    r#   r   zLoadImagesAndVideos.__len__  s    yy477*++r"   N)r+   r+   r   )r   r   r   r   r
   r   r   r   rb   r|   r   r	   r   r   r   r   r   r!   r"   r#   r   r   3  s    %N3]U3d?3 3]C 3]QT 3]dg 3]j
D!%S	4

+;T#Y FG D!LTs T, ,r"   r   c                      e Zd ZdZddeej
                  ej                  ef   de	fdZ
eddeej
                  ej                  f   dedej                  fd       Zde	fd	Zdeee   eej                     ee   f   fd
Zd Zy)LoadPilAndNumpya3  
    Load images from PIL and Numpy arrays for batch processing.

    This class manages loading and pre-processing of image data from both PIL and Numpy formats. It performs basic
    validation and format conversion to ensure that the images are in the required format for downstream processing.

    Attributes:
        paths (List[str]): List of image paths or autogenerated filenames.
        im0 (List[np.ndarray]): List of images stored as Numpy arrays.
        mode (str): Type of data being processed, set to 'image'.
        bs (int): Batch size, equivalent to the length of `im0`.

    Methods:
        _single_check: Validate and format a single image to a Numpy array.

    Examples:
        >>> from PIL import Image
        >>> import numpy as np
        >>> pil_img = Image.new("RGB", (100, 100))
        >>> np_img = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
        >>> loader = LoadPilAndNumpy([pil_img, np_img])
        >>> paths, images, _ = next(iter(loader))
        >>> print(f"Loaded {len(images)} images")
        Loaded 2 images
    r   r)   c           	      J   t        |t              s|g}t        |      D cg c]  \  }}t        |dd      xs d| d c}}| _        |dk(  rdnd}|D cg c]  }| j                  ||       c}| _        d| _        t        | j                        | _	        yc c}}w c c}w )	a;  
        Initialize a loader for PIL and Numpy images, converting inputs to a standardized format.

        Args:
            im0 (PIL.Image.Image | np.ndarray | List): Single image or list of images in PIL or numpy format.
            channels (int): Number of image channels (1 for grayscale, 3 for RGB).
        filenamer:   r   .jpgr+   LRGBN)
r   r   rV   getattrr   _single_checkr   r@   rJ   rK   )rs   r   r)   ru   r{   pil_flags         r#   r|   zLoadPilAndNumpy.__init__  s     #t$%CR[\_R`aBgb*b1DuQCt_Da
"a-3U?BCD&&r84C	dhh-	 bCs   BB r{   flagr   c                 z   t        | t        j                  t        j                  f      sJ dt	        |               t        | t        j                        rOt        j
                  | j                  |            } |dk(  r| d   n	| ddddf   } t        j                  |       } | S | j                  dk(  r| d   } | S )zVValidate and format an image to numpy array, ensuring RGB order and contiguous memory.z,Expected PIL/np.ndarray image type, but got r   r5   .Nr      )	r   r   r   r   typer   convertascontiguousarrayndim)r{   r  s     r#   r  zLoadPilAndNumpy._single_check  s     "u{{BJJ78s<himnpiqhr:ss8b%++&BJJt,-B"&#+I2c4R4i=B%%b)B 	 WW\IB	r"   c                 ,    t        | j                        S )zSReturn the length of the 'im0' attribute, representing the number of loaded images.)rJ   r   r   s    r#   r   zLoadPilAndNumpy.__len__"  s    488}r"   c                     | j                   dk(  rt        | xj                   dz  c_         | j                  | j                  dg| j                  z  fS )zDReturn the next batch of images, paths, and metadata for processing.r+   r:   r   r   r   r   rK   r   s    r#   r   zLoadPilAndNumpy.__next__&  ?    ::?

a
zz488bTDGG^33r"   c                     d| _         | S )zZIterate through PIL/numpy images, yielding paths, raw images, and metadata for processing.r   r   r   s    r#   r   zLoadPilAndNumpy.__iter__-  r   r"   Nr   )r   )r   r   r   r   r
   r   r   r   r   rb   r|   staticmethodr   r  r   r	   r   r   r!   r"   r#   r   r     s    4 E%++rzz4"?@  C  " 
%RZZ 78 
 
PRPZPZ 
 
 4%S	4

+;T#Y FG 4r"   r   c                       e Zd ZdZdej
                  ddfdZeddej
                  dedej
                  fd       Z	d	 Z
deee   ej
                  ee   f   fd
ZdefdZy)
LoadTensoraf  
    A class for loading and processing tensor data for object detection tasks.

    This class handles the loading and pre-processing of image data from PyTorch tensors, preparing them for
    further processing in object detection pipelines.

    Attributes:
        im0 (torch.Tensor): The input tensor containing the image(s) with shape (B, C, H, W).
        bs (int): Batch size, inferred from the shape of `im0`.
        mode (str): Current processing mode, set to 'image'.
        paths (List[str]): List of image paths or auto-generated filenames.

    Methods:
        _single_check: Validates and formats an input tensor.

    Examples:
        >>> import torch
        >>> tensor = torch.rand(1, 3, 640, 640)
        >>> loader = LoadTensor(tensor)
        >>> paths, images, info = next(iter(loader))
        >>> print(f"Processed {len(images)} images")
    r   r   Nc                     | j                  |      | _        | j                  j                  d   | _        d| _        t        |      D cg c]  \  }}t        |dd| d       c}}| _        yc c}}w )z
        Initialize LoadTensor object for processing torch.Tensor image data.

        Args:
            im0 (torch.Tensor): Input tensor with shape (B, C, H, W).
        r   r   r   r   N)r  r   rR   rK   r@   rV   r   r   )rs   r   ru   r{   s       r#   r|   zLoadTensor.__init__K  s`     %%c*((..#	LUVYN[51bgb*aSo>[
[s   
A/r{   stridec                 L   d| dt        | j                         d}t        | j                        dk7  rIt        | j                        dk7  rt        |      t	        j
                  |       | j                  d      } | j                  d   |z  s| j                  d   |z  rt        |      | j                         dt        j                  | j                        j                  z   kD  r:t	        j
                  d	| j                          d
       | j                         dz  } | S )zTValidate and format a single image tensor, ensuring correct shape and normalization.zRtorch.Tensor inputs should be BCHW i.e. shape(1, 3, 640, 640) divisible by stride z. Input shapez is incompatible.r   r   r   r  g      ?zBtorch.Tensor inputs should be normalized 0.0-1.0 but max value is z. Dividing input by 255.g     o@)r   rR   rJ   
ValueErrorr   r   	unsqueezerg   r;   finfor   epsri   )r{   r  rv   s      r#   r  zLoadTensor._single_checkW  s    ##)(-bhh7HHY[ 	
 rxx=A288}! m#NN1aB88A;288A;#7Q-668cEKK15555NNTUWU[U[U]T^^vw e#B	r"   c                     d| _         | S )zAYield an iterator object for iterating through tensor image data.r   r   r   s    r#   r   zLoadTensor.__iter__m  r   r"   c                     | j                   dk(  rt        | xj                   dz  c_         | j                  | j                  dg| j                  z  fS )zBYield the next batch of tensor images and metadata for processing.r+   r:   r  r   s    r#   r   zLoadTensor.__next__r  r  r"   c                     | j                   S )z*Return the batch size of the tensor input.r   r   s    r#   r   zLoadTensor.__len__y  r   r"   )    )r   r   r   r   r;   Tensorr|   r  rb   r  r   r	   r   r   r   r   r!   r"   r#   r  r  3  s    .
\ELL 
\T 
\ %,,  U\\  *
4%S	5<<c BC 4 r"   r  r   r   c                    g }| D ]  }t        |t        t        f      r^|j                  t	        j
                  t        |      j                  d      rt        j                  j                  |      n|             wt        |t        j                  t        j                  f      r|j                  |       t        dt        |      j                   d       |S )z]Merge a list of sources into a list of numpy arrays or PIL images for Ultralytics prediction.httpztype z is not a supported Ultralytics prediction source type. 
See https://docs.ultralytics.com/modes/predict for supported source types.)r   r   r   ro   r   r   
startswithrW   requesturlopenr   r   	TypeErrorr  r   )r   r   r{   s      r#   autocast_listr$  ~  s    E 	b3+&LL#b'BTBTU[B\FNN$:$:2$>bdefU[["**56LLR))* +] ^ 	 Lr"   urlmethodc                 2   |dk(  rt        d       ddlm}  ||       j                  j	                  dd      }t        |d d	      }|D ]8  }|j                  st        |j                  d
d       dk\  s,|j                  c S  y
|dk(  r:t        d       dd
l	}|j                  |       j                  d      j                  S |dk(  rt        d       dd
l}|j                  ddi      5 }|j                  | d      }d
d
d
       t        j!                  dg             D ]d  }	|	j!                  d      xs ddk\  xs |	j!                  d      xs ddk\  }
|
s8|	d   dk7  sA|	d   dk(  sJ|	d   dk(  sS|	j!                  d      c S  y
y
# 1 sw Y   xY w)a  
    Retrieve the URL of the best quality MP4 video stream from a given YouTube video.

    Args:
        url (str): The URL of the YouTube video.
        method (str): The method to use for extracting video info. Options are "pytube", "pafy", and "yt-dlp".

    Returns:
        (str | None): The URL of the best quality MP4 video stream, or None if no suitable stream is found.

    Examples:
        >>> url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
        >>> best_url = get_best_youtube_url(url)
        >>> print(best_url)
        https://rr4---sn-q4flrnek.googlevideo.com/videoplayback?expire=...

    Notes:
        - Requires additional libraries based on the chosen method: pytubefix, pafy, or yt-dlp.
        - The function prioritizes streams with at least 1080p resolution when available.
        - For the "yt-dlp" method, it looks for formats with video codec, no audio, and *.mp4 extension.
    pytubezpytubefix>=6.5.2r   )YouTubemp4T)file_extension
only_videoc                     | j                   S r   )
resolution)rv   s    r#   <lambda>z&get_best_youtube_url.<locals>.<lambda>  s
     r"   )keyreverseNr   i8  pafy)r2  zyoutube_dl==2020.12.2)preftypezyt-dlpquietF)downloadformatsr   i  r   vcodecnoneacodecextr%  )r   	pytubefixr)  streamsfilterr   r.  rb   r%  r2  newgetbestvideoyt_dlp	YoutubeDLextract_inforeversedrc   )r%  r&  r)  r<  r   r2  r@  ydl	info_dictr   	good_sizes              r#   r[   r[     s   , -.%#,&&--Ut-T&<dK 	"F  S):):3B)?%@D%Hzz!	" 
6	<=xx}))5)9===	8	8$wo. 	>#((u(=I	>)--	267 	$Aw,15W!%%/:NQSW9WIQx[F2q{f7LQRSXQY]bQbuuU|#		$ 
	> 	>s   -FF)r(  )/r   rj   rE   r   rW   dataclassesr   pathlibr   	threadingr   typingr   r   r   r	   r
   rA   numpyr   r;   PILr   ultralytics.data.utilsr   r   r   ultralytics.utilsr   r   r   r   ultralytics.utils.checksr   ultralytics.utils.patchesr   r   r%   r   r   r   r  r   r$  r   r[   LOADERSr!   r"   r#   <module>rR     s      	   !   4 4 
    M M > > 7 ,   6l l^M. M.`s, s,lG GTH HV$s) U5;;

3J-K(L "1$c 1$3 1$hsm 1$j )<o
Nr"   