CoolFace
Apppublic

undercovercd/Swap-Face-Model

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
data_util.cpython-39.pyc191 linesDownload Raw Back to __pycache__
1a

2��ek/�@s�ddlZddlZddlZddlmZddlmZ	ddl3mZddlm
Z
mZddd	�Zddd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zd dd�Zd!dd�ZdS)"�N)�path)�4functional��mod_crop)�5img2tensor�scandirF�cs�t|t�r|}nttt|dd���}dd�|D�}|rH�fdd�|D�}t|ddd�}tj|dd�}|r~d	d�|D�}||fS|Sd6S)a�Read a sequence of images from a given folder path.7 8    Args:9        path (list[str] | str): List of image paths or image folder path.10        require_mod_crop (bool): Require mod crop for each image.11            Default: False.12        scale (int): Scale factor for mod_crop. Default: 1.13        return_imgname(bool): Whether return image names. Default False.14 15    Returns:16        Tensor: size (t, c, h, w), RGB, [0, 1].17        list[str]: Returned image name list.18    T)�	full_pathcSs"g|]}t�|��tj�d�qS)g�o@)�cv2�imread�astype�np�float32)�.0�v�r�&D:\face swap\basicsr\data\data_util.py�19<listcomp>�z read_img_seq.<locals>.<listcomp>csg|]}t|���qSrr)r�img��scalerrr r)�bgr2rgbrr)�dimcSs g|]}t�t�|��d�qS)r)�osp�splitext�basename�rrrrrr%rN)�20isinstance�list�sortedrr�torch�stack)rZrequire_mod_croprZreturn_imgnameZ	img_paths�imgsZimgnamesrrr�read_img_seqs21r$�22reflectioncCs|ddksJd��|dvs,Jd|�d���|d}|d}g}t||||d�D]�}|dkr�|dkrpd}q�|d	kr�|}q�|d23kr�|||}q�||}nT||kr�|dkr�|}q�|d	kr�|d|}q�|d24kr�||||}q�||}n|}|�|�qV|S)a�Generate an index list for reading `num_frames` frames from a sequence25    of images.26 27    Args:28        crt_idx (int): Current center index.29        max_frame_num (int): Max number of the sequence of images (from 1).30        num_frames (int): Reading num_frames frames.31        padding (str): Padding mode, one of32            'replicate' | 'reflection' | 'reflection_circle' | 'circle'33            Examples: current_idx = 0, num_frames = 534            The generated frame indices under different padding mode:35            replicate: [0, 0, 0, 1, 2]36            reflection: [2, 1, 0, 1, 2]37            reflection_circle: [4, 3, 0, 1, 2]38            circle: [3, 4, 0, 1, 2]39 40    Returns:41        list[int]: A list of indices.42    �rz#num_frames should be an odd number.)�	replicater%�reflection_circle�circlezWrong padding mode: �.rr'r%r()�range�append)Zcrt_idxZ
max_frame_num�43num_frames�paddingZnum_pad�indices�i�pad_idxrrr�generate_frame_indices+s24445r2cCsrt|�dksJdt|�����t|�dks<Jdt|�����|\}}|\}}|�d�r`|�d�s�t|�d|�d|�d|�d|�d|����tt�|d	���}d46d�|D�}Wd�n1s�0Ytt�|d	���}d
d�|D�}Wd�n1s�0Yt|�t|�k�r0td|�d|�d���n>g}	t|�D],}47|	�t	|�d�|48f|�d�|49fg���q<|	SdS)u�Generate paired paths from lmdb files.50 51    Contents of lmdb. Taking the `lq.lmdb` for example, the file structure is:52 53    ::54 55        lq.lmdb56        ├── data.mdb57        ├── lock.mdb58        ├── meta_info.txt59 60    The data.mdb and lock.mdb are standard lmdb files and you can refer to61    https://lmdb.readthedocs.io/en/release/ for more details.62 63    The meta_info.txt is a specified txt file to record the meta information64    of our datasets. It will be automatically created when preparing65    datasets by our provided dataset tools.66    Each line in the txt file records67    1)image name (with extension),68    2)image shape,69    3)compression level, separated by a white space.70    Example: `baboon.png (120,125,3) 1`71 72    We use the image name without extension as the lmdb key.73    Note that we use the same key for the corresponding lq and gt images.74 75    Args:76        folders (list[str]): A list of folder path. The order of list should77            be [input_folder, gt_folder].78        keys (list[str]): A list of keys identifying folders. The order should79            be in consistent with folders, e.g., ['lq', 'gt'].80            Note that this key is different from lmdb keys.81 82    Returns:83        list[str]: Returned path list.84    r&�GThe len of folders should be 2 with [input_folder, gt_folder]. But got �>The len of keys should be 2 with [input_key, gt_key]. But got �.lmdbz folder and z2 folder should both in lmdb formats. But received z: z; �
meta_info.txtcSsg|]}|�d�d�qS�r*r��split�r�linerrrr�rz*paired_paths_from_lmdb.<locals>.<listcomp>NcSsg|]}|�d�d�qSr7r8r:rrrr�rzKeys in z_folder and z_folder are different.�_path)85�len�endswith�86ValueError�openr�join�setr r,�dict)�folders�keys�input_folder�	gt_folder�	input_key�gt_key�finZinput_lmdb_keysZgt_lmdb_keys�pathsZlmdb_keyrrr�paired_paths_from_lmdb_s4%�����,,*rLcCs�t|�dksJdt|�����t|�dks<Jdt|�����|\}}|\}}t|d��}dd�|D�}	Wd�n1sz0Yg}87|	D]h}t�t�|��\}}
|�|��|
��}t�||�}t�||�}|88�t|�d�|f|�d�|fg��q�|89S)	a}Generate paired paths from an meta information file.90 91    Each line in the meta information file contains the image names and92    image shape (usually for gt), separated by a white space.93 94    Example of an meta information file:95    ```96    0001_s001.png (480,480,3)97    0001_s002.png (480,480,3)98    ```99 100    Args:101        folders (list[str]): A list of folder path. The order of list should102            be [input_folder, gt_folder].103        keys (list[str]): A list of keys identifying folders. The order should104            be in consistent with folders, e.g., ['lq', 'gt'].105        meta_info_file (str): Path to the meta information file.106        filename_tmpl (str): Template for each filename. Note that the107            template excludes the file extension. Usually the filename_tmpl is108            for files in the input folder.109 110    Returns:111        list[str]: Returned path list.112    r&r3r4�rcSsg|]}|���d�d�qS)� r)�stripr9r:rrrr�rz4paired_paths_from_meta_info_file.<locals>.<listcomp>Nr<)	r=r@rrr�formatrAr,rC)rDrE�meta_info_file�
filename_tmplrFrGrHrIrJZgt_namesrKZgt_namer�ext�113input_name�114input_path�gt_pathrrr� paired_paths_from_meta_info_file�s �,(rWc	Cs.t|�dksJdt|�����t|�dks<Jdt|�����|\}}|\}}tt|��}tt|��}t|�t|�ks�J|�d|�dt|��dt|��d���g}	|D]�}115t�t�|116��\}}|�|��|��}
t�||
�}|
|vs�J|
�d|�d	���t�||117�}118|	�t	|�d119�|f|�d120�|121fg��q�|	S)a.Generate paired paths from folders.122 123    Args:124        folders (list[str]): A list of folder path. The order of list should125            be [input_folder, gt_folder].126        keys (list[str]): A list of keys identifying folders. The order should127            be in consistent with folders, e.g., ['lq', 'gt'].128        filename_tmpl (str): Template for each filename. Note that the129            template excludes the file extension. Usually the filename_tmpl is130            for files in the input folder.131 132    Returns:133        list[str]: Returned path list.134    r&r3r4z and z+ datasets have different number of images: z, r*z is not in z_paths.r<)135r=rrrrrrPrAr,rC)rDrErRrFrGrHrIZinput_pathsZgt_pathsrKrVrrSrTrUrrr�paired_paths_from_folder�s,���136(rXcs"tt���}�fdd�|D�}|S)z�Generate paths from folder.137 138    Args:139        folder (str): Folder path.140 141    Returns:142        list[str]: Returned path list.143    csg|]}t��|��qSr)rrAr��folderrrr�rz%paths_from_folder.<locals>.<listcomp>)rr)rZrKrrYr�paths_from_folder�s144r[cCs\|�d�std|�d���tt�|d���}dd�|D�}Wd�n1sN0Y|S)z�Generate paths from lmdb.145 146    Args:147        folder (str): Folder path.148 149    Returns:150        list[str]: Returned path list.151    r5zFolder zfolder should in lmdb format.r6cSsg|]}|�d�d�qSr7r8r:rrrrrz#paths_from_lmdb.<locals>.<listcomp>N)r>r?r@rrA)rZrJrKrrr�paths_from_lmdb�s152	153,r\�
皙�����?cCs:ddlm}t�||f�}d||d|df<|�||�S)z�Generate Gaussian kernel used in `duf_downsample`.154 155    Args:156        kernel_size (int): Kernel size. Default: 13.157        sigma (float): Sigma of the Gaussian kernel. Default: 1.6.158 159    Returns:160        np.array: The Gaussian kernel.161    r)�filtersrr&)�
scipy.ndimager_r
�zeros�gaussian_filter)�kernel_size�sigmar_�kernelrrr�generate_gaussian_kernels162rf�c	Cs|dvsJd|�d���d}|jdkr4d}|�d�}|��\}}}}}|�dd	||�}|d163|d164|d165|d166}	}167t�||	|	|168|169fd�}t|d|�}t�|��	|��d��d�}tj170|||d
�}|dd�dd�d171d�d172d�f}|�||||�d173�|�d��}|�r|�d�}|S)abDownsamping with Gaussian kernel used in the DUF official code.174 175    Args:176        x (Tensor): Frames to be downsampled, with shape (b, t, c, h, w).177        kernel_size (int): Kernel size. Default: 13.178        scale (int): Downsampling factor. Supported scale: (2, 3, 4).179            Default: 4.180 181    Returns:182        Tensor: DUF downsampled frames.183    )r&�rgz&Only support scale (2, 3, 4), but got r*FrgTr�����rr&�reflectg�������?)�strideN�����rh)�ndim�	unsqueeze�size�view�F�padrfr!�184from_numpy�type_as�conv2d�squeeze)�xrcrZsqueeze_flag�b�t�c�h�w�pad_w�pad_hrbrrr�duf_downsamples"185186" 187r)FrF)r%)r]r^)r]rg)r188�numpyr
r!�osrr�torch.nnrrq�basicsr.data.transformsr�
basicsr.utilsrrr$r2rLrWrXr[r\rfrrrrr�<module>s189 1904=,$191