
    Yh*>                        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mZ d dl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 dId
e!dee!ej"        f         de#ddfdZ$dJdee!ej"        f         de#de!fdZ%dee!ej"        f         de!dede&fdZ'dKdee!ej"        f         dee!         de&fdZ(dLd
e!de#de!fdZ)d
e!dee!         fdZ*	 	 	 dMd
e!dee!ej"        f         deee!ej"        f                  dee!         de#ddfdZ+dNdee!ej"        f         de&de,e!         fdZ-dNdee!ej"        f         d e!de&de,e!         fd!Z.	 	 dOd"e!dee!ej"        f         deee!ej"        f                  dee!         fd#Z/d$ee!ej"        f         d%ee!ej"        f         d&ee!         ddfd'Z0ej1        ej2        d(Z3e4e!e#f         e5d)<   d$ee!ej"        f         d%ee!ej"        f         d&ee!         ddfd*Z6e0e6d+Z7e4e!eee!ej"        f         ee!ej"        f         ee!         gdf         f         e5d,<   e j8        ej8        ej8        d-Z9e4e!ed.ef         f         e5d/<   d0d0d1d2Z:e4e!e;ee!         ee!         f         f         e5d3<   d4ee!ej"        f         de;e!ee!         ee!         f         fd5Z<	 	 dPd$ee!ej"        f         d%eee!ej"        f                  d6e&dej"        fd7Z=	 	 dPd$ee!ej"        f         d%eee!ej"        f                  d6e&dee!ej"        f         fd8Z>	 	 	 	 dQd
e!d9ee!ej"        f         d:eee!ej"        f                  deee!ej"        f                  dee!         d6e&ddfd;Z?d<ede!fd=Z@ ed>e!eA          ZB	 	 	 dRd?eBd@ee!         dAeeeB                  dBee!         deBf
dCZCdSdDee!ej"        f         dEe#dejD        fdFZEdGejF        dejF        fdHZGdS )T    N)Iterable)AnyCallableIOOptionalTypeVarUnion)urlparse)tqdm   )#_download_file_from_remote_location_is_remote_location_availablezpytorch/vision   urlfilename
chunk_sizereturnc                 :   t           j                            t           j                            | dt          i                    5 }t          |d          5 }t          |j        dd          5 }|                    |          x}rN|	                    |           |
                    t          |                     |                    |          x}Nd d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )N
