o
    ॵi                     @   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.pbmodel_anime_headzcartoon_bg.pbmodel_anime_bgNi   z	alpha.jpginterpolationg     o@ )super__init__r   r   facertfGraph
as_default	load_sessospathjoinsess_anime_headsess_anime_bg	box_widthcv2imreadresize
INTER_AREAcvtColorCOLOR_BGR2GRAYastypenpfloat32global_mask)selfr   kwargsr2   	__class__r   b/home/ubuntu/.local/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)allow_soft_placement)configzloading model from rb)namezload model z done.)r   ConfigProtogpu_optionsallow_growthSessionloggerinfogfile	FastGFileGraphDefParseFromStringreadgraphr!   import_graph_defrunglobal_variables_initializer)r3   
model_pathr;   r9   sessf	graph_defr   r   r7   r"   9   s   
zImageCartoonPipeline.load_sessinputreturnc                 C   s    t |}|t}d|i}|S )Nimg)r   convert_to_ndarrayr/   float)r3   rO   rQ   resultr   r   r7   
preprocessG   s   

zImageCartoonPipeline.preprocessc                 C   s4   |j \}}}| j|\}}}|j d dkrd S |S )Nr   )shaper   rI   )r3   rQ   src_hsrc_w_boxes	landmarksr   r   r7   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 )NrQ   i  )sizezmodel_anime_bg/output_image:0zmodel_anime_bg/input_image:0)	feed_dictzNo face detected!g      ?T)default_square)ratioreference_pts	crop_sizereturn_trans_invzmodel_anime_head/output_image:0zmodel_anime_head/input_image:0   r   )r   r   r   )borderValue   r   )r/   r0   uint8rV   r	   r   r'   rI   rG   get_tensor_by_namer\   printr   
OUTPUT_IMGr   r
   r   r(   r&   r)   
warpAffiner]   r2   expand_dimsr+   r,   )r3   rO   rQ   ori_hori_wrY   img_brgpad_bgpad_hpad_wbg_resresr[   landmarkf5phead_img	trans_invhead_reshead_trans_invmaskmask_trans_invr   r   r7   forwardU   s`   




	
zImageCartoonPipeline.forwardinputsc                 C   s   |S )Nr   )r3   r   r   r   r7   postprocess   s   z ImageCartoonPipeline.postprocess)__name__
__module____qualname__strr   r"   r   r   r   rU   r\   r~   r   __classcell__r   r   r5   r7   r      s    "*>r   )*r#   typingr   r   r)   numpyr0   
tensorflowr   modelscope.metainfor   modelscope.models.cv.cartoonr   r   r   r   r	   r
   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   modelscope.utils.loggerr   utils.devicer   __version__compatv1disable_eager_executionr@   register_moduleimage_portrait_stylizationperson_image_cartoonr   r   r   r   r7   <module>   s.    
