o
    0h[                     @   s<   d dl Zd dlmZ dddZdd Zddd	Zd
d ZdS )    NTc                    s`   ddl m ddlm  m  fddt| tr*fdd| D }t|S | }|S )aB  Render matplotlib figure to numpy format.

    Note that this requires the ``matplotlib`` package.

    Args:
        figures (matplotlib.pyplot.figure or list of figures): figure or a list of figures
        close (bool): Flag to automatically close the figure

    Returns:
        numpy.array: image in [CHW] order
    r   Nc                    s|    | }|  tj| tjd}| j \}}|||dgd d d d ddf }tj	|ddd} r<
|  |S )Ndtype   r         )sourcedestination)FigureCanvasAggdrawnp
frombufferbuffer_rgbauint8canvasget_width_heightreshapemoveaxisclose)figurer   datawh	image_hwc	image_chw)r   pltplt_backend_agg R/var/www/vscode/kcb/lib/python3.10/site-packages/torch/utils/tensorboard/_utils.pyrender_to_rgb   s   
&
z&figure_to_image.<locals>.render_to_rgbc                    s   g | ]} |qS r   r   ).0r   )r   r   r   
<listcomp>"   s    z#figure_to_image.<locals>.<listcomp>)	matplotlib.pyplotpyplotmatplotlib.backends.backend_aggbackendsbackend_agg
isinstancelistr   stack)figuresr   imagesimager   )r   r   r   r   r   figure_to_image   s   

r,   c           
      C   s   | j \}}}}}| jtjkrt| d } dd }|| j d sBtd| j d   | j d  }tj| tj|||||fdfdd} d| d d  }| j d | }	tj	| ||	||||fd	} tj
| d
d} tj	| ||| |	| |fd	} | S )aL  
    Convert a 5D tensor into 4D tensor.

    Convesrion is done from [batchsize, time(frame), channel(color), height, width]  (5D tensor)
    to [time(frame), new_width, new_height, channel] (4D tensor).

    A batch of images are spreaded to a grid, which forms a frame.
    e.g. Video with batchsize 16 will have a 4x4 grid.
    g     o@c                 S   s   | dko| | d @ dkS )Nr      r   )numr   r   r   	is_power28   s   z!_prepare_video.<locals>.is_power2r   r   )shape)axisr-   )newshape)r   r   r   r-      r   )axes)r0   r   r   r   float32int
bit_lengthconcatenatezerosr   	transpose)
Vbtcr   r   r/   len_additionn_rowsn_colsr   r   r   _prepare_video)   s   
 $rB      c           
   	   C   s  t | tjs
J d| jd dkrt| | | gd} | jdkr&| jd dks(J | jd }| jd }| jd }t||}ttt	|| }tj
d|| || f| jd}d}t|D ].}t|D ]'}	||krj n | | |d d || |d | |	| |	d | f< |d }qbq\|S )Nz*plugin error, should pass numpy array herer-   r   r   r   r   r   )r&   r   ndarrayr0   r8   ndimminr6   ceilfloatr9   r   range)
IncolsnimgHWnrowsr   iyxr   r   r   	make_gridJ   s&   



6
rS   c                    s  t t t  ksJ d  t | jt  ks%J d| j d     t  dkrH fdddD }| |}t|}|dd	d
S t  dkrn fdddD }| |}|jd	 dkrlt|||gd	}|S t  d	kr fdddD }| |} t| | | gd	} | S d S )NzJYou can not use the same dimension shordhand twice.         input_format: zKsize of input tensor and input format are different.         tensor shape: z, input_format: r   c                       g | ]}  |qS r   findr   r>   input_formatr   r   r    o       z"convert_to_HWC.<locals>.<listcomp>NCHWr-   r   r   r   c                    rT   r   rU   rW   rX   r   r   r    u   rZ   HWCc                    rT   r   rU   rW   rX   r   r   r    |   rZ   HW)	lensetr0   upperr:   rS   r   r8   r(   )tensorrY   indextensor_NCHW
tensor_CHW
tensor_HWCr   rX   r   convert_to_HWCd   s@   



rf   )T)rC   )	numpyr   numpy.typingtypingnptr,   rB   rS   rf   r   r   r   r   <module>   s   
"
!