o
    )j&                     @   sH   d dl Z d dlZd dlZddlmZ ejdkrejj	ZG dd dZ
dS )    N   configz2.0c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )FaceLandmarkc                 C   s   |d | _ d| _tjjd | _t | _| j	 V | 
| j \| _| _t d| _t d| _t d| _| jd d d | jf | _| jd d ddf d	 | _tj| jd d dd f | _W d    d S 1 srw   Y  d S )
Nz/keypoints.pb<      ztower_0/images:0ztower_0/prediction:0ztraining_flag:0ig     V@)
model_pathmin_facecfg	KEYPOINTSZp_numkeypoint_numtfGraph_graph
as_default
init_model_sessZget_default_graphZget_tensor_by_name	img_inputZ
embeddingstraininglandmarkheadposennZsigmoidstate)selfdir r   s/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/cv/cartoon/facelib/face_landmark.py__init__   s&   

""zFaceLandmark.__init__c           	      C   s\   g }g }t |D ]\}}| |||\}}|d ur#|| || qt|t|fS )N)	enumerate_one_shot_runappendnparray)	r   ZimgZbboxesZlandmark_resultZstate_resultibboxr   r   r   r   r   __call__$   s   

zFaceLandmark.__call__c                 C   st   | j  ) tj|dd}| jj| j| j| jg| j	|| j
did\}}}W d    ||fS 1 s1w   Y  ||fS )Nr   )ZaxisF)Z	feed_dict)r   r   r"   Zexpand_dimsr   runr   r   r   r   r   )r   Zcropped_imgr   pZstatesr   r   r   
simple_run.   s   


zFaceLandmark.simple_runc              	   C   sj  |d |d  }|d |d  }|| j kr|| j krdS tt||}tj|||||tjtjjd}||7 }ddtj	j
d   | }|d |d  d |d |d  d g}	|	d |d  |d< |	d |d  |d< |	d |d  |d< |	d |d  |d< |t}||d |d |d |d d d f }
|
j\}}}t|
tj	jd tj	jd f}
|
tj}
| |
\}}|d d | j d}|d d df | tj	jd  |d d df< |d d df | tj	jd  |d d df< g }t|jd D ]-}|| }|t|d tj	jd  |d  | t|d tj	jd  |d  | g qt|tj}||fS )Nr   r      r   )NN)Z
borderTypevalue)r   )r
   intmaxcv2ZcopyMakeBorderZBORDER_CONSTANTr   ZDATAZpixel_meansr   Zbase_extend_rangeZastypeshaperesizeZinput_shaper"   Zfloat32r)   r   Zreshaperanger!   r#   )r   imager%   r$   Z
bbox_widthZbbox_heightaddZbimgZone_edgecenterZ
crop_imagehw_Z	keypointsr   resr   _indexZx_yr   r   r   r    ;   sT   (
*,,""zFaceLandmark._one_shot_runc           	         sp   t |dkrd}|d }n
d}|d  |d  fdd}dd }|r)||}n| }|d }|d }||fS )	Nr   Tr   Fc                     sT   t  } |   t  }d|j_t j|d}t j }|	| t
d | |fS )NTr   zModel restred!)r   r   r   ConfigProtogpu_optionsZallow_growthSessiontrainZimport_meta_graphZrestoreprint)graphZconfigProtosessZsaver	meta_pathZrestore_model_pathr   r   ini_ckptx   s   z)FaceLandmark.init_model.<locals>.ini_ckptc                 S   s   t  }d|j_t  }|  t j|d}t j| d}t 	 }|
|  t j|dd W d    ||fS 1 s=w   Y  ||fS )Ng?r   rb )name)r   r;   r<   Zper_process_gpu_memory_fractionr   r   r=   ZgfileZGFileZGraphDefZParseFromStringreadZimport_graph_def)r	   r   Zcompute_graphrA   ZfidZ	graph_defr   r   r   init_pb   s   
z(FaceLandmark.init_model.<locals>.init_pb)len)	r   argsZuse_pbZpb_pathrD   rI   modelr@   rA   r   rB   r   r   n   s   

zFaceLandmark.init_modelN)__name__
__module____qualname__r   r&   r)   r    r   r   r   r   r   r      s    
3r   )r/   numpyr"   Z
tensorflowr   r   r   __version__compatZv1r   r   r   r   r   <module>   s   
