
    sg?                     (    d dl ZddZd ZddZd Zy)    Nc                     ddl m ddlmc m fd}t        | t              r*| D cg c]
  } ||       }}t        j                  |      S  ||       }|S c c}w )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                    	j                  |       }|j                          t        j                  |j	                         t        j
                        }| j                  j                         \  }}|j                  ||dg      d d d d ddf   }t        j                  |dd      }rj                  |        |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
          Q/var/www/html/venv/lib/python3.12/site-packages/torch/utils/tensorboard/_utils.pyrender_to_rgbz&figure_to_image.<locals>.render_to_rgb   s     008==););)=RXXN}}--/1LL!Q+Aq!A#I6	KK	!C	IIf    )	matplotlib.pyplotpyplotmatplotlib.backends.backend_aggbackendsbackend_agg
isinstancelistr   stack)figuresr   r    r   imagesimager   r   s    `    @@r   figure_to_imager-      sZ     $==	 '4 6=>F-'>>xxg&	 ?s   Ac           
         | j                   \  }}}}}| j                  t        j                  k(  rt        j                  |       dz  } d } || j                   d         skt        d| j                   d   j                         z  | j                   d   z
        }t        j                  | t        j                  |||||f      fd      } d|j                         dz
  dz  z  }| j                   d   |z  }	t        j                  | ||	||||f      } t        j                  | d	
      } t        j                  | |||z  |	|z  |f      } | 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                 &    | dk7  xr | | dz
  z  dk(  S )Nr       )nums    r   	is_power2z!_prepare_video.<locals>.is_power27   s    ax4cS1Wo!34r!   r   r	   )shape)axisr0   )newshape)r	   r   r   r0      r   )axes)r4   r   r   r   float32int
bit_lengthconcatenatezerosr   	transpose)
Vbtcr   r   r3   len_additionn_rowsn_colss
             r   _prepare_videorF   (   s'    GGMAq!Qww"((JJqME!5 QWWQZ 1
 5 5 77!''!*DENNArxx|Q1a.HIJQRSALLNQ&1,-FWWQZ6!F


11a;<A
Q/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 ]A  }t        |      D ]1  }	||k\  r | |   |d d ||z  |dz   |z  |	|z  |	dz   |z  f<   |dz   }3 C |S )Nz*plugin error, should pass numpy array herer0   r   r   r   r	   r   )r'   r   ndarrayr4   r<   ndimminr:   ceilfloatr=   r   range)
IncolsnimgHWnrowsr   iyxs
             r   	make_gridrW   I   sN   a$R&RR$wwqzQNNAq!9a(66Q;1771:?**771:D	
A	
AeEde+,-EXXq!e)QY/qww?F	A5\ u 	ADyBCA$F1a!eq1uk)1q5AEQ;+>>?AA		 Mr!   c                 @   t        t        |            t        |      k(  s
J d|        t        | j                        t        |      k(  sJ d| j                   d|        |j                         }t        |      dk(  rMdD cg c]  }|j	                  |       }}| j                  |      }t        |      }|j                  ddd      S t        |      d	k(  r\d
D cg c]  }|j	                  |       }}| j                  |      }|j                  d   dk(  rt        j                  |||gd      }|S t        |      dk(  rJdD cg c]  }|j	                  |       }}| j                  |      } t        j                  | | | gd      } | S y c c}w c c}w c c}w )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   NCHWr0   r	   r   r   HWCHW)
lensetr4   upperfindr>   rW   r   r<   r)   )tensorinput_formatrB   indextensor_NCHW
tensor_CHW
tensor_HWCs          r   convert_to_HWCrf   c   s   s< !S&  c	ST`Sabc  v||!  D
||n$4\NDD   %%'L
<A/56!""1%66&&u-{+
##Aq!,,
<A/45!""1%55%%e,
A!#Z(LaPJ
<A/34!""1%44!!%(6662A6	  7 6 5s   F F
F)T)   )numpyr   r-   rF   rW   rf   r1   r!   r   <module>ri      s    DB4r!   