o
    *j                     @   s   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	m
Z
 d dlmZmZmZmZmZ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 d d
lmZ ddlm Z  ej!dkrgej"j#Ze$  e Z%ej&ej'e
j(dG dd deZ)dS )    N)AnyDict)	Pipelines)FaceAnaget_f5pget_reference_facial_pointspadTo16xresize_sizewarp_and_crop_face)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)Tasks)
get_logger   )device_placementz2.0)module_namec                       s   e Zd Zdef fddZdd Zdedeeef fdd	Z	d
d Z
deeef deeef fddZdeeef deeef fddZ  ZS )ImageCartoonPipelinemodelc                    s   t  jdd|i| t| j| _t  " | t	j
| jdd| _| t	j
| jdd| _W d   n1 s<w   Y  d| _tt	j
| jd}tj|| j| jftjd	}t|tjtjd
 | _dS )z
        use `model` to create a image cartoon pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   zcartoon_h.pbZmodel_anime_headzcartoon_bg.pbZmodel_anime_bgNi   z	alpha.jpginterpolationg     o@ )super__init__r   r   facertfZGraph
as_default	load_sessospathjoinsess_anime_headsess_anime_bg	box_widthcv2Zimreadresize
INTER_AREAZcvtColorZCOLOR_BGR2GRAYastypenpZfloat32global_mask)selfr   kwargsr+   	__class__r   o/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/image_cartoon_pipeline.pyr   #   s*   

zImageCartoonPipeline.__init__c                 C   s   t jdd}d|j_t j|d}td|  t j|d&}t 	 }|
|  |j  t j||d |t   W d    n1 sHw   Y  td| d |S )	NT)Zallow_soft_placement)configzloading model from rb)namezload model z done.)r   ZConfigProtoZgpu_optionsZallow_growthSessionloggerinfoZgfileZ	FastGFileZGraphDefZParseFromStringreadgraphr   Zimport_graph_defrunZglobal_variables_initializer)r,   Z
model_pathr3   r1   sessfZ	graph_defr   r   r0   r   9   s   
zImageCartoonPipeline.load_sessinputreturnc                 C   s    t |}|t}d|i}|S )Nimg)r   Zconvert_to_ndarrayr)   float)r,   r<   r>   resultr   r   r0   
preprocessG   s   

zImageCartoonPipeline.preprocessc                 C   s4   |j \}}}| j|\}}}|j d dkrd S |S )Nr   )shaper   r9   )r,   r>   Zsrc_hZsrc_w_Zboxes	landmarksr   r   r0   detect_faceM   s
   z ImageCartoonPipeline.detect_facec              
   C   s  |d  tj}|j\}}}t|dd}|d d d d d d df }t|\}}}	| jj| jj	dd|id}
|
d |d |	d d f }| 
|}|d u rWtd tj|iS |D ]n}t||}t||d	td
d| j| jfd
d\}}| jj| jj	dd|d d d d d d df id}tj||t|dt|dfdd}| j}tj||t|dt|dfdd}t|d}|| d| |  }qYtj|||ftjd}tj|iS )Nr>   i  )sizezmodel_anime_bg/output_image:0zmodel_anime_bg/input_image:0)Z	feed_dictzNo face detected!g      ?T)Zdefault_square)ratioZreference_ptsZ	crop_sizeZreturn_trans_invzmodel_anime_head/output_image:0zmodel_anime_head/input_image:0   r   )r   r   r   )ZborderValue   r   )r)   r*   Zuint8rB   r	   r   r$   r9   r8   Zget_tensor_by_namerE   printr   Z
OUTPUT_IMGr   r
   r   r%   r#   r&   Z
warpAffinerF   r+   Zexpand_dimsr'   r(   )r,   r<   r>   Zori_hZori_wrC   Zimg_brgZpad_bgZpad_hZpad_wZbg_resresrD   ZlandmarkZf5pZhead_imgZ	trans_invZhead_resZhead_trans_invmaskZmask_trans_invr   r   r0   forwardU   s`   




	
zImageCartoonPipeline.forwardinputsc                 C   s   |S )Nr   )r,   rO   r   r   r0   postprocess   s   z ImageCartoonPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r   rA   rE   rN   rP   __classcell__r   r   r.   r0   r      s    "*>r   )*r    typingr   r   r&   numpyr*   Z
tensorflowr   Zmodelscope.metainfor   Zmodelscope.models.cv.cartoonr   r   r   r   r	   r
   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   Zutils.devicer   __version__compatZv1Zdisable_eager_executionr5   Zregister_moduleZimage_portrait_stylizationZperson_image_cartoonr   r   r   r   r0   <module>   s.    
