o
    *j                     @   s  d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	Z	d dl
Zd dl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 d d
lmZ d dlm Z  dZ!e  Z"ej#dfddZ$ej%ej&ej&dG dd deZ'dS )    N)AnyDictOptionalUnion)	make_grid)	Pipelines)MSRResNetLiteModelRealBasicVSRNetForVideoSR)
OutputKeys)InputPipeline)	PIPELINES)VideoReader)Tasks)
get_logger).mp4z.mov)r      c              	   C   s  t | pt| totdd | D }|stdt|  t | r&| g} g }| D ]}|dd}| 	 
 j| }||d  |d |d   }| }|dkrxt|tt|ddd }t|g d	d
d
d
d
f d}n*|dkr| }t|g d	d
d
d
d
f d}n|dkr| }ntd| |tjkr|d  }||}|| q*t|dkr|d }|S |}|S )aQ  Convert torch Tensors into image numpy arrays.
    After clamping to (min, max), image values will be normalized to [0, 1].
    For different tensor shapes, this function will have different behaviors:
        1. 4D mini-batch Tensor of shape (N x 3/1 x H x W):
            Use `make_grid` to stitch images in the batch dimension, and then
            convert it to numpy array.
        2. 3D Tensor of shape (3/1 x H x W) and 2D Tensor of shape (H x W):
            Directly change to numpy array.
    Note that the image channel in input tensors should be RGB order. This
    function will convert it to cv2 convention, i.e., (H x W x C) with BGR
    order.
    Args:
        tensor (Tensor | list[Tensor]): Input tensors.
        out_type (numpy type): Output types. If ``np.uint8``, transform outputs
            to uint8 type with range [0, 255]; otherwise, float type with
            range [0, 1]. Default: ``np.uint8``.
        min_max (tuple): min and max values for clamp.
    Returns:
        (Tensor | list[Tensor]): 3D ndarray of shape (H x W x C) or 2D ndarray
        of shape (H x W).
    c                 s   s    | ]}t |V  qd S N)torch	is_tensor).0t r   x/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/video_super_resolution_pipeline.py	<genexpr>4   s    

ztensor2img.<locals>.<genexpr>z(tensor or list of tensors expected, got r   r      F)Znrow	normalize)   r   r   N)r   r   r      r   z?Only support 4D, 3D or 2D tensor. But received with dimension:      o@)r   r   
isinstancelistall	TypeErrortypeZsqueezefloatdetachcpuZclamp_dimr   intmathsqrtsizenumpynpZ	transpose
ValueErroruint8roundastypeappendlen)ZtensorZout_typeZmin_max	conditionresultZ_tensorZn_dimZimg_npr   r   r   
tensor2img   sN   
$$


r7   )module_namec                       s   e Zd Z	ddeeeef 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 )VideoSuperResolutionPipelineNmodelc                    s^   t  jd||d| | j  | jj| _tj r"td| _	ntd| _	t
d d S )N)r:   preprocessorcudar'   z&load video super-resolution model doner   )super__init__r:   evalconfigr   r<   Zis_availableZdevice_deviceloggerinfo)selfr:   r;   kwargs	__class__r   r   r>   `   s   


z%VideoSuperResolutionPipeline.__init__inputreturnc                 C   s   t |}g }|D ]}|tj|dd q|j}t|D ]\}}t|d ddd	 }|
d||< qtj|dd}||dS )Nr   )Zaxisr   r   r   r(   )videofps)r   r3   r.   fliprL   	enumerater   Z
from_numpyZpermuter%   Z	unsqueezestack)rD   rH   Zvideo_readerinputsframerL   iimgr   r   r   
preprocessp   s   
z'VideoSuperResolutionPipeline.preprocessc              	   C   s   |d }t  d t| jjjtrUg }td|d| jjjD ]-}|d d ||| jjj d d d d d d f }|	| j
}|| j|d   qt j|dd}n|	| j
}| j|d  }W d    n1 sow   Y  ||d dS )NrK   r   r   outputrJ   rL   )rU   rL   )r   Zno_gradr    r@   r:   Zmax_seq_lenr)   ranger,   torA   r3   Z_inference_forwardr'   cat)rD   rH   rP   ZoutputsrR   Zimgsr   r   r   forward~   s(   
"z$VideoSuperResolutionPipeline.forwardrP   c              	   K   s   | dd }| dd}|d u rtjddj}|d jdd  \}}tjd }t|||d	 ||f}td
|d 	dD ] }	t
|d d d |	d d d d d d f }
||
tj q=|  |rtdd
ksoJ d|d d d }d| d| }tj|dd tj|iS tj|iS )NZoutput_videodemo_serviceTr   )suffixrU   Zmp4vrL   r   r   zffmpeg -versionzaffmpeg is not installed correctly, please refer to https://trac.ffmpeg.org/wiki/CompilationGuide.z_web.mp4z
ffmpeg -i z -vcodec h264 -crf 5 )shell)gettempfileNamedTemporaryFilenameshapecv2ZVideoWriter_fourccZVideoWriterrV   r,   r7   writer2   r.   r0   releaseossystem
subprocesscallr
   ZOUTPUT_VIDEO)rD   rP   rE   Zoutput_video_pathrZ   hwZfourccZvideo_writerrR   rS   Zoutput_video_path_for_webZconvert_cmdr   r   r   postprocess   s4   
*

z(VideoSuperResolutionPipeline.postprocessr   )__name__
__module____qualname__r   r	   r   strr>   r   r   r   rT   rY   rm   __classcell__r   r   rF   r   r9   \   s    "*r9   )(r*   rg   ri   r`   typingr   r   r   r   rd   r-   r.   r   Ztorchvision.utilsr   Zmodelscope.metainfor   Z+modelscope.models.cv.video_super_resolutionr   r	   Zmodelscope.outputsr
   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessors.cvr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   ZVIDEO_EXTENSIONSrB   r0   r7   Zregister_moduleZvideo_super_resolutionr9   r   r   r   r   <module>   s0   >