o
    *jT                     @   s  d dl Z d dlZ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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mZ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&m'Z' d d	l(m)Z) d d
l*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z;m<Z< d dl=m>Z> zd dl?m@Z@ W n eAy   d dl?mBZ@ Y nw ejCdkrejDjEZeF  e> ZGe2jHe9jIejIdG dd de0ZJdS )    N)AnyDict)loadmatsavemat)	Pipelines)LargeBaseLmkInfer)POSalign_for_lm	draw_lineenlarged_bbox
extract_5pimage_warp_grid1	load_lm3dmesh_to_stringread_objresize_n_crop_imgresize_on_long_sidespread_flow	write_obj)HeadSegmentor)TexProcesser)Model)
OutputKeys)pipeline)InputPipeline)	PIPELINES)	LoadImage)Config)	ModelFileTasks)create_devicedevice_placement)
get_logger)get_dir)_get_torch_homez2.0)module_namec                       s   e Zd Zd!dedef fddZdedeeef fdd	Z	
		d"ddZ				
	
	d#ddZ
d$ddZdd Zdd Zd%ddZdeeef deeef fddZdeeef fdd Z  ZS )&HeadReconstructionPipelineFmodeldevicec              
      s  t  j||d |}tj|d}tj|tj}tj|tj}t	d|  t
|| _|| _d|v r:d| _n|| _| j | _tj|d}t|| jdk| _td| jd| _d	}	tjtjtj||	d
dd}
| j|
 | j  t| j}| j| | j| | j  | j  | j  t  }tj|d}tj!|st"| t#$tj|dd| t#$tj|dd| t#$tj|dd| t%j&t%j'j(dd| _)t*j+dd}d|j,_-d|j,_.t*/ }t*j0||d| _1| j12 Z |2 E t*j34tj|dd(}t*5 }|6|7  | j1j82  t*j9|dd | j1:t*;  W d   n	1 s;w   Y  W d   n	1 sKw   Y  W d   n	1 s[w   Y  t<|d| _=t>|d| _?t@|| _AtBdC|| _DtE| jF}|| _dS )aa  The inference pipeline for head reconstruction task.

        Args:
            model (`str` or `Model` or module instance): A model instance or a model local dir
                or a model id in the model hub.
            device ('str'): device str, should be either cpu, cuda, gpu, gpu:X or cuda:X.

        Example:
            >>> from modelscope.pipelines import pipeline
            >>> test_image = 'data/test/images/face_reconstruction.jpg'
            >>> pipeline_headRecon = pipeline('head-reconstruction',
                model='damo/cv_HRN_head-reconstruction')
            >>> result = pipeline_headRecon(test_image)
            >>> mesh = result[OutputKeys.OUTPUT]['mesh']
            >>> texture_map = result[OutputKeys.OUTPUT_IMG]
            >>> mesh['texture_map'] = texture_map
            >>> write_obj('head_reconstruction.obj', mesh)
        )r(   r)   Zassetszloading config from Zgpucudazlarge_base_net.pthi   )max_sizer)   z,retinaface_resnet50_2020-07-20_old_torch.pthcpuT)Zmap_locationZweights_onlyZcheckpointsface_alignmentzs3fd-619a316812.pthz3DFAN4-4a694010b9.zipzdepth-6c4283c0e0.zipF)Z
flip_input)Zallow_soft_placementg?)graphconfigzsegment_face.pbrb )nameN)
model_rootz {}/assets/BBRegressorParam_r.mat)Gsuper__init__ospathjoinr   ZTORCH_MODEL_FILEZCONFIGURATIONloggerinfor   	from_filecfghair_texdevice_name_lowerr   Zmodel_preloadlarge_base_lmks_modelr   detectortorchloaddirnameZload_state_dictevalr)   r(   Z
set_devicesetupZparallelizeZ
set_renderr$   existsmakedirsshutilcopyr-   ZFaceAlignmentZLandmarksTypeZTHREE_Dlm_sesstfZConfigProtoZgpu_optionsZper_process_gpu_memory_fractionZallow_growthZGraphSession	face_sessZ
as_defaultZgfileZ	FastGFileZGraphDefZParseFromStringreadr.   Zimport_graph_defrunZglobal_variables_initializerr   head_segmentorr   tex_processorr   lm3d_stdr   formatalign_paramsr!   Zdevice_name)selfr(   r)   r=   r3   Z
bfm_folderZcheckpoint_pathZconfig_pathZlmks_cpkt_pathZdetector_ckpt_nameZ
state_dictZhub_dirZsave_ckpt_dirr/   g1fZ	graph_def	__class__ u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/head_reconstruction_pipeline.pyr5   6   s   