User-AgentheaderswbBT)totalunit
unit_scale)urllibrequesturlopenRequest
USER_AGENTopenr   lengthreadwriteupdatelen)r   r   r   responsefhpbarchunks          l/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/datasets/utils.py_urlretriever-      s   			 6 6s\S]D^ 6 _ _	`	` (dl(D!! 	(RHO#Z^)_)_)_ 	(cg#==444% (CJJ''' $==444% (	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s[   DC8/A&C!C8!C%%C8(C%)C8,D8C<	<D?C<	 DDD   fpathc                 *   t          j        d          }t          | d          5 }|                    |          x}r,|                    |           |                    |          x},d d d            n# 1 swxY w Y   |                                S )NF)usedforsecurityrb)hashlibmd5r"   r$   r&   	hexdigest)r/   r   r4   fr+   s        r,   calculate_md5r7   #   s     +e
,
,
,C	eT		 avvj)))e 	JJu vvj)))e 	               ==??s   AA66A:=A:r4   kwargsc                 $    |t          | fi |k    S N)r7   )r/   r4   r8   s      r,   	check_md5r;   .   s    -000000    c                 l    t           j                            |           sdS |dS t          | |          S )NFT)ospathisfiler;   )r/   r4   s     r,   check_integrityrA   2   s7    7>>%   u
{tUC   r<      max_hopsc           	      t   | }dt           d}t          |dz             D ]}t          j                            t          j                            | |                    5 }|j        | k    s|j        | cd d d            c S |j        } d d d            n# 1 swxY w Y   t          d| d| d|  d          )	NHEAD)Methodr      r   zRequest to z
 exceeded z( redirects. The last redirect points to .)r!   ranger   r   r   r    r   RecursionError)r   rC   initial_urlr   _r(   s         r,   _get_redirect_urlrM   :   s3   Kz::G8a<   	
 	
^##FN$:$:3$:$P$PQQ 	U]|s""hl&:	 	 	 	 	 	 	 	 	 	 ,C		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 i+iiiicfiii
 
 	
s   BBB	B	c                     t          |           }t          j        d|j                  d S t          j        d|j                  }|d S |                    d          S )Nz(drive|docs)[.]google[.]comz/file/d/(?P<id>[^/]*)id)r
   rematchnetlocr?   group)r   partsrQ   s      r,   _get_google_drive_file_idrU   J   sW    SMME	x.==EtH-uz::E}t;;tr<   rootmax_redirect_hopsc                     t           j                            |          }|st           j                            |           }t          j        t           j                            ||                    }t          j        |d           t          ||          rdS t                      rt          ||            nt          | |          } t          |           }|t          ||||          S 	 t          | |           n_# t          j        j        t"          f$ rA}| dd         dk    r'|                     dd          } t          | |           n|Y d}~nd}~ww xY wt          ||          st'          d	          dS )
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_okN)rC      httpszhttps:zhttp:File not found or corrupted.)r>   r?   
expanduserbasenamefspathjoinmakedirsrA   r   r   rM   rU   download_file_from_google_driver-   r   errorURLErrorOSErrorreplaceRuntimeError)r   rV   r   r4   rW   r/   file_ides           r,   download_urlrk   W   s     7d##D )7##C((Ibgll42233EKt$$$$ uc"" $&& +E37777  .?@@@ ,C0027D(CPPP	e$$$$%w/ 	 	 	2A2w'!!kk(G44S%(((( )((((	 5#&& ;9:::; ;s   /D   E7EEFprefixc                      t           j                                         fdt          j                   D             }|du r fd|D             }|S )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
    c                     g | ]A}t           j                            t           j                            |                    ?|BS  )r>   r?   isdirra   ).0prV   s     r,   
<listcomp>zlist_dir.<locals>.<listcomp>   s=    WWW"'--TST@U@U2V2VW1WWWr<   Tc                 P    g | ]"}t           j                            |          #S ro   r>   r?   ra   rq   drV   s     r,   rs   zlist_dir.<locals>.<listcomp>   s)    BBBrw||D!,,BBBr<   r>   r?   r^   listdir)rV   rl   directoriess   `  r,   list_dirr{      se     7d##DWWWWbj..WWWK~~BBBBkBBBr<   suffixc                      t           j                                         fdt          j                   D             }|du r fd|D             }|S )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
    c                     g | ]V}t           j                            t           j                            |                    ?|                              T|WS ro   )r>   r?   r@   ra   endswith)rq   rr   rV   r|   s     r,   rs   zlist_files.<locals>.<listcomp>   sQ    iii1BGNN27<<a;P;P,Q,QiVWV`V`agVhVhiQiiir<   Tc                 P    g | ]"}t           j                            |          #S ro   ru   rv   s     r,   rs   zlist_files.<locals>.<listcomp>   s)    6661dA&&666r<   rx   )rV   r|   rl   filess   ``  r,   
list_filesr      sh     7d##Diiiii
4((iiiE~~6666666Lr<   ri   c                    	 ddl }n# t          $ r t          d          w xY wt          j                            |          }|s| }t          j        t          j                            ||                    }t          j        |d           t          ||          rdS |
                    | |dt                     t          ||          st          d          dS )	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'.TrY   F)rO   outputquiet
