
    ,}h?                        U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZ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mZmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlZd dlmZ ddl m!Z!m"Z" dZ#dDde$d	ee$ejJ                  f   d
e&ddfdZ'dEdee$ejJ                  f   d
e&de$fdZ(dee$ejJ                  f   de$dede)fdZ*dFdee$ejJ                  f   dee$   de)fdZ+dGde$de&de$fdZ,de$dee$   fdZ-	 	 	 dHde$dee$ejJ                  f   d	eee$ejJ                  f      dee$   de&ddfdZ.dIdee$ejJ                  f   de)dee$   fdZ/dIdee$ejJ                  f   de$de)dee$   fdZ0	 	 dJde$dee$ejJ                  f   d	eee$ejJ                  f      dee$   fdZ1dee$ejJ                  f   d ee$ejJ                  f   d!ee$   ddfd"Z2ejf                  ejh                  d#Z5ee$e&f   e6d$<   dee$ejJ                  f   d ee$ejJ                  f   d!ee$   ddfd%Z7e2e7d&Z8ee$eee$ejJ                  f   ee$ejJ                  f   ee$   gdf   f   e6d'<   e jr                  ejr                  ejr                  d(Z:ee$ed)ef   f   e6d*<   d+d+d,d-Z;ee$eee$   ee$   f   f   e6d.<   d/ee$ejJ                  f   dee$ee$   ee$   f   fd0Z<	 	 dKdee$ejJ                  f   d eee$ejJ                  f      d1e)dejJ                  fd2Z=	 	 dKdee$ejJ                  f   d eee$ejJ                  f      d1e)dee$ejJ                  f   fd3Z>	 	 	 	 dLde$d4ee$ejJ                  f   d5eee$ejJ                  f      d	eee$ejJ                  f      dee$   d1e)ddfd6Z?d7ede$fd8Z@ ed9e$eA      ZB	 	 	 dMd:eBd;ee$   d<eeeB      d=ee$   deBf
d>ZCdNd?ee$ejJ                  f   d@e&dej                  fdAZEdBej                  dej                  fdCZGy)O    N)
AnyCallableDictIOIterableListOptionalTupleTypeVarUnion)urlparse)tqdm   )#_download_file_from_remote_location_is_remote_location_availablezpytorch/visionurlfilename
chunk_sizereturnc                    t         j                  j                  t         j                  j                  | dt        i            5 }t        |d      5 }t        |j                        5 }|j                  |      x}r?|j                  |       |j                  t        |             |j                  |      x}r?d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)N
User-Agentheaderswb)total)urllibrequesturlopenRequest
USER_AGENTopenr   lengthreadwriteupdatelen)r   r   r   responsefhpbarchunks          Y/var/www/html/test/engine/venv/lib/python3.12/site-packages/torchvision/datasets/utils.py_urlretriever,      s    			 6 6s\S]D^ 6 _	` (dl(D! 	(RHOO)D 	(#==44%4CJ' $==44%4	( 	(( (	( 	( 	( 	(( (s=   C-C!)AC<C!C-CC!!C*	&C--C6fpathc                 n   t         j                  dk\  rt        j                  d      }nt        j                         }t	        | d      5 }|j                  |      x}r%|j                  |       |j                  |      x}r%d d d        |j                         S # 1 sw Y   |j                         S xY w)N)   	   F)usedforsecurityrb)sysversion_infohashlibmd5r!   r#   r%   	hexdigest)r-   r   r6   fr*   s        r+   calculate_md5r9   #   s     6!kk%0kkm	eT	 avvj))e)JJu vvj))e) ==? ==?s   8BB4r6   kwargsc                      |t        | fi |k(  S N)r9   )r-   r6   r:   s      r+   	check_md5r=   1   s    -0000    c                 `    t         j                  j                  |       sy|yt        | |      S )NFT)ospathisfiler=   )r-   r6   s     r+   check_integrityrC   5   s*    77>>% 
{UC  r>   max_hopsc           	      v   | }dt         d}t        |dz         D ]}  }t        j                  j	                  t        j                  j                  | |            5 }|j                  | k(  s|j                  | cd d d        c S |j                  } d d d         t        d| d| d|  d      # 1 sw Y   xY w)	NHEAD)Methodr      r   zRequest to z
 exceeded z( redirects. The last redirect points to .)r    ranger   r   r   r   r   RecursionError)r   rD   initial_urlr   _r'   s         r+   _get_redirect_urlrN   =   s    Kz:G8a<  	
^^##FNN$:$:3$:$PQ 	U]||s"hll&:	 	 ,,C		 		
 +j
