o
    *jZ                     @   s   d dl mZmZ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 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OptionalUnionN)	Pipelines)FastInst)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)Tasks)
get_logger)module_namec                       s   e Zd Z	ddeeef def fddZdd Z			d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de
eef de
eef fddZ  ZS ) FastInstanceSegmentationPipelineNmodelpreprocessorc                    s&   t  jd||d| | j  dS )a  The inference pipeline for fastinst models.

        The model outputs a dict with keys of `scores`, `labels`, and `masks`.

        Args:
            model (`str` or `Model` or module instance): A model instance or a model local dir
                or a model id in the model hub.
            preprocessor (`Preprocessor`, `optional`): A Preprocessor instance.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.

        Examples:
            >>> from modelscope.outputs import OutputKeys
            >>> from modelscope.pipelines import pipeline
            >>> pipeline_ins = pipeline('image-segmentation',
                model='damo/cv_resnet50_fast-instance-segmentation_coco')
            >>> input_img = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_instance_segmentation.jpg'
            >>> print(pipeline_ins(input_img)[OutputKeys.LABELS])
        )r   r   N )super__init__r   eval)selfr   r   kwargs	__class__r   |/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/fast_instance_segmentation_pipeline.pyr      s   z)FastInstanceSegmentationPipeline.__init__c                 C   s   ||}}|d }|t || }||k r||| }	}
n|| |}	}
t|	|
|kr;|d t|	|
 }|	| }	|
| }
t|
d }
t|	d }	|	|
fS )Ng      ?      ?)minmaxint)r   ZoldhZoldwZshort_edge_lengthmax_sizehwsizescaleZnewhZnewwr   r   r   _get_preprocess_shape2   s   
z6FastInstanceSegmentationPipeline._get_preprocess_shape  5  inputreturnc                 C   sx   t |}|jd d \}}||d}| ||||\}}	tt||	ft g}
|
|}|d |d< d|gi}|S )N   )widthheightg     o@imageZbatched_inputs)r   Zconvert_to_imgr#   r%   TZComposeZResizeZToTensor)r   r(   Zmin_sizer    r-   r"   r!   Zdataset_dictZnew_hZnew_wZtest_transformsresultr   r   r   
preprocessB   s   


z+FastInstanceSegmentationPipeline.preprocessc                 K   s>   t   | jdi |}W d    |S 1 sw   Y  |S )Nr   )torchZno_gradr   )r   r(   Zforward_paramsoutputr   r   r   forwardS   s   

z(FastInstanceSegmentationPipeline.forwardr   inputsc                 C   s   |d d d }|d     }|d     }|d     }t||k}|| }|| }|| }tjg tjg tjg i}t	|||D ]2\}	}
}t
|	}	| jjt|
 }tj|tj
d}|tj |	 |tj | |tj | qJ|S )NZeval_resultr   Z	instancesscores
pred_maskspred_classes)Zdtype)detachcpunumpynparrayr   ZMASKSZLABELSZSCORESzipZfloat64r   classesr   append)r   r4   Z	score_thrZpredictionsr5   r6   r7   Zthresholded_idxsZresults_dictZscoreclsmasklabelr   r   r   postprocessY   s(   
z,FastInstanceSegmentationPipeline.postprocess)N)r&   r'   )r   )__name__
__module____qualname__r   r   strr   r   r%   r	   r   r   r0   r3   rC   __classcell__r   r   r   r   r      s0    






r   )!typingr   r   r   r   r:   r;   r1   Ztorchvision.transformsZ
transformsr.   Zmodelscope.metainfor   Z0modelscope.models.cv.image_instance_segmentationr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   r
   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   loggerZregister_moduleZimage_segmentationZfast_instance_segmentationr   r   r   r   r   <module>   s"   