o
    ॵi/
                     @   s   d dl m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 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
jdkrWe
jjZ
e Z ej!ej"ej"d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_loggerz2.0)module_namec                       s   e Zd Zdef fddZ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 )ImageMattingPipelinemodelc              
      s8  t  jdd|i| t| jtj}t| j| j	x t
jdd}d|j_t
j|d| _| j I td|  t
j|d%}t
 }||  t
j|dd | jjd	| _d
| _W d   n1 siw   Y  td W d   n1 s}w   Y  W d   dS W d   dS 1 sw   Y  dS )z
        use `model` to create a image matting pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   T)allow_soft_placement)configzloading model from rb )namezoutput_png:0zinput_image:0Nzload model done )super__init__ospjoinr   r
   TF_GRAPH_FILEr   	frameworkdevice_nametfConfigProtogpu_optionsallow_growthSession_session
as_defaultloggerinfogfile	FastGFileGraphDefParseFromStringreadimport_graph_defgraphget_tensor_by_nameoutput
input_name)selfr   kwargs
model_pathr   f	graph_def	__class__r   b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/image_matting_pipeline.pyr      s,   "zImageMattingPipeline.__init__inputreturnc                 C   s    t |}|t}d|i}|S )Nimg)r	   convert_to_ndarrayastypefloat)r1   r9   r;   resultr   r   r8   
preprocess6   s   

zImageMattingPipeline.preprocessc                 C   sf   | j  $ | j|d i}| j j| j|d}t|tj}tj	|iW  d    S 1 s,w   Y  d S )Nr;   )	feed_dict)
r#   r$   r0   runr/   cv2cvtColorCOLOR_RGBA2BGRAr   
OUTPUT_IMG)r1   r9   rA   
output_imgr   r   r8   forward<   s   $zImageMattingPipeline.forwardinputsc                 C   s   |S )Nr   )r1   rI   r   r   r8   postprocessC   s   z ImageMattingPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r@   rH   rJ   __classcell__r   r   r6   r8   r      s
    "*r   )%os.pathpathr   typingr   r   rC   numpynp
tensorflowr   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr	   modelscope.utils.constantr
   r   modelscope.utils.devicer   modelscope.utils.loggerr   __version__compatv1r%   register_moduleportrait_mattinguniversal_mattingr   r   r   r   r8   <module>   s.   