:bcfbgghi
 	
	 	s   B/B//B8	c                     t        |       }t        j                  d|j                        y t        j                  d|j                        }|y |j                  d      S )Nz(drive|docs)[.]google[.]comz/file/d/(?P<id>[^/]*)id)r   rematchnetlocrA   group)r   partsrR   s      r+   _get_google_drive_file_idrV   M   sP    SME	xx.=EHH-uzz:E};;tr>   rootmax_redirect_hopsc                    t         j                  j                  |      }|st         j                  j                  |       }t        j                  t         j                  j                  ||            }t        j                  |d       t        ||      rt        d|z          yt               rt        ||        nIt        | |      } t        |       }|t        ||||      S 	 t        d| z   dz   |z          t        | |       t        ||      st)        d      y# t        j                   j"                  t$        f$ rG}| dd d	k(  r3| j'                  d
d      } t        d| z   dz   |z          t        | |       n|Y d}~}d}~ww xY w)a  Download a file from a url and place it in root.

    Args:
        url (str): URL to download file from
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the basename of the URL
        md5 (str, optional): MD5 checksum of the download. If None, do not check
        max_redirect_hops (int, optional): Maximum number of redirect hops allowed
    Texist_okz$Using downloaded and verified file: N)rD   zDownloading  to    httpszhttps:zhttp:z;Failed download. Trying https -> http instead. Downloading File not found or corrupted.)r@   rA   
expanduserbasenamefspathjoinmakedirsrC   printr   r   rN   rV   download_file_from_google_driver,   r   errorURLErrorOSErrorreplaceRuntimeError)r   rW   r   r6   rX   r-   file_ides           r+   download_urlrn   Z   sj     77d#D77##C(IIbggll423EKKt$ uc"4u<=$&+E37  .?@ ,C027D(CPP		.3&/%78e$ 5#&9:: ' %%w/ 	2Aw'!kk(G4SVYY\bbejjkS%( )		s   & D #F=FFprefixc           	      z   t         j                  j                  |       } t        j                  |       D cg c]B  }t         j                  j	                  t         j                  j                  | |            sA|D }}|du r-|D cg c]"  }t         j                  j                  | |      $ }}|S c c}w c c}w )a  List all directories at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the directories found
    T)r@   rA   r`   listdirisdirrc   )rW   ro   pdirectoriesds        r+   list_dirrv      s     77d#D jj.W"''--TST@U2V1WKW~6ABrww||D!,BB XBs   AB3:B3'B8suffixc           	         t         j                  j                  |       } t        j                  |       D cg c]T  }t         j                  j	                  t         j                  j                  | |            sA|j                  |      sS|V }}|du r-|D cg c]"  }t         j                  j                  | |      $ }}|S c c}w c c}w )a  List all files ending with a suffix at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').
            It uses the Python "str.endswith" method and is passed directly
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the files found
    T)r@   rA   r`   rq   rB   rc   endswith)rW   rw   ro   rs   filesru   s         r+   
list_filesr{      s     77d#D

4(i1BGGNN277<<a;P,QVWV`V`agVhQiEi~0561dA&66L j6s   AC:CC'C
rl   c                    	 ddl }t        j                  j                  |      }|s| }t        j                  t        j                  j                  ||            }t        j                  |d       t        ||      rt        d|rdnd d	|        y|j                  | |d
t               t        ||      st        d      y# t        $ r t        d      w xY w)af  Download a Google Drive file from  and place it in root.

    Args:
        file_id (str): id of file to be downloaded
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the id of the file.
        md5 (str, optional): MD5 checksum of the download. If None, do not check
    r   Nz`To download files from GDrive, 'gdown' is required. You can install it with 'pip install gdown'.TrZ   zUsing downloaded zand verified  zfile: F)rP   outputquiet
user_agentr_   )gdownModuleNotFoundErrorrk   r@   rA   r`   rb   rc   rd   rC   re   downloadr    )rl   rW   r   r6   r   r-   s         r+   rf   rf      s    
 77d#DIIbggll423EKKt$uc"!S/b!AwOP	NNge5ZNP5#&9:: '%  
n
 	

