o
    ॵi                     @   s   d dl mZmZmZ d dl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mZ d dlmZ d dl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 )    )AnyDictUnionN)	Pipelines)
OutputKeys)InputModelPipeline)	PIPELINES)
load_image)Tasks)
get_logger)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 )!ImageSemanticSegmentationPipelinemodelc                    s$   t  jdd|i| td dS )z
        use `model` to create a image semantic segmentation pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   z*semantic segmentation model, pipeline initN )super__init__loggerinfo)selfr   kwargs	__class__r   p/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/image_semantic_segmentation_pipeline.pyr      s   z*ImageSemanticSegmentationPipeline.__init__inputreturnc           
      C   s  ddl m} ddlm}m} ddlm} | jj}t	|t
r;d|jjjd _tt|}|d d d d d d df }nJt	|tjjr\d|jjjd _t|d d d d d d df }n)t	|tjr|d|jjjd _t|jdkryt|tj}n|}n	tdt| t|d	}||jjj|jj_||jjj}	|	|}||gd
d}dd |d D |d< dd |d D |d< t| j jr||t| j jgd }|S )Nr   )Compose)collatescatter)replace_ImageToTensorLoadImageFromWebcam   z9input should be either str, PIL.Image, np.array, but got )img   )samples_per_gpuc                 S      g | ]}|j d  qS r   data).0	img_metasr   r   r   
<listcomp>D   s    
z@ImageSemanticSegmentationPipeline.preprocess.<locals>.<listcomp>r,   c                 S   r'   r(   r)   )r+   r$   r   r   r   r-   G   s    r$   ) mmdet.datasets.pipelinesr   mmcv.parallelr   r   mmdet.datasetsr    r   cfg
isinstancestrr*   testpipelinetypenparrayr   PILImagendarraylenshapecv2cvtColorCOLOR_GRAY2BGR	TypeErrordictnext
parametersis_cudadevice)
r   r   r   r   r   r    r1   r$   r*   test_pipeliner   r   r   
preprocess#   s>   
$

z,ImageSemanticSegmentationPipeline.preprocessc                 C   s   | j |}|S N)r   	inference)r   r   resultsr   r   r   forwardN   s   z)ImageSemanticSegmentationPipeline.forwardinputsc                 C   s8   | j |}tj|tj tj|tj tj|tj i}|S rI   )r   postprocessr   MASKSLABELSSCORES)r   rM   rK   outputsr   r   r   rN   R   s   z-ImageSemanticSegmentationPipeline.postprocess)__name__
__module____qualname__r3   r   r   r   r   rH   rL   rN   __classcell__r   r   r   r   r      s
    
"+*r   )typingr   r   r   r>   numpyr7   r9   torchmodelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   r   r	   modelscope.pipelines.builderr
   modelscope.preprocessorsr   modelscope.utils.constantr   modelscope.utils.loggerr   r   register_moduleimage_segmentationimage_semantic_segmentationr   r   r   r   r   <module>   s$   