o
    *j/                     @   sH  d dl Z d dlmZm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  mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlT d dlmZ d d	lmZ d d
lmZm 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, e	j-dkre	j.j/Z	e	0  e, Z1e"j2e'j3ej3dG dd de Z4dS )    N)AnyDict)	Pipelines)DetectionUNet)RetouchingNet)UNet)*)
OutputKeys)pipeline)InputPipeline)	PIPELINES)	LoadImage)	ModelFileTasks)create_devicedevice_placement)
get_loggerz2.0)module_namec                       s   e Zd Zdedef fddZdedeeef fddZdeeef deeef fd	d
Z	dd Z
			dddZdeeef deeef fddZ  ZS )SkinRetouchingPipelinemodeldevicec              	      s\  t  j||d t| j}tj| jtj	}tj| jd}tj| jtj
}tdd|| _| jtj|dddd  | j  d}ttj|d	| _| jj| || _tj| jddd}td
dd|| _tddd|| _| j|d  | j|d  | j  | j  d| _|| _| jdurt| j| jU tj dd}d|j!_"d|j!_#tj$|d| _%tj&'| jd(}	t( }
|
)|	*  | j%j+,  tj-|
dd | j%.t/  W d   n1 sw   Y  W d   n1 sw   Y  t01t02 t03ddg| _4t5 | _6t7| j6|8 | _6| j69dddd | _6d| _:|| _;dS )z
        use `model` to create a skin retouching pipeline for prediction
        Args:
            model: model id on modelscope hub.
        )r   r   zjoint_20210926.pth   cpuT)Zmap_locationZweights_only	generatorz*damo/cv_resnet50_face-detection_retinaface)r      )Zin_channelsZout_channels   )Z
n_channelsZ	n_classesinpainting_netdetection_neti   N)Zallow_soft_placementg333333?)configrb )name)      ?r#   r#      r   )N.)<super__init__r   Zdevice_nameospathjoinr   r   ZTORCH_MODEL_FILEZTF_GRAPH_FILEr   tor   Zload_state_dicttorchloadevalr
   r   Zface_detectiondetectorlocal_model_pathr   r   r   r   
patch_sizeskin_model_pathr   Z	frameworktfZConfigProtoZgpu_optionsZper_process_gpu_memory_fractionZallow_growthSessionsessZgfileZ	FastGFileZGraphDefZParseFromStringreadgraphZ
as_defaultZimport_graph_defrunZglobal_variables_initializer
transformsZComposeZToTensorZ	NormalizeZimage_files_transformsZgen_diffuse_maskdiffuse_mask
from_numpyfloatpermute
input_sizer   )selfr   r   Z
model_pathr/   r1   Zdet_model_idZckpt_dict_loadr   fZ	graph_def	__class__ q/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/skin_retouching_pipeline.pyr&   (   s   







zSkinRetouchingPipeline.__init__inputreturnc                 C   s<   t |}t|jdkrt|tj}|t}d|i}|S )Nr$   img)	r   Zconvert_to_ndarraylenshapecv2ZcvtColorZCOLOR_GRAY2BGRastyper;   )r>   rD   rF   resultrB   rB   rC   
preprocessj   s   

z!SkinRetouchingPipeline.preprocessc                 C   s  |d  tj}d}d}d}d}d}t ] |r9|dkr9| jd ur9t|d\}}	| jj| jj	
dd	|id
}
t|| j}|rWtj|jd |jd dftjdd }| |}g }tt|d D ]7}i }t|d |  tj |d< |d | |d< t|d |  tjdd |d< || qft|}t|}|dkr|  d d d d d d df |d}|W  d    S d}|D ]j}t||\}}}t|}|jd d|jd  krd}|| j}t|}|r| jd ur|  |}| j!||d|d}|d }|||d |d |d |d f< |r6|d }|||d |d |d |d f< q|rM|dkrM| jd urMt"||
||d}t#|tj$sZ|  }|d d d d d d df }t%j&|iW  d    S 1 sxw   Y  d S )NrF   T      ?g?Fr   i   zoutput_png:0zinput_image:0)Z	feed_dictr   r   )Zdtyper#   ZscoresZboxesbboxZscoreZ	keypoints   r$   Z	landmarks)predface_numg?)degreesmooth_border	return_mgrQ   pred_mg)flag_bigKernal)'rJ   npZuint8r+   no_gradr1   Zresize_on_long_sider4   r7   r6   Zget_tensor_by_namer:   r*   r   ZonesrH   Zfloat32r.   rangerG   arrayZint32tolistZreshapeappendZget_crop_bboxr   numpyZget_roi_without_paddingZroi_to_tensorZpreprocess_roir/   retouch_localpredict_roiZ
whiten_img
isinstanceZndarrayr	   Z
OUTPUT_IMG)r>   rD   Z	rgb_imager_   Z	whiteningrS   Zwhitening_degreerU   Zrgb_image_smallZresize_scaleZ	skin_maskZoutput_predZ	output_mgZdet_resultsresultsiZ	info_dictZcrop_bboxesrR   outputrW   rN   roiexpandZ	crop_tblrZ
roi_outputZroi_predZroi_mgrB   rB   rC   forwardr   s   


