o
    *j                     @   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   }/var/www/html/Deteccion_Ine/venv/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_ImageToTensorZLoadImageFromWebcam   z9input should be either str, PIL.Image, np.array, but got )img   )Z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#   ) Zmmdet.datasets.pipelinesr   Zmmcv.parallelr   r   Zmmdet.datasetsr    r   cfg
isinstancestrr(   testZpipelinetypenparrayr   PILZImageZndarraylenshapecv2ZcvtColorZCOLOR_GRAY2BGR	TypeErrordictnext
parametersZis_cudaZdevice)
r   r   r   r   r   r    r,   r#   r(   Ztest_pipeliner   r   r   
preprocess#   s>   
$

z,ImageSemanticSegmentationPipeline.preprocessc                 C   s   | j |}|S N)r   Z	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 r<   )r   postprocessr   ZMASKSZLABELSZSCORES)r   r?   r=   Zoutputsr   r   r   r@   R   s   z-ImageSemanticSegmentationPipeline.postprocess)__name__
__module____qualname__r.   r   r   r   r   r;   r>   r@   __classcell__r   r   r   r   r      s
    
"+*r   )typingr   r   r   r6   numpyr1   r3   ZtorchZmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   r	   Zmodelscope.pipelines.builderr
   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   r   Zregister_moduleZimage_segmentationZimage_semantic_segmentationr   r   r   r   r   <module>   s$   