s   C C	from_pathto_pathcompressionc                     t        j                  | |rd|dd   nd      5 }|j                  |       d d d        y # 1 sw Y   y xY w)Nzr:rH   r)tarfiler!   
extractall)r   r   r   tars       r+   _extract_tarr      sH     
i;2k!"o%6!7C	P  TWw     s	   <A).bz2.xz_ZIP_COMPRESSION_MAPc                     t        j                  | d|r	t        |   nt         j                        5 }|j	                  |       d d d        y # 1 sw Y   y xY w)Nr   )r   )zipfileZipFiler   
ZIP_STOREDr   )r   r   r   zips       r+   _extract_zipr      sL     
3$8$EZaZlZl
  	w     s   AA).tarz.zip_ARCHIVE_EXTRACTORS)r   .gzr   ._COMPRESSED_FILE_OPENERS)r   r   )r   r   )z.tbzz.tbz2z.tgz_FILE_TYPE_ALIASESfilec                    t        j                  |       j                  }|st        d|  d      |d   }|t        v r|gt        |   S |t
        v r||dfS |t        v r(t        |      dkD  r|d   }|t
        v r||z   ||fS |d|fS t        t        t              t        t
              z  t        t              z        }t        d| d| d	      )
a  Detect the archive type and/or compression of a file.

    Args:
        file (str): the filename

    Returns:
        (tuple): tuple of suffix, archive type, and compression

    Raises:
        RuntimeError: if file has no suffix or suffix is not supported
    zFile 'zP' has no suffixes that could be used to detect the archive type and compression.NrH   z&Unknown compression or archive type: 'z'.
Known suffixes are: 'z'.)
pathlibPathsuffixesrk   r   r   r   r&   sortedset)r   r   rw   suffix2valid_suffixess        r+   _detect_file_typer      s    ||D!**HTFjk
 	
 b\F ##4+F344 $$vt## ))x=1rlG --'&88tV##C 23c:M6NNQTUmQnnoN
?xG`ao`pprs
ttr>   remove_finishedc                    t        |       \  }}}|st        d| d      |<t        j                  t	        j
                  |       j                  |||nd            }t        |   } || d      5 }t        |d      5 }|j                  |j                                ddd       ddd       |rt	        j                  |        t        j                  |      S # 1 sw Y   =xY w# 1 sw Y   AxY w)a  Decompress a file.

    The compression is automatically detected from the file name.

    Args:
        from_path (str): Path to the file to be decompressed.
        to_path (str): Path to the decompressed file. If omitted, ``from_path`` without compression extension is used.
        remove_finished (bool): If ``True``, remove the file after the extraction.

    Returns:
        (str): Path to the decompressed file.
    z*Couldn't detect a compression from suffix rI   Nr}   r2   r   )r   rk   r   r   r@   rb   rj   r   r!   r$   r#   remove)	r   r   r   rw   archive_typer   compressed_file_openerrfhwfhs	            r+   _decompressr   )  s    " ):)(D%FL+GxqQRR,,ryy3;;FT`TlLrtuv 6kB		4	0 Cgt9L PS		#((*  
		)<<     s$   1C&> CC&C#	C&&C/c           
          dt         j                  dt        t        t         j                  f   f fd}|t        j
                  j                         }t               \  }}}|sat         t        j
                  j                  |t        j
                  j                         j                  |d            |      } ||      S t        |   } | ||       |rt	        j                           |t        j                  |            S )aD  Extract an archive.

    The archive type and a possible compression is automatically detected from the file name. If the file is compressed
    but not an archive the call is dispatched to :func:`decompress`.

    Args:
        from_path (str): Path to the file to be extracted.
        to_path (str): Path to the directory the file will be extracted to. If omitted, the directory of the file is
            used.
        remove_finished (bool): If ``True``, remove the file after the extraction.

    Returns:
        (str): Path to the directory the file was extracted to.
    ret_pathr   c                 R    t        t              rt        j                  |       S | S r<   )
isinstancestrr@   rb   )r   r   s    r+   path_or_strz$extract_archive.<locals>.path_or_stra  s!    i%99X&&Or>   r}   )r   )r   r   r   r   r@   rA   dirnamer   r   rc   ra   rj   r   r   )	r   r   r   r   rw   r   r   r   	extractors	   `        r+   extract_archiver   M  s    (gll uS',,5F/G  ''//),(9)(D%FL+GGLL"''"2"29"="E"Efb"QR+

 8$$ $L1Ii+.
		)w||G,--r>   download_rootextract_rootc                 $   t         j                  j                  |      }||}|st         j                  j                  |       }t	        | |||       t         j                  j                  ||      }t        d| d|        t        |||       y )NzExtracting r\   )r@   rA   r`   ra   rn   rc   re   r   )r   r   r   r   r6   r   archives          r+   download_and_extract_archiver   }  s}     GG&&}5M$77##C(mXs3ggll=(3G	Ky\N