user_agentr]   )gdownModuleNotFoundErrorrh   r>   r?   r^   r`   ra   rb   rA   downloadr!   )ri   rV   r   r4   r   r/   s         r,   rc   rc      s    
 
 
 
n
 
 	


 7d##D Ibgll42233EKt$$$$uc"" 	NNge5ZNPPP5#&& ;9:::; ;s    !	from_pathto_pathcompressionc                     t          j        | |rd|dd           nd          5 }|                    |           d d d            d S # 1 swxY w Y   d S )Nzr:rG   r)tarfiler"   
extractall)r   r   r   tars       r,   _extract_tarr      s     
i;!O!7k!""o!7!7!7C	P	P  TWw                                   s   AAA).bz2.xz_ZIP_COMPRESSION_MAPc                     t          j        | d|rt          |         nt           j                  5 }|                    |           d d d            d S # 1 swxY w Y   d S )Nr   )r   )zipfileZipFiler   
ZIP_STOREDr   )r   r   r   zips       r,   _extract_zipr      s     
3$l$8$E$EZaZl
 
 
  	w                                   s   A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          |         R S |t
          v r||dfS |t          v r1t          |          dk    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.NrG   z&Unknown compression or archive type: 'z'.
Known suffixes are: 'z'.)
pathlibPathsuffixesrh   r   r   r   r'   sortedset)r   r   r|   suffix2valid_suffixess        r,   _detect_file_typer      s4    |D!!*H 
kTkkk
 
 	
 b\F ###4+F3444 $$$vt## )))x==1rlG ---'&88tV##C 233c:M6N6NNQTUmQnQnnooN
sssaosss
t
ttr<   remove_finishedc                 &   t          |           \  }}}|st          d| d          |>t          j        t	          j        |                               |||nd                    }t          |         } || d          5 }t          |d          5 }|	                    |
                                           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |rt	          j        |            t          j        |          S )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 rH   N r2   r   )r   rh   r   r   r>   r`   rg   r   r"   r%   r$   remove)	r   r   r   r|   archive_typer   compressed_file_openerrfhwfhs	            r,   _decompressr   "  s   " ):)(D(D%FL+ SQQQQRRR,ry33;;FT`TlLLrtuuvv 6kB			4	0	0 Cgt9L9L PS		#((**                               
	)<   s6   C(C;CC	CC	CC"%C"c           
      B    dt           j        dt          t          t           j        f         f fd}|t          j                                       }t                     \  }}}|slt           t          j        	                    |t          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                 Z    t          t                    rt          j        |           S | S r:   )
isinstancestrr>   r`   )r   r   s    r,   path_or_strz$extract_archive.<locals>.path_or_strZ  s*    i%% 	9X&&&Or<   Nr   )r   )r   r   r	   r   r>   r?   dirnamer   r   ra   r_   rg   r   r   )	r   r   r   r   r|   r   r   r   	extractors	   `        r,   extract_archiver   F  s$   (gl uS',5F/G       '//),,(9)(D(D%FL+ %GLL"'"2"29"="="E"Efb"Q"QRR+
 
 

 {8$$$ $L1IIi+... 
	);w|G,,---r<   download_rootextract_rootc                    t           j                            |          }||}|st           j                            |           }t	          | |||           t           j                            ||          }t          |||           d S r:   )r>   r?   r^   r_   rk   ra   r   )r   r   r   r   r4   r   archives          r,   download_and_extract_archiver   v  s     G&&}55M$ )7##C((mXs333gll=(33GG\?;;;;;r<   iterablec                 L    dd                     d | D                       z   dz   S )N'z', 'c                 ,    g | ]}t          |          S ro   )r   )rq   items     r,   rs   z#iterable_to_str.<locals>.<listcomp>  s    ===Dc$ii===r<   )ra   )r   s    r,   iterable_to_strr     s-    ==H===>>>DDr<   Tvalueargvalid_values
