
    PhI                     *   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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!Z!d dl"m#Z# ddl$m%Z%m&Z& dZ'	 dIdee(   d	e)d
ee*   ddfdZ+dJde)de)de*ddfdZ,dKde)de*de)fdZ-de)de)dede.fdZ/dIde)dee)   de.fdZ0dLde)de*de)fdZ1de)dee)   fdZ2	 dMde)de)dee)   dee)   de*ddfdZ3dNde)de.dee)   fdZ4dNde)de)de.dee)   fd Z5dJde*dee(ee(   f   fd!Z6dOd"e)de)dee)   dee)   fd#Z7d$e)d%e)d&ee)   ddfd'Z8ejr                  ejt                  d(Z;ee)e*f   e<d)<   d$e)d%e)d&ee)   ddfd*Z=e8e=d+Z>ee)ee)e)ee)   gdf   f   e<d,<   e j~                  ej~                  ej~                  d-Z@ee)ed.ef   f   e<d/<   d0d0d1d2ZAee)eee)   ee)   f   f   e<d3<   d4e)dee)ee)   ee)   f   fd5ZBdPd$e)d%ee)   d6e.de)fd7ZCdPd$e)d%ee)   d6e.de)fd8ZD	 	 	 	 dQde)d9e)d:ee)   dee)   dee)   d6e.ddfd;ZEd<ede)fd=ZF ed>e)e(      ZG	 	 	 dRd?eGd@ee)   dAeeeG      dBee)   deGf
dCZHdSdDe)dEe*dej                  fdFZJdGe!j                  de!j                  fdHZLy)T    N)
AnyCallableDictIOIterableIteratorListOptionalTupleTypeVar)urlparse)tqdm   )#_download_file_from_remote_location_is_remote_location_availablezpytorch/visioncontentdestinationlengthreturnc                     t        |d      5 }t        |      5 }| D ]0  }|s|j                  |       |j                  t	        |             2 	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nwb)total)openr   writeupdatelen)r   r   r   fhpbarchunks         eC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torchvision/datasets/utils.py_save_response_contentr!      s]    
 
