o
    pi<                  	   @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	Z
d dlZd dlZddlmZmZ ddlmZ e ZeeZedd Zdd
eejj dededefddZddefddZddefddZ		ddeee
j eejj f dededefddZdS )    N)contextmanager)ListUnion   )BACKENDS_MAPPINGis_opencv_available)
get_loggerc                 c   s    t | }|V  |  d S N)ioBufferedWriterflush)raw_ff r   Z/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/utils/export_utils.pybuffered_writer   s   
r   
   imageoutput_gif_pathfpsreturnc                 C   s@   |d u rt jddj}| d j|d| dd  dd| dd |S )	Nz.gifsuffixr   Tr   Fi  )save_allappend_imagesoptimizedurationloop)tempfileNamedTemporaryFilenamesave)r   r   r   r   r   r   export_to_gif   s   
r"   output_ply_pathc                    s  |du rt jddj} j   } j  }tj	 fdddD dd}t
t|d	}|d
 |d |tdt| dd |d |d |d |durk|d |d |d |dur|tdt| dd |d |d |dur|d  t}dd t| | D }td}|D ]
}||j|  qntd}| D ]
}	||j|	  q|durtd}| D ]}
||jt|
g|
R   qW d   |S W d   |S 1 sw   Y  |S )z&
    Write a PLY file for a mesh.
    Nz.plyr   c                    "   g | ]} j |    qS r   vertex_channelsdetachcpunumpy.0xmeshr   r   
<listcomp>4      " z!export_to_ply.<locals>.<listcomp>RGBr   axiswbs   ply
s    format binary_little_endian 1.0
zelement vertex 
asciis   property float x
s   property float y
s   property float z
s   property uchar red
s   property uchar green
s   property uchar blue
zelement face s%   property list uchar int vertex_index
s   end_header
go@c                 S   s   g | ]\}}g ||R qS r   r   )r+   coordrgbr   r   r   r/   H   s    z<3f3Bz<3fz<B3I)r   r   r    vertsr'   r(   r)   facesnpstackr   openwritebyteslenroundastypeintziptoliststructStructpack)r.   r#   coordsr:   r8   r   verticesformatitemvertextrir   r-   r   export_to_ply+   s^   













&
&&rO   output_obj_pathc                    s   |d u rt jddj} j   } j  }tj	 fdddD dd}dd t
| | D }d	d | D }d
d |D | }t|d}|d| W d    d S 1 sbw   Y  d S )Nz.objr   c                    r$   r   r%   r*   r-   r   r   r/   f   r0   z!export_to_obj.<locals>.<listcomp>r1   r   r2   c                 S   s$   g | ]\}}d j g ||R  qS )z{} {} {} {} {} {})rK   )r+   r7   colorr   r   r   r/   g   s    c              	   S   s>   g | ]}d  t|d d t|d d t|d d qS )z
f {} {} {}r   r      )rK   str)r+   rN   r   r   r   r/   k   s   > c                 S   s   g | ]}d | qS )zv r   )r+   rM   r   r   r   r/   m   s    wr5   )r   r   r    r9   r'   r(   r)   r:   r;   r<   rD   rE   r=   
writelinesjoin)r.   rP   r9   r:   vertex_colorsrJ   combined_datar   r   r-   r   export_to_obj_   s   "rY   video_framesoutput_video_pathc                 C   s   t  rdd l}nttd d d|d u rtjddj}t| d t	j
r.dd | D } nt| d tjjr>d	d | D } |jd
 }| d j\}}}|j|||||fd}tt| D ]}	|| |	 |j}
||
 q\|S )Nr   opencvr   export_to_videoz.mp4r   c                 S   s   g | ]
}|d   tjqS )   )rB   r;   uint8r+   framer   r   r   r/   ~   s    z#export_to_video.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r;   arrayr`   r   r   r   r/      s    mp4v)r   	frameSize)r   cv2ImportErrorr   rK   r   r   r    
isinstancer;   ndarrayPILImageVideoWriter_fourccshapeVideoWriterranger@   cvtColorCOLOR_RGB2BGRr>   )rZ   r[   r   re   fourcchrT   cvideo_writeriimgr   r   r   r]   s   s    

r]   )Nr   r	   ) r
   randomrF   r   
contextlibr   typingr   r   r)   r;   	PIL.Imageri   PIL.ImageOpsimport_utilsr   r   loggingr   Random
global_rng__name__loggerr   rj   rS   rC   r"   rO   rY   rh   r]   r   r   r   r   <module>   s8    
$4