custom_msgc                 *   t          | t                    s:|d}nd}|                    t          |           |          }t	          |          || S | |vr;||}n'd}|                    | |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!! ;;CCNCjjd5kksj33ooL  !CCdC**5cP\@]@]*^^CooLr<   	file_nameslice_channelsc                 "   t          | d          5 }|                                                                }|dvrt          d          t	          j        d|                                          }|st          d          d |                                D             \  }}t          |                                                                          }|dk     rd}| }nd	}t          j
        ||d
z             }	ddd           n# 1 swxY w Y   |dk    rdnd}
|	                    |||
                              ddd          }	t          j        |	d          }	|	d|ddddf         }	|	                    t          j                  S )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   4   K   | ]}t          |          V  d S r:   )int)rq   dims     r,   	<genexpr>z_read_pfm.<locals>.<genexpr>  s(      77SC777777r<   r   <>r6   )dtypeNr   rB   rG   r   )axis)r"   readlinerstripr   rP   rQ   	Exceptiongroupsfloatnpfromfilereshape	transposeflipastypefloat32)r   r   r6   header	dim_matchwhscaleendiandatapfm_channelss              r,   	_read_pfmr     s    
i		 2!$$&&''/000H11::<<@@	 	5344477I$4$4$6$67771ajjll))++,,199FFEEF{1FSL111#2 2 2 2 2 2 2 2 2 2 2 2 2 2 2& %11QL<<1l++55aA>>D74a   DAAA%&D;;rz"""s   C-D

DDtc                 6     |                                                      t          j                  j        g | j        |                                 R                      d          j        g | j        d d         dR                      | j                  S )Nr   )
contiguousviewtorchuint8shapeelement_sizer   r   )r   s    r,   _flip_byte_orderr     s    W-EK((-IqwI8H8HIIINNrRRWjYZY`adbdadYejgijjjoopqpwxxr<   )r   )r.   r:   )rB   )NNrB   )F)NN)NF)NNNF)NNN)r   )Hbz2gzipr3   lzmar>   os.pathr   rP   r   r   urllib.errorurllib.requestr   collections.abcr   typingr   r   r   r   r   r	   urllib.parser
   numpyr   r   torch.utils.model_zoor   _internally_replaced_utilsr   r   r!   r   r   r   r-   r7   boolr;   rA   rM   rU   rk   listr{   r   rc   r   	ZIP_BZIP2ZIP_LZMAr   dict__annotations__r   r   r"   r   r   tupler   r   r   r   r   bytesr   r   ndarrayr   Tensorr   ro   r<   r,   <module>r     s]   




    				   				            $ $ $ $ $ $ > > > > > > > > > > > > > > > > ! ! ! ! ! !      & & & & & & k k k k k k k k
( (c (U3+<%= (3 (_c ( ( ( ( sGL01 s UX    1U3,- 1C 13 14 1 1 1 1! !5gl!23 !(3- !SW ! ! ! !
 
3 
# 
c 
 
 
 
 
3 
8C= 
 
 
 
  482; 2;	2;
W\!
"2; uS',./02; 
#	2;
 2; 
2; 2; 2; 2;j 5gl*+ T d3i     U3,- s D UYZ]U^    ( 48	"; ";";
W\!
""; uS',./0"; 
#	"; "; "; ";J S',&' 27W\8I2J YabeYf 	        ( ( d38n    S',&' 27W\8I2J YabeYf 	        w w T#xsGL/@)A5glIZC[]efi]j(kmq(qrrs   
 H99: : $sHS"W$556    F F DeHSM8C=$@AAB   (uE#w|"34 (usHSMS[\_S`?`9a (u (u (u (uZ 37!!! !!S',&'!!eC-./!! !! \	!! !! !! !!L 37!-. -.S',&'-.eC-./-. -. 3	-. -. -. -.f 8<37!< <	<gl*+< 5gl!234< uS',./0	<
 
#< < 
< < < <(Eh E3 E E E E GCe
 *. $	 	# 8A;' 	
    8!# !#sGL01 !#3 !#rz !# !# !# !#H       r<   