
    1;jiI              	       x   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j                    Z ee          Zed             Zdd	eej        j                 d
ededefdZddefdZddefdZ	 ddeee
j                 eej        j                 f         dededefdZdS )    N)contextmanager)ListUnion   )BACKENDS_MAPPINGis_opencv_available)
get_loggerc              #   b   K   t          j        |           }|V  |                                 d S N)ioBufferedWriterflush)raw_ffs     V/root/voice-cloning/.venv/lib/python3.11/site-packages/diffusers/utils/export_utils.pybuffered_writerr      s0      
%  A
GGGGGIIIII    
   imageoutput_gif_pathfpsreturnc                     |t          j        d          j        }| d                             |d| dd          dd|z  d           |S )	Nz.gifsuffixr   Tr   Fi  )save_allappend_imagesoptimizedurationloop)tempfileNamedTemporaryFilenamesave)r   r   r   s      r   export_to_gifr%      sc    "5VDDDI	!HMMABBi     r   output_ply_pathc           	          |t          j        d          j        } j                                                                                                        } j                                                                        }t          j	         fddD             d          }t          t          |d                    5 }|                    d	           |                    d
           |                    t          dt          |           dd                     |                    d           |                    d           |                    d           |?|                    d           |                    d           |                    d           |I|                    t          dt          |           dd                     |                    d           |                    d           ||dz                                                      t"                    }d t%          |                                |                                          D             }t)          j        d          }|D ]}|                     |j        |             nHt)          j        d          }|                                D ]}	|                     |j        |	             |Yt)          j        d          }|                                D ]0}
|                     |j        t          |
          g|
R             1ddd           n# 1 swxY w Y   |S )z&
    Write a PLY file for a mesh.
    Nz.plyr   c                     g | ]E}j         |                                                                                                         FS  vertex_channelsdetachcpunumpy.0xmeshs     r   
<listcomp>z!export_to_ply.<locals>.<listcomp>7   sE    RRRqD(+224488::@@BBRRRr   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                 "    g | ]\  }}g ||R S r)   r)   )r0   coordrgbs      r   r3   z!export_to_ply.<locals>.<listcomp>K   s6       E3 %#  r   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)r2   r&   coordsr>   r<   r   verticesformatitemvertextris   `          r   export_to_plyrS   .   st    "5VDDDIZ  $$&&,,..FJNN""$$E
(RRRRERRRYZ
[
[
[C	ot44	5	5 $5		4555	7F777AABBB	%&&&	%&&&	%&&&?GG+,,,GG-...GG,---GGE8#e**888'BBCCCGG=>>>	   ?=''))0055C "%MMOOJJLL# #  H ]7++F  , ,T*++++, ]5))F --// . .V,----]6**F||~~ 5 5CHH3s3334444I$5 $5 $5 $5 $5 $5 $5 $5 $5 $5 $5 $5 $5 $5 $5L s   JMM Moutput_obj_pathc                     |t          j        d          j        } j                                                                                                        } j                                                                        }t          j	         fddD             d          }d t          |                                |                                          D             }d |                                D             }d	 |D             |z   }t          |d
          5 }|                    d                    |                     d d d            d S # 1 swxY w Y   d S )Nz.objr   c                     g | ]E}j         |                                                                                                         FS r)   r*   r/   s     r   r3   z!export_to_obj.<locals>.<listcomp>i   sF    \\\QRd215<<>>BBDDJJLL\\\r   r4   r   r5   c                 2    g | ]\  }} d j         g ||R  S )z{} {} {} {} {} {})rO   )r0   r;   colors      r   r3   z!export_to_obj.<locals>.<listcomp>j   sC       7Cue""2E2E222  r   c           
          g | ][}d                      t          |d         dz             t          |d         dz             t          |d         dz                       \S )z
f {} {} {}r   r      )rO   str)r0   rR   s     r   r3   z!export_to_obj.<locals>.<listcomp>n   sV    nnnX[\  SVaZ#c!fqj//3s1vPQz??SSnnnr   c                     g | ]}d |z   S )zv r)   )r0   rQ   s     r   r3   z!export_to_obj.<locals>.<listcomp>p   s    :::vTF]:::r   wr8   )r!   r"   r#   r=   r,   r-   r.   r>   r?   r@   rH   rI   rA   
writelinesjoin)r2   rT   r=   r>   vertex_colorsrN   combined_datar   s   `       r   export_to_objrb   b   s   "5VDDDIJ##%%++--EJNN""$$EH\\\\V[\\\cdeeeM GJ5<<>>[h[o[o[q[qGrGr  H on_d_k_k_m_mnnnE:::::UBM	os	#	# /q	TYY}--.../ / / / / / / / / / / / / / / / / /s   #)EE Evideo_framesoutput_video_pathc                    t                      rdd l}n3t          t          d         d                             d                    |t          j        d          j        }t          | d         t          j
                  rd | D             } n1t          | d         t          j        j                  rd | D             }  |j        d	 }| d         j        \  }}}|                    |||||f
          }t!          t#          |                     D ]8}	|                    | |	         |j                  }
|                    |
           9|S )Nr   opencvr   export_to_videoz.mp4r   c                 R    g | ]$}|d z                       t          j                  %S )   )rF   r?   uint8r0   frames     r   r3   z#export_to_video.<locals>.<listcomp>   s,    QQQ5,,RX66QQQr   c                 6    g | ]}t          j        |          S r)   )r?   arrayrk   s     r   r3   z#export_to_video.<locals>.<listcomp>   s     BBBEBBBr   mp4v)r   	frameSize)r   cv2ImportErrorr   rO   r!   r"   r#   
isinstancer?   ndarrayPILImageVideoWriter_fourccshapeVideoWriterrangerD   cvtColorCOLOR_RGB2BGRrB   )rc   rd   r   rq   fourcchr]   cvideo_writeriimgs              r   rg   rg   v   sW     S



*84Q7>>?PQQRRR $7vFFFK,q/2:.. CQQLQQQ	LOSY_	5	5 CBB\BBB#S#V,F1o#GAq!??#4f#RSUVQW?XXL3|$$%%    ll<?C,=>>3r   )Nr   r   ) r   randomrJ   r!   
contextlibr   typingr   r   r.   r?   	PIL.Imageru   PIL.ImageOpsimport_utilsr   r   loggingr	   Random
global_rng__name__loggerr   rv   r[   rG   r%   rS   rb   rt   rg   r)   r   r   <module>r      s   				    % % % % % %                                   V]__
	H		    cio.  RU _b    1 1 1 1 1 1h/ / / / / /* mo RZ($sy*??@UXfi     r   