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 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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 )!ImagePanopticSegmentationPipelinemodelc                    s$   t  jdd|i| td dS )z
        use `model` to create a image panoptic segmentation pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   z*panoptic 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_panoptic_segmentation_pipeline.pyr      s   z*ImagePanopticSegmentationPipeline.__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 }nAt	|tjjrSd|jjjd _t|d}n)t	|tjrsd|jjjd _t|jdkrpt|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RGB   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>?   s    
z@ImagePanopticSegmentationPipeline.preprocess.<locals>.<listcomp>r,   c                 S   r'   r(   r)   )r+   r$   r   r   r   r-   B   s    r$   )!mmdet.datasets.pipelinesr   mmcv.parallelr   r   mmdet.datasetsr   r   cfg
isinstancestrr*   testpipelinetypenparrayr
   PILImageconvert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,ImagePanopticSegmentationPipeline.preprocessc                 C   s   | j |}|S )N)r   	inference)r   r   resultsr   r   r   forwardH   s   z)ImagePanopticSegmentationPipeline.forwardinputsc           
   	      s   |d d }d t |d d d }|| jjk}|| }t j fdd|D t jd}|d  |d d d d f k}dd |D }t | jj|  }tj	|tj
|tjd	d tt|D i}	|	S )
Nr   pan_resultsi  r!   c                    s   g | ]}|  qS r   r   )r+   idINSTANCE_OFFSETr   r   r-   S   s    zAImagePanopticSegmentationPipeline.postprocess.<locals>.<listcomp>)dtypec                 S   s   g | ]}| tjqS r   )astyper7   int32)r+   itr   r   r   r-   U   s    c                 S   s   g | ]}d qS )g+?r   )r+   _r   r   r   r-   Z   s    )r7   uniquer   num_classesr8   int64CLASSEStolistr   MASKSLABELSSCORESranger=   )
r   rM   rN   idslegal_indiceslabelssegmsmasks
labels_txtoutputsr   rP   r   postprocessL   s   z-ImagePanopticSegmentationPipeline.postprocess)__name__
__module____qualname__r3   r   r   r   r   rI   rL   rg   __classcell__r   r   r   r   r      s
    	"&*r   )typingr   r   r   r?   numpyr7   r9   torchmodelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr	   modelscope.preprocessorsr
   modelscope.utils.constantr   modelscope.utils.loggerr   r   register_moduleimage_segmentationimage_panoptic_segmentationr   r   r   r   r   <module>   s$   