a
    Ad7                     @   st   d dl Z d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZ d dlmZmZ G dd deZd	d
 ZdS )    N)ImageRealESRGANer)UpscalerUpscalerData)cmd_optsopts)modelloadererrorsc                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	UpscalerRealESRGANc           	         s   d| _ || _t   zddlm} ddlm} ddlm	} d| _
g | _| |}| jdgd}|D ]T}|jd	rt|j  fd
d|D }|r|d |_|j tjv rd| j| qdW n, ty   tjddd d| _
g | _Y n0 d S )NZ
RealESRGANr   RRDBNetr   SRVGGNetCompactT.pth)
ext_filterhttpc                    s    g | ]}|   d r|qS )r   )endswith).0local_modelfilename @/var/www/html/stable-diffusion-webui/modules/realesrgan_model.py
<listcomp>       z/UpscalerRealESRGAN.__init__.<locals>.<listcomp>zError importing Real-ESRGANexc_infoF)name	user_pathsuper__init__basicsr.archs.rrdbnet_archr   
realesrganr   realesrgan.archs.srvgg_archr   enablescalersload_modelsfind_modelslocal_data_path
startswithr	   friendly_namer   realesrgan_enabled_modelsappend	Exceptionr
   report)	selfpathr   r   r   r&   local_model_pathsscalerZlocal_model_candidates	__class__r   r   r!      s.    


zUpscalerRealESRGAN.__init__c              	   C   s   | j s
|S z| |}W n* tyB   tjd| dd | Y S 0 t|j|j| t	j
 obt	j tjtj| jd}|jt||jdd }t|}|S )Nz Unable to load RealESRGAN model Tr   )scale
model_pathmodelhalftiletile_paddevice)outscaler   )r%   
load_modelr.   r
   r/   r   r6   r)   r8   r   no_halfupcast_samplingr   ESRGAN_tileESRGAN_tile_overlapr<   enhancenparrayr   	fromarray)r0   imgr1   info	upsamplerZ	upsampledimager   r   r   
do_upscale)   s&    


zUpscalerRealESRGAN.do_upscalec                 C   sn   | j D ]T}|j|kr|jdr4tj|j| jd|_tj	|jsRt
d|j |  S qtd| d S )Nr   )	model_dirzRealESRGAN data missing: zUnable to find model info: )r&   	data_pathr)   r*   r	   load_file_from_urlmodel_download_pathosr1   existsFileNotFoundError
ValueError)r0   r1   r3   r   r   r   r>   B   s    


zUpscalerRealESRGAN.load_modelc                 C   s   t | S )N)get_realesrgan_models)r0   _r   r   r   r'   O   s    zUpscalerRealESRGAN.load_models)__name__
__module____qualname__r!   rK   r>   r'   __classcell__r   r   r4   r   r      s   r   c                    s   zddl m  ddlm tddd| fddd	td
dd| fddd	tddd| fddd	tddd|  fddd	tddd|  fddd	tddd|  fddd	g}|W S  ty   tjddd Y n0 d S )Nr   r   r   zR-ESRGAN General 4xV3zZhttps://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth   c                      s    dddddddS N   @       rZ   prelu	num_in_ch
num_out_chnum_featnum_convupscaleact_typer   r   r   r   r   <lambda>]   r   z'get_realesrgan_models.<locals>.<lambda>)r   r1   r6   upscalerr8   zR-ESRGAN General WDN 4xV3z^https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pthc                      s    dddddddS r[   r   r   r   r   r   rg   d   r   zR-ESRGAN AnimeVideozZhttps://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pthc                      s    dddddddS )Nr\   r]      rZ   r_   r`   r   r   r   r   r   rg   k   r   zR-ESRGAN 4x+zUhttps://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pthc                      s    dddddddS )Nr\   r]      r^   rZ   ra   rb   rc   	num_blocknum_grow_chr6   r   r   r   r   r   rg   r   r   zR-ESRGAN 4x+ Anime6Bz`https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pthc                      s    dddddddS )Nr\   r]      r^   rZ   rk   r   r   r   r   r   rg   y   r   zR-ESRGAN 2x+zUhttps://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth   c                      s    dddddddS )Nr\   r]   rj   r^   ro   rk   r   r   r   r   r   rg      r   z$Error making Real-ESRGAN models listTr   )r"   r   r$   r   r   r.   r
   r/   )r3   modelsr   )r   r   r   rT   S   sb    





,rT   )rP   numpyrD   PILr   r#   r   modules.upscalerr   r   Zmodules.sharedr   r   modulesr	   r
   r   rT   r   r   r   r   <module>   s   G