o
    *j                     @   s   d dl mZ d dlmZmZ d dlZd dlZ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 d d
lmZ e Zejeje
jdG dd deZdS )    N)AnyDict)	Pipelines)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)	ModelFileTasks)device_placement)
get_logger)module_namec                	       s   e Zd Zdef fddZdd Z	dde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eeef d	eeef fddZ  ZS )ImageStyleTransferPipelinemodelc              
      s  t  jdd|i| ddl}|jdkr|jj}t| jt	j
}t| j| j |jdd}d|j_|j|d| _d| _| j n td	|  |j|d
J}| }||  |j|dd | d| _| d| _ | d| _!| d| _"| d| _#| d| _$W d   n1 sw   Y  td W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z
        use `model` to create a style transfer pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   r   Nz2.0T)Zallow_soft_placement)configi   zloading model from rb )namez	content:0zstyle:0zstylized_output:0zattention_map:0zinter_weight:0zcentroids:0zload model done )%super__init__Z
tensorflow__version__compatZv1ospjoinr   r
   ZTF_GRAPH_FILEr   Z	frameworkZdevice_nameZConfigProtoZgpu_optionsZallow_growthSession_session
max_length
as_defaultloggerinfoZgfileZ	FastGFileZGraphDefZParseFromStringreadZimport_graph_defZget_default_graphZget_tensor_by_namecontentstyleoutputZ	attentioninter_weightZ	centroids)selfr   kwargstfZ
model_pathr   fZ	graph_def	__class__r   v/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/image_style_transfer_pipeline.pyr      sP   
"z#ImageStyleTransferPipeline.__init__c                 K   s
   |i i fS Nr   )r'   Zpipeline_parametersr   r   r-   _sanitize_parameters?   s   
z/ImageStyleTransferPipeline._sanitize_parametersNr#   r$   returnc                 C   s   t |tu r|d }|d }t|}t|jdkr!t|tj}|	t
}t|}t|jdkr9t|tj}|	t
}||d}|S )Nr$   r#      )r#   r$   )typedictr	   Zconvert_to_ndarraylenshapecv2cvtColorZCOLOR_GRAY2BGRastypefloat)r'   r#   r$   Zcontent_imgZ	style_imgresultr   r   r-   
preprocessB   s   




z%ImageStyleTransferPipeline.preprocessinputc                 C   s$  |d |d }}t |d }t |d }|| jks!|| jkrF||kr6t|t| j| | | jf}nt|| jt| j| | f}| j = | j|| j	|| j
di}| jj| j|d}t|d tj}t |ddt j}t|||f}tj|iW  d    S 1 sw   Y  d S )Nr#   r$   r      g      ?)	feed_dict   )npr5   r   r6   resizeintr   r   r#   r$   r&   runr%   r7   ZCOLOR_RGB2BGRZclipr8   Zuint8r   Z
OUTPUT_IMG)r'   r<   Zcontent_feedZ
style_feedhwr>   Z
output_imgr   r   r-   forwardV   s0   $z"ImageStyleTransferPipeline.forwardinputsc                 C   s   |S r.   r   )r'   rG   r   r   r-   postprocesst   s   z&ImageStyleTransferPipeline.postprocessr.   )__name__
__module____qualname__strr   r/   r   r   r   r;   rF   rH   __classcell__r   r   r+   r-   r      s    &

"*r   )Zos.pathpathr   typingr   r   r6   numpyr@   Zmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr	   Zmodelscope.utils.constantr
   r   Zmodelscope.utils.devicer   Zmodelscope.utils.loggerr   r    Zregister_moduleZimage_style_transferr   r   r   r   r-   <module>   s"   