z#HeadReconstructionPipeline.__init__inputreturnc                 C   sR   t |trt|}t|jdkrt|tj}|	t
}n|	t
}d|i}|S )N   img)
isinstancestrr   Zconvert_to_ndarraylenshapecv2cvtColorZCOLOR_GRAY2BGRastypefloat)rV   r]   r`   resultr[   r[   r\   
preprocess   s   


z%HeadReconstructionPipeline.preprocessN      l@     Y@c                 C   s   |j \}}|jd dkrt|}	n|}	t|	 | \}
}|| }t|||
|||d\}}}t||||
d d |
d d g}||||fS )ax  
        Return:
            transparams        --numpy.array  (raw_W, raw_H, scale, tx, ty)
            img_new            --PIL.Image  (target_size, target_size, 3)
            lm_new             --numpy.array  (68, 2), y direction is opposite to v direction
            mask_new           --PIL.Image  (target_size, target_size)

        Parameters:
            img                --PIL.Image  (raw_H, raw_W, 3)
            lm                 --numpy.array  (68, 2), y direction is opposite to v direction
            lm3D               --numpy.array  (5, 3)
            mask               --PIL.Image  (raw_H, raw_W, 3)
        r      )target_sizemask   )sizerd   r   r   Z	transposer   nparray)rV   r`   lmZlm3Dro   rn   rescale_factorZw0Zh0Zlm5ptsZimg_newZlm_newZmask_newZtrans_paramsr[   r[   r\   	align_img   s   

$z$HeadReconstructionPipeline.align_imgT        R@c	                 C   s  t j|dd d df }	|	j\}
}|d |d d df  |d d df< t j|}t j|dd d df }| |	||\}}}}| j|	||||d\}}}}| j|||||| d d\}}}}| jj| jjddt	
|id	}|rtjt	
|d
 tjddddd}tjt	
|d
 tjddddd}tjt	
|d
 tjdd d d d d d f }|d urtjt	
|d
 tjdd d d d d d f nd }t|d}t|d}tjt	
|d
 tjddddd}t|d}||||||||fS )N.rp   )ro   ru      )rn   ru   zoutput_alpha:0zinput_image:0)Z	feed_dict     o@)dtyper_   r   )PILZImageZ	fromarrayrq   rx   rN   rP   r.   Zget_tensor_by_namerr   rs   rB   Ztensorfloat32ZpermuteZ	unsqueeze)rV   r`   rt   rS   Z	to_tensorZ	image_resimg_fat	head_maskru   ZimWHZim_fat_Zim_lr_coeffZlm_lr_coeffZim_lrZlm_lrZmask_lr_headZim_hdZlm_hdZmask_lrr[   r[   r\   	read_data   st   

$

z$HeadReconstructionPipeline.read_datac                 C   s   t ||| j\}}}|dkrd S t|g dtj}|dd d d d d d df }||d }|d d d df | }|d d df |d  |d d df< |d d df |d  |d d df< |S )Nr   )rp   r|   r|      r{   r_   rp   )r	   rU   rr   Zreshaperg   r   Zget_landmarks_from_image)rV   r`   rK   five_pointsZ	input_imgscalebboxlandmarkr[   r[   r\   prepare_data   s   
$$z'HeadReconstructionPipeline.prepare_datac           !   
   C   s  d}d}g }t |t j}| j|}g }|D ]$}|d dkr! n||d d |d d |d d |d d	 d
 q|D ]}	|	d }
