o
    *jM                     @   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 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)UNetForVideoDeinterlace)
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   s/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/video_deinterlace_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   
$$


r6   )module_namec                       s   e Zd Z	dd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 )VideoDeinterlacePipelineNmodelc                    sl   t  jd||d| tj rtd| _ntd| _| jj| _| j	| j | j
  td dS )a  The inference pipeline for all the video deinterlace sub-tasks.

        Args:
            model (`str` or `Model` or module instance): A model instance or a model local dir
                or a model id in the model hub.
            preprocessor (`Preprocessor`, `optional`): A Preprocessor instance.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.

        Example:
            >>> from modelscope.pipelines import pipeline
            >>> pipeline_ins = pipeline('video-deinterlace',
                model='damo/cv_unet_video-deinterlace')
            >>> input = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/videos/video_deinterlace_test.mp4'
            >>> print(pipeline_ins(input)[OutputKeys.OUTPUT_VIDEO])
        )r9   preprocessorcudar&   z!load video deinterlace model doneNr   )super__init__r   r;   Zis_availableZdevice_devicer9   nettoevalloggerinfo)selfr9   r:   kwargs	__class__r   r   r=   `   s   


z!VideoDeinterlacePipeline.__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   r2   r-   fliprL   	enumerater   Z
from_numpyZpermuter$   	unsqueezestack)rD   rH   Zvideo_readerinputsframerL   iimgr   r   r   
preprocess   s   
z#VideoDeinterlacePipeline.preprocessc              	   C   s  |d d }| j j}| j j}t b g }g }td|dD ]3}|||||d df | j	 |dkr@|d gd }q|||
 d |dd  }q||d  |||
 d tj|dd}W d    n1 sww   Y  ||d d	S )
NrK   r   r   .r   rJ   rL   )outputrL   )r?   frenetenhnetr   Zno_gradranger+   r2   r@   r>   r&   rO   cat)rD   rH   rQ   rX   rY   ZoutputsframesrS   r   r   r   forward   s$   
&z VideoDeinterlacePipeline.forwardrQ   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_serviceFr   )suffixrW   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 T)shell)gettempfileNamedTemporaryFilenameshapecv2ZVideoWriter_fourccZVideoWriterrZ   r+   r6   writer1   r-   r/   releaseossystem
subprocesscallr	   ZOUTPUT_VIDEO)rD   rQ   rE   Zoutput_video_pathr^   hwZfourccZvideo_writerrS   rT   Zoutput_video_path_for_webZconvert_cmdr   r   r   postprocess   s4   
*

z$VideoDeinterlacePipeline.postprocessr   )__name__
__module____qualname__r   r   strr=   r
   r   r   rU   r]   rq   __classcell__r   r   rF   r   r8   \   s    
 "*r8   )'r)   rk   rm   rd   typingr   r   r   r   rh   r,   r-   r   Ztorchvision.utilsr   Zmodelscope.metainfor   ZAmodelscope.models.cv.video_deinterlace.UNet_for_video_deinterlacer   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   r/   r6   Zregister_moduleZvideo_deinterlacer8   r   r   r   r   <module>   s0   >