k4	 B6(:dEHHUOKKE
#  );	 	 (:(:	 	 s"   A.6A"A."A+	'A..A7urlfilename
chunk_sizec                    t         j                  j                  t         j                  j                  | dt        i            5 t        t        fdd      |j                         d d d        y # 1 sw Y   y xY w)N
User-Agentheadersc                  &    j                         S N)read)r$   responses   r    <lambda>z_urlretrieve.<locals>.<lambda>0   s    HMM*,E    r.   )r   )urllibrequesturlopenRequest
USER_AGENTr!   iterr   )r"   r#   r$   r,   s     `@r    _urlretriever5   .   sZ    			 6 6s\S]D^ 6 _	`dlt$EsKX^f^m^mn 
a	`	`s   'A77B 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)r6   r$   r?   fr   s        r    calculate_md5rB   3   s     6!kk%0kkm	eT	avvj))e)JJu vvj))e) 
 ==? 
 ==?s   8BB4r?   kwargsc                      |t        | fi |k(  S r*   )rB   )r6   r?   rC   s      r    	check_md5rE   A   s    -0000r.   c                 `    t         j                  j                  |       sy|yt        | |      S )NFT)ospathisfilerE   )r6   r?   s     r    check_integrityrJ   E   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 .)r3   ranger/   r0   r1   r2   r"   RecursionError)r"   rK   initial_urlr(   _r,   s         r    _get_redirect_urlrU   M   s    Kz:G8a< ^^##FNN$:$:3$:$PQU]||s"hll&: RQ ,,C	 RQ ! +j
:bcfbgghi
 	
 RQs   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netlocrH   group)r"   partsrY   s      r    _get_google_drive_file_idr]   ]   sP    SME	xx.=EHH-uzz:E};;tr.   rootmax_redirect_hopsc                    t         j                  j                  |      }|st         j                  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)rK   zDownloading  to    httpszhttps:zhttp:z;Failed download. Trying https -> http instead. Downloading zFile not found or corrupted.)rG   rH   
expanduserbasenamejoinmakedirsrJ   printr   r   rU   r]   download_file_from_google_driver5   r/   errorURLErrorOSErrorreplaceRuntimeError)r"   r^   r#   r?   r_   r6   file_ides           r    download_urlrs   j   sa    77d#D77##C(GGLLx(E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 #E5.=E00E5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)rG   rH   rf   listdirisdirrh   )r^   rt   pdirectoriesds        r    list_dirr{      s     77d#D jj.W."''--TST@U2V1.KW~6ABkrww||D!,k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)rG   rH   rf   rv   rI   rh   endswith)r^   r|   rt   rx   filesrz   s         r    
list_filesr      s     77d#D

4(i(1BGGNN277<<a;P,QVWV`V`agVhQ(Ei~0561dA&6L j6s   AC:CC'C
c                    | j                  |      }d }|st        |      }|st        j                  |g|      }	 t	        j
                  d|j                               }||d   nd }||fS # t        $ r d }Y ||fS w xY w)Nz3<title>Google Drive - (?P<api_response>.+?)</title>api_response)iter_contentnext	itertoolschainrX   searchdecodeUnicodeDecodeError)r,   r$   r   first_chunkrY   r   s         r    _extract_gdrive_api_responser      s    ##J/GK7m oo{mW5G		OQ\QcQcQef050Au^,t       s   -A- -A>=A>rq   c                 f   t         j                  j                  |      }|s| }t         j                  j                  ||      }t        j                  |d       t        ||      rt        d|rdnd d|        yd}t        | d	
      }t        j                         5 }|j                  ||d      }|j                  j                         D ]  \  }	}
|	j                  d      s|
} n t        |      \  }}|dk(  rdnd}|-|j                  |t        ||      d      }t        |      \  }}dk(  rt        d| d      t!        |       ddd       t        j"                  |      j$                  dk  rtt'        j(                  t*              5  t-        |      5 }|j/                         }t1        j2                  d|      rt5        j6                  d|        ddd       ddd       |rt9        ||      st        d| d      yy# 1 sw Y   xY w# 1 sw Y   <xY w# 1 sw Y   @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
    Tra   zUsing downloaded zand verified  zfile: Nzhttps://drive.google.com/ucdownload)rW   export)paramsstreamdownload_warningzVirus scan warningt)confirmzQuota exceededzThe daily quota of the file z} is exceeded and it can't be downloaded. This is a limitation of Google Drive and can only be overcome by trying again later.i (  z5</?\s*[a-z-][^>]*\s*>|(&(?:[\w\d]+|#\d+|#x[a-f\d]+);)zWe detected some HTML elements in the downloaded file. This most likely means that the download triggered an unhandled API response by GDrive. Please report this to torchvision at https://github.com/pytorch/vision/issues including the response:

z&The MD5 checksum of the download file z does not match the one on record.Please delete the file and try again. If the issue persists, please report this to torchvision at https://github.com/pytorch/vision/issues.)rG   rH   rf   rh   ri   rJ   rj   dictrequestsSessiongetcookiesitems
startswithr   rp   r!   statst_size
contextlibsuppressr   r   r+   rX   r   warningswarnrE   )rq   r^   r#   r?   r6   r"   r   sessionr,   keyvaluetokenr   r   r   texts                   r    rk   rk      s    77d#DGGLLx(EKKt$uc"!S/b!AwOP
'CWZ0F				w;;s6$;?"**002JC~~01 3
 %A$J!L''+??CTE{{3tFE/JSW{XH$@$J!L'++.xj 9B C  	w.- 
2 
wwu~	)  !34d5kR779DyyQSWX( )-v/	 7B4 9UC(4UG <t u
 	
 )sK 
	4 7Bk44s8    AH'A+HH'!?H H'HH$	 H''H0	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:rO   r)tarfiler   
extractall)r   r   r   tars       r    _extract_tarr     s>    	i;2k!"o%6!7C	PTWw 
Q	P	Ps	   <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     sE    	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.NrO   z&Unknown compression or archive type: 'z'.
Known suffixes are: 'z'.)
pathlibPathsuffixesrp   r   r   r   r   sortedset)r   r   r|   suffix2valid_suffixess        r    _detect_file_typer   5  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                 r   t        |       \  }}}|st        d| d      || j                  |||nd      }t        |   } || d      5 }t	        |d      5 }|j                  |j                                ddd       ddd       |rt        j                  |        |S # 1 sw Y   *xY w# 1 sw Y   .x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 rP   Nr   r;   r   )	r   rp   ro   r   r   r   r+   rG   remove)	r   r   r   r|   archive_typer   compressed_file_openerrfhwfhs	            r    _decompressr   `  s     ):)(D%FL+GxqQRR##FL<TLZ\] 6kB		4	0Cgt9LPS		#((* :M	0 
		)N :M9L	0	0s$   B- B!8B-!B*	&B--B6c           
      p   |t         j                  j                  |       }t        |       \  }}}|sYt	        | t         j                  j                  |t         j                  j                  |       j                  |d            |      S t        |   } || ||       |r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.
    r   )r   )