|	d }|	d }|	d }||
 d }|| d }||
 d }|| d }t||| }||d  }
||d  }|
}|}|
| }|| }|j\}}}td|
 }td| }td|
}
td|}td|| }td|| }t||}t||}|t	|t	|t	|
t	|f }|dks|dks|dks|dkrt j
|t	|t	|t	|t	|t jdd}t |||f}t|| j| jdk}|| }td}tdD ]'} |d | d d  | | ||  d< |d | d d  | | ||  d< qt|d d df }
t|d d df }t|d d df }t|d d df }||
 d }|| d }||
 d }|| d }t||| }||d  }
||d  }|
}|}|
| }|| }|j\}}}td|
 }td| }td|
}
td|}td|| }td|| }t||}t||}|t	|t	|t	|
t	|f }|dks|dks|dks|dkrt j
|t	|t	|t	|t	|t jdd}t |||f}t|| j| j dk}|| }td}tdD ]'} |d | d d  | | ||  d< |d | d d  | | ||  d< q/|| q>||fS )Nr|   g?Zscorer{   r   r   rp   r_   r   )x1y1x2y2r   r   r   r   )g\(Y@gR1]@gQ^@)valuer*   )j   r_   r   )re   rf   ZCOLOR_BGR2RGBrA   Zpredict_jsonsappendmaxrd   minintZcopyMakeBorderZBORDER_CONSTANTresizer   Z	infer_imgr@   r>   rr   zerosranger?   )!rV   Zimg_bgrZ
INPUT_SIZEZENLARGE_RATIO	landmarks	rgb_imageresultsboxesannoZdetect_resultr   r   r   r   whZcxcyszZtrans_x1Ztrans_y1heightwidthr   ZdxZdyZedxZedyZcrop_imgZ	base_lmksZ	inv_scaleZaffine_base_lmksidxr[   r[   r\   
infer_lmks  s   









$ 




$(
z%HeadReconstructionPipeline.infer_lmksc              	   C   sL  |  |\}}t|}g dg dg dg dg dg dg dg dg}g }tt|D ]/}t|| d	 || d
 || d || d g|jd |jd d}dd |D }|| q.g }t|jd D ]}||d d d d f }	g }
