o
    *j                     @   s   d dl mZmZmZm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mZ d d
lmZ d dlmZ e ZdgZejeje	jdG dd deZdS )    )AnyDictOptionalUnionN)
transforms)	Pipelines)NAFNetForImageDeblur)
OutputKeys)InputPipeline)	PIPELINES)ImageDeblurPreprocessor	LoadImage)Tasks)
get_loggerImageDeblurPipeline)module_namec                       s   e Zd ZdZ	ddeeef dee f fddZ	de
deeef fd	d
Z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 )r   a  

    Examples:

    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks
    >>> from modelscope.outputs import OutputKeys
    >>> import cv2
    >>>
    >>> img = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/blurry.jpg'
    >>> image_deblur_pipeline = pipeline(Tasks.image_deblurring, 'damo/cv_nafnet_image-deblur_gopro')
    >>> result = image_deblur_pipeline(img)[OutputKeys.OUTPUT_IMG]
    >>> cv2.imwrite('result.png', result)
    Nmodelpreprocessorc                    s^   t  jd||d| | j  | jj| _tj r"td| _	ntd| _	t
d dS )z
        use `model` and `preprocessor` to create a cv image deblur pipeline for prediction
        Args:
            model: model id on modelscope hub.
        )r   r   cudacpuzload image denoise model doneN )super__init__r   evalconfigtorchr   Zis_availableZdevice_deviceloggerinfo)selfr   r   kwargs	__class__r   n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/image_deblur_pipeline.pyr   '   s   	


zImageDeblurPipeline.__init__inputreturnc                 C   s<   t |}tt g}||}d|d| ji}|S )Nimgr   )r   Zconvert_to_imgr   ZComposeZToTensorZ	unsqueezetor   )r    r%   r'   Ztest_transformsresultr   r   r$   
preprocess:   s
   
zImageDeblurPipeline.preprocessc              
   C   s  t |}|jdd  \}}t|d dt|d d}}d}|| || }}	t|D ]}
t|D ]}||
 }|	| }|
|d k rD|n|| }||d k rP|	n|| }|d d d d td|| t|| | |td|| t|| | |f  }td|| dkr|nd}td|| dkr|nd}t|| | ||k r|| n|}t|| | ||k r|| n|}| j|d d d d d ||||f |d d d d ||| ||| f< q2q,|S )Ni         r   Zoutputs)	r   Z
zeros_likeshapemaxrangemin
contiguousr   Z_inference_forward)r    r%   outputZihiwZ	crop_rowsZ	crop_colsZoverlapZstep_hZstep_wyxZcrop_yZcrop_xZcrop_hZcrop_wZcrop_framesZh_startZw_startZh_endZw_endr   r   r$   crop_processA   sv   

z ImageDeblurPipeline.crop_processc                 C   s\   dd }d}|| j | t  | |d }W d    d|iS 1 s%w   Y  d|iS )Nc                 S   s   |r|    d S |   d S N)trainr   )r   is_trainr   r   r$   	set_phasef   s   z.ImageDeblurPipeline.forward.<locals>.set_phaseFr'   output_tensor)r   r   Zno_gradr7   )r    r%   r;   r:   r3   r   r   r$   forwardd   s   

zImageDeblurPipeline.forwardc                 C   sL   |d  dd  ddd d}tj|d d d d d d df iS )Nr<   r      r,      Zuint8)Zsqueezer   ZpermutenumpyZastyper	   Z
OUTPUT_IMG)r    r%   Z
output_imgr   r   r$   postprocesss   s   
"zImageDeblurPipeline.postprocessr8   )__name__
__module____qualname____doc__r   r   strr   r   r   r
   r   r   r*   r7   r=   rB   __classcell__r   r   r"   r$   r      s    
"#*) typingr   r   r   r   r   Ztorchvisionr   Zmodelscope.metainfor   Z!modelscope.models.cv.image_deblurr   Zmodelscope.outputsr	   Zmodelscope.pipelines.baser
   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   r   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   r   __all__Zregister_moduleZimage_deblurringZimage_deblurr   r   r   r   r$   <module>   s"   