34G\?;r>   iterablec           	      h    ddj                  | D cg c]  }t        |       c}      z   dz   S c c}w )N'z', ')rc   r   )r   items     r+   iterable_to_strr     s-    H=Dc$i=>>DD=s   /
Tvalueargvalid_values
custom_msgc                    t        | t              s.|d}nd}|j                  t        |       |      }t	        |      || S | |vr9||}t	        |      d}|j                  | |t        |            }t	        |      | S )Nz'Expected type str, but got type {type}.z:Expected type str for argument {arg}, but got type {type}.)typer   zPUnknown value '{value}' for argument {arg}. Valid values are {{{valid_values}}}.)r   r   r   )r   r   formatr   
ValueErrorr   )r   r   r   r   msgs        r+   verify_str_argr     s     eS!;;CNCjjd5ksj3oL !C o eC**5cP\@]*^CoLr>   	file_nameslice_channelsc                    t        | d      5 }|j                         j                         }|dvrt        d      t	        j
                  d|j                               }|st        d      d |j                         D        \  }}t        |j                         j                               }|dk  rd}| }nd	}t        j                  ||d
z         }	ddd       dk(  rdnd}
	j                  |
      j                  ddd      }	t        j                  |	d      }	|	d|ddddf   }	|	j                  t        j                        S # 1 sw Y   zxY w)aD  Read file in .pfm format. Might contain either 1 or 3 channels of data.

    Args:
        file_name (str): Path to the file.
        slice_channels (int): Number of channels to slice out of the file.
            Useful for reading different data formats stored in .pfm files: Optical Flows, Stereo Disparity Maps, etc.
    r2   )   PFs   PfzInvalid PFM files   ^(\d+)\s(\d+)\s$zMalformed PFM header.c              3   2   K   | ]  }t        |        y wr<   )int).0dims     r+   	<genexpr>z_read_pfm.<locals>.<genexpr>  s     7SC7s   r   <>r8   )dtypeNr   r/   rH   r   )axis)r!   readlinerstripr   rQ   rR   	Exceptiongroupsfloatnpfromfilereshape	transposeflipastypefloat32)r   r   r8   header	dim_matchwhscaleendiandatapfm_channelss              r+   	_read_pfmr     s7    
i	 2!$$&'/00HH11::<@	3447I$4$4$671ajjl))+,19FFEF{{1FSL1#2& %1QL<<1l+55aA>D774a DA%&D;;rzz""12 2s   CEEtc                 >     | j                         j                  t        j                        j                  g | j                  | j                          j                  d      j                  g | j                  d d d j                  | j                        S )Nr   )
contiguousviewtorchuint8shapeelement_sizer   r   )r   s    r+   _flip_byte_orderr     s    W-EKK(--IqwwI8HINNrRWWjYZY`Y`adbdYejgijoopqpwpwxr>   )i   )i   r<   )r/   )NNr/   )F)NN)NF)NNNF)NNN)r   )Hbz2gzipr5   lzmar@   os.pathr   rQ   r3   r   r   urllib.errorurllib.requestr   typingr   r   r   r   r   r   r	   r
   r   r   urllib.parser   numpyr   r   torch.utils.model_zoor   _internally_replaced_utilsr   r   r    r   r   r   r,   r9   boolr=   rC   rN   rV   rn   rv   r{   rf   r   	ZIP_BZIP2ZIP_LZMAr   __annotations__r   r   r!   r   r   r   r   r   r   r   bytesr   r   ndarrayr   Tensorr    r>   r+   <module>r     s   
    	   	 
      [ [ [ !   & k
(c (U3+<%= (3 (_c (sGLL01 s UX 1U3,- 1C 13 14 1!5gll!23 !(3- !SW !
3 
# 
c 
 
3 
8C= 
  485;	5;
W\\!
"5; uS',,./05; 
#	5;
 5; 
5;p5gll*+ T d3i U3,- s D UYZ]U^ ( 48	#;#;
W\\!
"#; uS',,./0#; 
#	#;L S',,&' 27W\\8I2J YabeYf 	  ( d38n  S',,&' 27W\\8I2J YabeYf 	  w T#xsGLL/@)A5gllIZC[]efi]j(kmq(qrrs 
 HH9999: $sHS"W$556  F DeHSM8C=$@AAB (uE#w||"34 (usHSMS[\_S`?`9a (uZ 37!!!S',,&'!!eC-./!! !! \\	!!L 37!-.S',,&'-.eC-./-. -. 3	-.f 8<37!<	<gll*+< 5gll!234< uS',,./0	<
 
#< < 
<*Eh E3 E Ce
 *. $		# 8A;' 	
 8!#sGLL01 !#3 !#rzz !#H  r>   