
    `iY                     6    d dl Zd dlmZ ddZd Zd	dZd ZdS )
    NTc                     ddl m ddlmc m fdt          | t                    r"fd| D             }t          j        |          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                    	                     |           }|                                 t          j        |                                t          j                  }| j                                        \  }}|                    ||dg          d d d d ddf         }t          j	        |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_chwr   pltplt_backend_aggs
          r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/utils/tensorboard/_utils.pyrender_to_rgbz&figure_to_image.<locals>.render_to_rgb   s     0088M&*<*<*>*>bhOOO}--//1LL!Q++AAAqqq!A#I6	K	!CCC	 	IIf    c                 &    g | ]} |          S  r#   ).0r   r    s     r   
<listcomp>z#figure_to_image.<locals>.<listcomp>"   s#    >>>F--''>>>r!   )	matplotlib.pyplotpyplotmatplotlib.backends.backend_aggbackendsbackend_agg
isinstancelistr   stack)figuresr   imagesimager   r   r    s    `  @@@r   figure_to_imager1      s     $#####=========	 	 	 	 	 	 	 '4   >>>>g>>>xg&&r!   c           
         | j         \  }}}}}| j        t          j        k    rt          j        |           dz  } d } || j         d                   smt          d| j         d                                         z  | j         d         z
            }t          j        | t          j        |||||f          fd          } d|                                dz
  dz  z  }| j         d         |z  }	t          j	        | ||	||||f          } t          j
        | d	
          } t          j	        | |||z  |	|z  |f          } | S )aJ  
    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 spread to a grid, which forms a frame.
    e.g. Video with batchsize 16 will have a 4x4 grid.
    g     o@c                 &    | dk    o| | dz
  z  dk    S )Nr      r#   )nums    r   	is_power2z!_prepare_video.<locals>.is_power28   s    ax4cS1Wo!34r!   r   r	   )shape)axisr4   )newshape)r	   r   r   r4      r   )axes)r7   r   r   r   float32int
bit_lengthconcatenatezerosr   	transpose)
Vbtcr   r   r6   len_additionn_rowsn_colss
             r   _prepare_videorI   )   sM    GMAq!Qw"(JqMME!5 5 5 9QWQZ   T1
 5 5 7 77!'!*DEENArx|Q1a.HIIIJQRSSSALLNNQ&1,-FWQZ6!F

11a;<<<A
Q/000A

16A:vz1=>>>AHr!      c           	         t          | t          j                  s
J d            | j        d         dk    rt          j        | | | gd          } | j        dk    r| j        d         dk    sJ | j        d         }| j        d         }| j        d         }t          ||          }t          t          j        t          |          |z                      }t          j
        d||z  ||z  f| j                  }d}t          |          D ]G}t          |          D ]5}	||k    r n,| |         |d d ||z  |dz   |z  |	|z  |	dz   |z  f<   |dz   }6H|S )Nz*plugin error, should pass numpy array herer4   r   r   r   r	   r   )r+   r   ndarrayr7   r?   ndimminr=   ceilfloatr@   r   range)
IncolsnimgHWnrowsr   iyxs
             r   	make_gridr[   J   sg   a$$RR&RRR$wqzQNAq!9a((6Q;;171:???*71:D	
A	
AeEde+,,--EXq!e)QY/qw???F	A5\\  u 	 	ADyyBCA$F111a!eq1uk)1q5AEQ;+>>?AAAMr!   c                 2   t          t                              t                    k    sJ d             t          | j                  t                    k    sJ d| j         d                                             t                    dk    rIfddD             }|                     |          }t          |          }|                    ddd	          S t                    d
k    rNfddD             }|                     |          }|j        d         dk    rt          j        |||gd          }|S t                    dk    r=fddD             }|                     |          } t          j        | | | 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 | ]}                     |          S r#   findr$   rE   input_formats     r   r%   z"convert_to_HWC.<locals>.<listcomp>o   s'    666!""1%%666r!   NCHWr4   r	   r   r   c                 :    g | ]}                     |          S r#   r^   r`   s     r   r%   z"convert_to_HWC.<locals>.<listcomp>u   s'    555!""1%%555r!   HWCc                 :    g | ]}                     |          S r#   r^   r`   s     r   r%   z"convert_to_HWC.<locals>.<listcomp>|   s'    444!""1%%444r!   HW)	lensetr7   upperrA   r[   r   r?   r-   )tensorra   indextensor_NCHW
tensor_CHW
tensor_HWCs    `    r   convert_to_HWCro   d   s   s<  !!S& &   bT`bb   v|! !   D|D D5AD D    %%''L
<A6666v666&&u--{++
##Aq!,,,
<A5555u555%%e,,
A!##Z(LaPPJ
<A4444t444!!%((6662A66	 r!   )T)rJ   )	numpyr   numpy.typingtypingnptr1   rI   r[   ro   r#   r!   r   <module>rt      su                D  B   4    r!   