a
    
d                  	   @   s   d dl Z d dlZd dlmZ d dlZd dlmZmZ d dl	m
Z
 d dlZejejejejeZdd ZdddZdd	d
ZdddZdddZdS )    N)download_url_to_fileget_dir)urlparsec                 C   s   t j|dd |  D ]\}}d| }tt||}t|rt| d}| dkrt	d| d|  t
j||dd	 q| d
krt	d|  qtdqt	d| d|  t
j||dd	 qd S )NTexist_okzhttps://drive.google.com/uc?id=z- already exist. Do you want to cover it? Y/N
yz	Covering z to F)quietnz	Skipping zWrong input. Only accepts Y/N.zDownloading )osmakedirsitemsospabspathjoinexistsinputlowerprintgdowndownload
ValueError)Zfile_idsZsave_path_root	file_namefile_idZfile_url	save_pathZuser_response r   R/var/www/html/stable-diffusion-webui/repositories/CodeFormer/facelib/utils/misc.pydownload_pretrained_models   s    

r   Tc                 C   s4   |r&t jt j|}t j|dd t|| |S )ay  Write image to file.

    Args:
        img (ndarray): Image array to be written.
        file_path (str): Image file path.
        params (None or list): Same as opencv's :func:`imwrite` interface.
        auto_mkdir (bool): If the parent folder of `file_path` does not exist,
            whether to create it automatically.

    Returns:
        bool: Successful or not.
    Tr   )r
   pathr   dirnamer   cv2imwrite)img	file_pathparams
auto_mkdirdir_namer   r   r   r    $   s    r    c                    s8   dd  t | tr( fdd| D S  | S dS )aO  Numpy array to tensor.

    Args:
        imgs (list[ndarray] | ndarray): Input images.
        bgr2rgb (bool): Whether to change bgr to rgb.
        float32 (bool): Whether to change to float32.

    Returns:
        list[tensor] | tensor: Tensor images. If returned results only have
            one element, just return tensor.
    c                 S   sX   | j d dkr4|r4| jdkr&| d} t| tj} t| ddd} |rT| 	 } | S )N      float64float32r      )
shapedtypeastyper   cvtColorCOLOR_BGR2RGBtorch
from_numpy	transposefloat)r!   bgr2rgbr)   r   r   r   	_totensorD   s    

zimg2tensor.<locals>._totensorc                    s   g | ]} |qS r   r   ).0r!   r5   r4   r)   r   r   
<listcomp>O       zimg2tensor.<locals>.<listcomp>N)
isinstancelist)imgsr4   r)   r   r7   r   
img2tensor7   s    

r=   c                 C   s   |du rt  }tj|d}tjtjt|dd t| }tj|j}|durV|}tjtjt||}tj	|st
d|  d| d t| |d|d |S )	zWRef:https://github.com/1adrianb/face-alignment/blob/master/face_alignment/utils.py
    NcheckpointsTr   zDownloading: "z" to 
)hash_prefixprogress)r   r
   r   r   r   ROOT_DIRr   basenamer   r   r   r   )url	model_dirrA   r   hub_dirpartsfilenamecached_filer   r   r   load_file_from_urlT   s    rJ   Fc                    s@   |durt |ttfstd|  fdd  | ||dS )a  Scan a directory to find the interested files.
    Args:
        dir_path (str): Path of the directory.
        suffix (str | tuple(str), optional): File suffix that we are
            interested in. Default: None.
        recursive (bool, optional): If set to True, recursively scan the
            directory. Default: False.
        full_path (bool, optional): If set to True, include the dir_path.
            Default: False.
    Returns:
        A generator for all the interested files with relative paths.
    Nz-"suffix" must be a string or tuple of stringsc                 3   s   t | D ]r}|jds^| r^r.|j}nt|j}|d u rL|V  q|||r||V  q
|r
 |j||dE d H  q
q
q
d S )N.suffix	recursive)	r
   scandirname
startswithis_filer   r   relpathendswith)dir_pathrM   rN   entryreturn_path_scandir	full_pathrootr   r   rY   {   s    
zscandir.<locals>._scandirrL   )r:   strtuple	TypeError)rU   rM   rN   rZ   r   rX   r   rO   h   s
    rO   )NT)TT)NTN)NFF)r   r
   os.pathr   r   r0   	torch.hubr   r   urllib.parser   r   r   r   __file__rB   r   r    r=   rJ   rO   r   r   r   r   <module>   s   $


