o
    *j	                     @   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?)meanZstdzcontent check model loaded!N )super__init__r   ZComposeZResizeZ
CenterCropZToTensorZ	Normalizetest_transformsloggerinfo)selfr   kwargs	__class__r   o/var/www/html/Deteccion_Ine/venv/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   Z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      ?Zpornographicnormal)Z	unsqueezer   FZsoftmaxtolistr   ZSCORESZLABELS)r   r    r"   r$   Z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   )(Zos.pathpathZosptypingr   r   Zcv2numpynpZPILZtorchZtorch.nn.functionalr   Z
functionalr*   Ztorchvisionr   Zmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelinesr   Zmodelscope.pipelines.baser	   r
   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r   Zregister_moduleZimage_classificationZcontent_checkr   r   r   r   r   <module>   s,   