o
    ॵi                     @   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   is_availabledevice_deviceloggerinfo)selfr   r   kwargs	__class__r   a/home/ubuntu/.local/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   convert_to_imgr   ComposeToTensor	unsqueezetor   )r"   r'   r)   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   outputs)	r   
zeros_likeshapemaxrangemin
contiguousr   _inference_forward)r"   r'   outputihiw	crop_rows	crop_colsoverlapstep_hstep_wyxcrop_ycrop_xcrop_hcrop_wcrop_framesh_startw_starth_end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   no_gradrP   )r"   r'   rT   rS   r=   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 )NrU   r      r3      uint8)squeezer   permutenumpyastyper	   
OUTPUT_IMG)r"   r'   
output_imgr   r   r&   postprocesss   s   
"zImageDeblurPipeline.postprocessrQ   )__name__
__module____qualname____doc__r   r   strr   r   r   r
   r   r   r1   rP   rW   rb   __classcell__r   r   r$   r&   r      s    
"#*) typingr   r   r   r   r   torchvisionr   modelscope.metainfor   !modelscope.models.cv.image_deblurr   modelscope.outputsr	   modelscope.pipelines.baser
   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   r   modelscope.utils.constantr   modelscope.utils.loggerr   r    __all__register_moduleimage_deblurringimage_deblurr   r   r   r   r&   <module>   s"   