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
Z
d dlm  m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 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" e" Z#ej$e j%ej&dG dd deZ'dS )    N)AnyDict)nn)
transforms)	Pipelines)
OutputKeys)pipeline)InputPipeline)	PIPELINES)	LoadImage)	ModelFile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 )ContentCheckPipelinemodelc              	      sZ   t  jdd|i| ttdtdt tjg dg ddg| _t	
d dS )	aG  
        use `model` to create a content check pipeline for prediction
        Args:
            model: model id on modelscope hub.
        Example:
        ContentCheckPipeline can judge whether the picture is pornographic

        ```python
        >>> from modelscope.pipelines import pipeline
        >>> cc_func = pipeline('image_classification', 'damo/cv_resnet50_image-classification_cc')
        >>> cc_func("https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/content_check.jpg")
        {'scores': [0.2789826989173889], 'labels': 'pornographic'}
        ```
        r      )g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?)meanstdzcontent check model loaded!N )super__init__r   ComposeResize
CenterCropToTensor	Normalizetest_transformsloggerinfo)selfr   kwargs	__class__r   b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/content_check_pipeline.pyr      s   zContentCheckPipeline.__init__inputreturnc                 C   s(   t |}| | }i }||d< |S )Nimg)r   convert_to_imgr   float)r!   r&   r(   resultr   r   r%   
preprocess8   s
   
zContentCheckPipeline.preprocessc                 C   sj   |d  d}| |}dt|d d d df d d   g}|d dk r+d}nd}tj|tj|iS )	Nr(   r         g      ?pornographicnormal)	unsqueezer   Fsoftmaxtolistr   SCORESLABELS)r!   r&   r(   r+   scorelabelr   r   r%   forward?   s   
,zContentCheckPipeline.forwardinputsc                 C   s   |S )Nr   )r!   r;   r   r   r%   postprocessI   s   z ContentCheckPipeline.postprocess)__name__
__module____qualname__strr   r	   r   r   r,   r:   r<   __classcell__r   r   r#   r%   r      s
    "*
r   )(os.pathpathosptypingr   r   cv2numpynpPILtorchtorch.nn.functionalr   
functionalr3   torchvisionr   modelscope.metainfor   modelscope.outputsr   modelscope.pipelinesr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r   register_moduleimage_classificationcontent_checkr   r   r   r   r%   <module>   s,   