"%

&zSkinRetouchingPipeline.forwardc              	   C   s4  t  
 |jdd \}}tj|dddd}t | |}tj|||fdd}|d	k }|d
k }|d|  | }|| }d| }|| j dkrO|n	|| j d | j }|| j dkrb|n	|| j d | j }	tj	|d|	| d|| ddfddd}
tj	|d|	| d|| ddfddd}t
|
| j| jd}
t
|| j| jd}|
 \}}}}g }t|D ])}|
||d  }|||d  }|| }| ||}|d| |  }|| qt j|dd}t|tt|| j tt|	| j dddddd|d|f }|W  d   S 1 sw   Y  dS )z
        image: rgb
        r$   N)   rh   bilinearT)sizemodeZalign_cornersnearest)rj   rk   gffffff?r#   r   r   Zconstant)padrk   value)p1Zp2)dim)hw)r+   rY   rH   FinterpolateZsigmoidr   r;   r0   rm   Zpatch_partition_overlaprj   rZ   r   r]   catZpatch_aggregation_overlapintround)r>   imageZsub_HZsub_WZsub_image_standardZsub_mask_predZsub_mask_pred_hard_lowZsub_mask_pred_hard_highZsub_H_standardZsub_W_standardZsub_image_paddingZsub_mask_pred_paddingZ	B_paddingZ	C_padding_Zsub_comp_padding_listZwindow_itemZsub_image_padding_windowZsub_mask_pred_padding_windowZsub_input_image_padding_windowZsub_output_padding_windowZsub_comp_padding_windowZsub_comp_paddingZsub_comprB   rB   rC   r_      s   




&z$SkinRetouchingPipeline.retouch_localrM   Fc           	      C   s"  t   tj|| j| jfdd}| |}|d | d }|dd}tj||jdd  dd}|d ddd}t	|jdkrE|d	 }|rMt
| j|}|d dddd d }dd|  | | d| |  }|d
  }d|i}|r~|  |d< |W  d    S 1 sw   Y  d S )Nri   )rk   r#   g        rM   r$   r   r   ).Ng     o@rQ   rV   )r+   rY   rs   rt   r=   r   clamprH   r<   rG   Zsmooth_border_mgr9   byter   r^   )	r>   re   rS   rT   rU   rx   rV   rQ   rd   rB   rB   rC   r`     s:   



$z"SkinRetouchingPipeline.predict_roiinputsc                 C   s   |S )NrB   )r>   r|   rB   rB   rC   postprocess6  s   z"SkinRetouchingPipeline.postprocess)rM   FF)__name__
__module____qualname__strr&   r   r   r   rL   rg   r_   r`   r}   __classcell__rB   rB   r@   rC   r   $   s    B"[J
*!r   )5r'   typingr   r   rI   r^   rX   ZPILZ
tensorflowr2   r+   Ztorch.nn.functionalnnZ
functionalrs   Ztorchvision.transformsr8   Zmodelscope.metainfor   ZFmodelscope.models.cv.skin_retouching.detection_model.detection_unet_inr   ZEmodelscope.models.cv.skin_retouching.inpainting_model.inpainting_unetr   Z0modelscope.models.cv.skin_retouching.unet_deployr   Z*modelscope.models.cv.skin_retouching.utilsZmodelscope.outputsr	   Zmodelscope.pipelinesr
   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.devicer   r   Zmodelscope.utils.loggerr   __version__compatZv1Zdisable_eager_executionloggerZregister_moduleZskin_retouchingr   rB   rB   rB   rC   <module>   s<   