t|jd |jd ftj	}|| }|d |d  }|d |d  }||k r|n|}|d }|dkrd}|d }|d dkr|n|d }t
|D ]L\}}t|jd |jd ftj	}t||	|d |d  d||d  t|||fd}|dkrt||	|d |d  d|d |d  |
| qt|||fd}|
| ||
 qg|d |fS )N)r   !   F)r   &   F)*   /   F)3   7   F)9   @   F)B   J   T)K   S   T)T   `   Tr   r   r   r   rp   r   g      ?c                 S   s   g | ]}t |qS r[   )r   ).0xr[   r[   r\   
<listcomp>  s    z@HeadReconstructionPipeline.find_face_contour.<locals>.<listcomp>r_   r   
   )   r   r   )r   rr   rs   r   rc   r   rd   r   r   uint8	enumerater
   re   ZGaussianBlur)rV   imager   r   argsZ	roi_bboxsiZroi_bboxZpeople_mapsr   mapsZ
whole_maskZroi_boxZroi_box_widthZroi_box_heightZshort_side_lengthZ
line_widthZkernel_sizeZgaussian_kernelrv   argro   r[   r[   r\   find_face_contour  sb   
(

z,HeadReconstructionPipeline.find_face_contour{Gz?c                 C   s  t |d\}}| |\}}|d }|d }tj|jd |jd dftjd}|d |d  d |d |d	  d g}	tt|d |d	  t|d |d  }
d|jd |	d  d  }d|jd |	d  d  }|
d d|	d d  d|	d d  ||g}t|}t	|}t
d
|| }t|||f}||t	|	d |d  t	|	d |d  t	|	d |d  t	|	d |d  f< |t||f d }|d }|dt||fd   }t||jd |jd f}|| }t|d |d |dg d\}}}}}|S )Ni   r   rp   r_   )rd   r~   r   r   r   r   d   r}   r{   g      ?).r   ).rp   )r   r   r   r   )r   r   rr   r   rd   r   r   absr   r   r   re   r   Zdstackr   )rV   r`   ZdegreeZ_imgr   Zcontour_mapsr   Zcontour_mapZFlowZ
box_centerZ
box_lengthZvalue_1Zvalue_2Z
value_listZflow_box_lengthZsfZinter_face_mapspredZ	top_boundZbottom_boundZ
left_boundZright_boundr[   r[   r\   fat_face  sh   $"
z#HeadReconstructionPipeline.fat_facec              	   C   s  |d    tj}t|tj}|}|jd dks#|jd dkr*t	|d\}}| 
|\}}|d u s?t|jd dkrAi S | |}g }	|d }dD ]}
|	||
 d ||
 d g qNt|	}	| j|| j|	d}	| j|d }| j||	| j||d\}}}}}}}}||||||||d	}| j| |  }|d usJ |d
 tj}| jj|| jd}|d |d d |d |d |d |d}d|i}tjd tj|tj|iS )Nr`   r   i  rp   i  )r   r   6   r   Z   )r   )r   r   )ZimgsZimgs_hdZlmsZlms_hdZ	face_maskr   Z
imgs_coeffZ	lms_coefftex_map)r=   verticesZ	trianglesZuvsfaces_uvnormals)r   ZfacesZUVsr   r   texture_mapmesh)r,   numpyrg   rr   r   re   rf   ZCOLOR_RGB2BGRrd   r   r   rs   r   r   r   rK   rQ   processr   rS   r(   Z	set_inputr   rR   Zpost_process_texturer=   r   
OUTPUT_OBJ
OUTPUT_IMGOUTPUT)rV   r]   r   Z	bgr_imager`   r   boxr   Zfatbgrr   r   r   Z	im_tensorZ	lm_tensorZim_hd_tensorZlm_hd_tensorro   Zim_coZlm_codataoutputr   Z	head_meshr[   r[   r\   forward
  s`   
 



z"HeadReconstructionPipeline.forwardc           
      K   s   | dd}|tj }|tj }|tj }|r,t }t|d }|jdd}|	| tj|tj|tj|r;d i}	|	S |i}	|	S )NrenderFr   zutf-8)encoding)
getr   r   r   r   ioBytesIOr   encodewrite)
rV   Zinputskwargsr   Z
output_objr   r   Zmesh_strZ
mesh_bytesri   r[   r[   r\   postprocessO  s"   




z&HeadReconstructionPipeline.postprocess)F)Nrk   rl   )Try   NNrz   )N)r   )__name__
__module____qualname__rb   r5   r   r   r   rj   rx   r   r   r   r   r   r   r   __classcell__r[   r[   rY   r\   r'   2   s(    c
*

4 

8"7Er'   )Kr   r6   rI   typingr   r   re   r-   r   rr   Z	PIL.Imager   Z
tensorflowrL   rB   Zscipy.ior   r   Zmodelscope.metainfor   ZRmodelscope.models.cv.face_reconstruction.models.facelandmark.large_base_lmks_inferr   Z.modelscope.models.cv.face_reconstruction.utilsr   r	   r
   r   r   r   r   r   r   r   r   r   r   ZAmodelscope.models.cv.head_reconstruction.models.head_segmentationr   Z=modelscope.models.cv.head_reconstruction.models.tex_processorr   Z>modelscope.models.cv.skin_retouching.retinaface.predict_singler   Zmodelscope.outputsr   Zmodelscope.pipelinesr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r    Zmodelscope.utils.devicer!   r"   Zmodelscope.utils.loggerr#   Z	torch.hubr$   BaseExceptionr%   __version__compatZv1Zdisable_eager_executionr9   Zregister_moduleZhead_reconstructionr'   r[   r[   r[   r\   <module>   sN   <