rG   rH   dirnamer   r   rh   rg   ro   r   r   )r   r   r   r|   r   r   	extractors          r    extract_archiver     s     ''//),(9)(D%FL+GGLL"''"2"29"="E"Efb"QR+
 	
 $L1Ii+.
		)Nr.   download_rootextract_rootc                 $   t         j                  j                  |      }||}|st         j                  j                  |       }t	        | |||       t         j                  j                  ||      }t        d| d|        t        |||       y )NzExtracting rc   )rG   rH   rf   rg   rs   rh   rj   r   )r"   r   r   r#   r?   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', ')rh   str)r   items     r    iterable_to_strr     s1    H=HDc$iH=>>DD=s   /
Tr   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   )
isinstancer   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.
    r;   )   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     7$6SC$6s   r   <>rA   )dtypeNr   r8   rO   r   )axis)r   readlinerstripr   rX   rY   	Exceptiongroupsfloatnpfromfilereshape	transposeflipastypefloat32)r   r   rA   header	dim_matchwhscaleendiandatapfm_channelss              r    	_read_pfmr    s0    
i	!$$&'/00HH11::<@	3447I$4$4$671ajjl))+,19FFEF{{1FSL1# 
& %1QL<<1l+55aA>D774a DA%&D;;rzz""1 
	s   CEEr   c                 >     | 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.   r*   )i   )i   )r8   )NNr8   )F)NN)NF)NNNF)NNN)r   )Mbz2r   gzipr>   r   lzmarG   os.pathr   rX   r<   r   r/   urllib.errorurllib.requestr   r   typingr   r   r   r   r   r   r	   r
   r   r   urllib.parser   numpyr   r   r  torch.utils.model_zoor   _internally_replaced_utilsr   r   r3   bytesr   r   r!   r5   rB   boolrE   rJ   rU   r]   rs   r{   r   r   rk   r   	ZIP_BZIP2ZIP_LZMAr   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   ndarrayr  Tensorr   r.   r    <module>r)     s   
      	   	 
       ^ ^ ^ !    & k
 !$e_$$ SM$ 
	$oc oS oc o$ o
 #  1S 1s 1c 1d 1!3 !Xc] !d !
3 
# 
c 
 
3 
8C= 
 no1;	1;1;#+C=1;>Fsm1;gj1;	1;h3  c S # t S	 "!s !5QVX`afXgQgKh ! B
S B
 B
xPS} B
bjknbo B
J C  #  HSM  d   ( d38n  C  #  HSM  d   M T#xc8C=(A4(GHHI 
 HH9999: $sHS"W$556  F DeHSM8C=$@AAB (uC (uE#x}hsm*K$L (uV3 # PT ad @!s !Xc] !TX !eh !N #'"!<	<< 3-< sm	<
 
#< < 
<*Eh E3 E Ce
 *. $		# 8A;' 	
 8!# !#c !#"** !#H  r.   