CoolFace
Apppublic

Prakhar56/Loop

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
video_generator.cpython-311.pyc108 linesDownload Raw Back to __pycache__
12%uNh�.���ddlZddlZddlmZddlZddlmZddlm	Z3ddlZddlZej
e��ZGd�d��ZdS)�N)�DiffusionPipeline)�Imagec�Z�eZdZdZdd�Zd�Zdd�Zdd4�Zdd�Zd
�Z	d�Z5dd�Zd�Zdd�Z
dS)�WanVideoGeneratorzO6    Video generation class that integrates with Wan 2.1 or similar models7    �Wan-AI/Wan2.1-T2V-14Bc�~�||_d|_tj���rdnd|_d|_dS)N�cuda�cpuF)�8model_path�pipe�torchr	�is_available�device�model_loaded)�selfrs  �7/home/ubuntu/looping-video-generator/video_generator.py�__init__zWanVideoGenerator.__init__s<��$�����	� %�9� 7� 7� 9� 9�D�f�f�u���!�����c�:�	t�d|j�d|j����ddl}|�d��d|_t�d��dS#t$r(}t�d|����Yd}~d	Sd}~wwxYw)10zLoad the video generation modelzLoading model z on rN�TzModel loaded successfullyzFailed to load model: F)	�logger�inforr�time�sleepr�	Exception�error)rr�es   r�11load_modelzWanVideoGenerator.load_models���	��K�K�K���K�K�d�k�K�K�L�L�L�
�K�K�K��J�J�q�M�M�M� $�D���K�K�3�4�4�4��4���	�	�	��L�L�5�!�5�5�6�6�6��5�5�5�5�5�����	���s�A$A(�(12B�2B�B��720pNc��	|jstd���d}|�||z}|dkrd\}}nd\}}t�d|�d|�d	|����|�||||��}d13t|��dz�d�}	|�||	|��|	S#t$r(}14t�d
|15����Yd}16~17dSd}18~19wwxYw)aq20        Generate video from text prompt21        22        Args:23            prompt (str): Text description of the video24            duration (int): Duration in seconds25            resolution (str): Video resolution (480p or 720p)26            num_frames (int): Number of frames to generate27        28        Returns:29            str: Path to generated video file30        �Model not loaded�Nr �ii��i@i�zGenerating z frames at �xzstatic/uploads/video_�'�.mp4z!Text-to-video generation failed: )rrrr�_generate_demo_frames�hash�_frames_to_videor)r�prompt�duration�31resolution�32num_frames�fps�width�height�frames�output_pathrs           r�
text_to_videozWanVideoGenerator.text_to_video0s%��	��$�
4�� 2�3�3�3��C��
,�%��^�33��V�#�
)� )�
��v�v� (�
��v��K�K�M�j�M�M�U�M�M�V�M�M�N�N�N��/�/��34�E�6�R�R�F�M�$�v�,�,��2F�L�L�L�K��!�!�&�+�s�;�;�;�����	�	�	��L�L�@�Q�@�@�A�A�A��4�4�4�4�4�����	���s�BB�35C�&C	�	C�c��	|jstd���tj|��}|dkrd\}}nd\}}|�||ftjj��}d}||z}	t�d|����|�	|||	��}36dt||z��dz�d	�}|�|37||��|S#t$r(}t�d38|����Yd}~dSd}~wwxYw)ai39        Generate video from image with optional text prompt40        41        Args:42            image_path (str): Path to input image43            prompt (str): Optional text description44            duration (int): Duration in seconds45            resolution (str): Video resolution46        47        Returns:48            str: Path to generated video file49        r"r r$r%r#zGenerating video from image: zstatic/uploads/i2v_r'r(z"Image-to-video generation failed: N)
rrr�open�resize�50Resampling�LANCZOSrr� _generate_demo_frames_from_imager*r+r)
r�51image_pathr,r-r.�imager1r2r0r/r3r4rs
             r�image_to_videoz WanVideoGenerator.image_to_video\sB��"	��$�
4�� 2�3�3�3��J�z�*�*�E��V�#�
)� )�
��v�v� (�
��v��L�L�%���%�2B�2J�K�K�E��C�!�C��J��K�K�D�52�D�D�E�E�E��:�:�5�&�*�U�U�F�X��Z�&�5H�0I�0I�E�0Q�W�W�W�K��!�!�&�+�s�;�;�;�����	�	�	��L�L�A�a�A�A�B�B�B��4�4�4�4�4�����	���s�CC�53D�C>�>D�seamlessc	���	t�d|����tj|��}|dkr�|�d��}|�|jd|jzz54��}|�||d���}tj||j���}tj	||g��}n�|dkr;|�55tjj��}	tj	||	g��}nk|d	krcd56}57|�58tjj
|59��}|�60tjj|61��}tj	||g��}n|}dt|��dz�d
�}
|�|
dddddd���|���|���|
S#t$$r(}t�d|����Yd}~dSd}~wwxYw)z�62        Create a seamless looping video63        64        Args:65            video_path (str): Path to input video66            loop_method (str): Method for creating loop67        68        Returns:69            str: Path to looped video file70        zCreating seamless loop for: r@r��)r/)r0�bounce�fadeg�?zstatic/uploads/loop_r'r(�libx264�aacztemp-audio.m4aTFN)�codec�audio_codec�temp_audiofile�remove_temp�verboserzLoop creation failed: )rr�mp�
VideoFileClip�	get_framer-r0�_generate_transition_frames�ImageSequenceClip�concatenate_videoclips�fx�vfx�time_mirror�fadeout�fadeinr*�write_videofile�closerr)r�71video_path�loop_method�clip�first_frame�72last_frame�transition_frames�transition_clip�looped_clip�
reversed_clip�
fade_duration�fade_out�fade_inr4rs               r�create_seamless_loopz&WanVideoGenerator.create_seamless_loop�s ��:	��K�K�C�z�C�C�D�D�D��#�J�/�/�D��j�(�
#�"�n�n�Q�/�/��!�^�^�D�M�A�d�h�J�,F�G�G�73�%)�$D�$D����%E�%�%�!�74#%�"6�7H�d�h�"W�"W�"W��!�7���8O�P�P�����(�
#� $�����(:� ;� ;�
� �7��}�8M�N�N�����&�	
#� #�
��7�7�2�6�>�=�A�A���'�'�"�&�-��?�?�� �7��7�8K�L�L���#��P��j�1A�1A�E�1I�O�O�O�K��'�'���!�/� ���
(�
�
�
�
�J�J�L�L�L�����������	�	�	��L�L�5�!�5�5�6�6�6��4�4�4�4�4�����	���s�GG�75G7�G2�2G7c76���g}|�|��}t|��D�]J}tj||dftj���}t|��D]G}	t|dd|	|zz77z|d|	|zzz��}78|79|d|dg||	dd�f<�H|dztdtjdtjz|z|z��z��z}|dztdtjdtjz|z|z��z��z}tj80|||fdd	d81��tj|ddtjdd	d��|�
|����L|S)
zCGenerate demo frames for text-to-video (placeholder implementation)�)�dtyperrBrN�2�)��rlrl�����z82Demo Video)rjrj)�_get_color_from_prompt�range�np�zeros�uint8�int�sin�pi�cos�cv2�circle�putText�FONT_HERSHEY_SIMPLEX�append)
rr,r/r1r2r3�83base_color�i�frame�y�	intensity�center_x�center_ys
             rr)z'WanVideoGenerator._generate_demo_frames�s������0�0��8�8�84��z�"�"�	!�	!�A��H�f�e�Q�/�r�x�@�@�@�E��6�]�]�
H�
H���85�1�
��Q��Z�� @�:�a�=�TU�X^�T^�C_� _�`�`�	�(�*�Q�-��A��G��a����d�����z�C��R�V�A���I��M�J�4N�-O�-O�(O�$P�$P�P�H���{�S��b�f�Q���Y��]�Z�5O�.P�.P�)P�%Q�%Q�Q�H��J�u�x��2�B���L�L�L�
�K��|�X�s�7O��/�1�
.�
.�
.�
�M�M�%� � � � ��
rc��g}tj|��}t|��D]�}|���}ddtjdtjz|z|z��zz}|jdd�\}	}86|87dz|	dz}}t|88|z��}
t|	|z��}|dkr;tj	||
|f��}|
|89z90dz}||	z91dz}||||	z�|||92z�f}|�93|����|S)zDGenerate demo frames for image-to-video (placeholder implementation)g�?g�������?rN)rp�arrayro�copyrtru�shapersrwr9r{)rr>r,r/r3�image_arrayr}r~�zoom_factor�h�wr�r��new_w�new_h�resized�start_x�start_ys                  rr<z2WanVideoGenerator._generate_demo_frames_from_image�s,�����h�u�o�o���z�"�"�	!�	!�A��$�$�&�&�E���r�v�a�"�%�i�!�m�j�.H�'I�'I� I�I�K��;�r��r�?�D�A�q�!"�a���a��h�H���K��(�(�E���K��(�(�E��S� �
F��*�U�U�E�N�;�;�� �1�9��*�� �1�9��*������	� 1�7�7�1�9�3D� D�E���M�M�%� � � � ��
rrCc��g}t|��D]J}||dz94z}d|z95|z||zz}|�|�tj�����K|S)z-Generate transition frames between two framesrB)ror{�astyperprr)r�start_frame�	end_framer/r3r}�alpha�transition_frames        rrPz-WanVideoGenerator._generate_transition_framessp�����z�"�"�	=�	=�A���a��(�E� !�E�	�[�8�5�9�;L�L���M�M�*�1�1�"�(�;�;�<�<�<�<��
rc�,��|����t�fd�dD����rgd�St�fd�dD����rgd�St�fd�dD����rgd	�St�fd96�dD����rgd�Sgd
�S)z2Extract color theme from prompt (simple heuristic)c3� �K�|]}|�vV��	dS�N���.0�word�prompt_lowers  �r�	<genexpr>z;WanVideoGenerator._get_color_from_prompt.<locals>.<genexpr> s(�����M�M��t�|�#�M�M�M�M�M�Mr)�sunset�orange�warm)rl�rc3� �K�|]}|�vV��	dSr�r�r�s  �rr�z;WanVideoGenerator._get_color_from_prompt.<locals>.<genexpr>"s(�����M�M�$���%�M�M�M�M�M�Mr)�ocean�blue�water)r�drlc3� �K�|]}|�vV��	dSr�r�r�s  �rr�z;WanVideoGenerator._get_color_from_prompt.<locals>.<genexpr>$s(�����P�P�$���%�P�P�P�P�P�Pr)�forest�green�nature)rrlr�c3� �K�|]}|�vV��	dSr�r�r�s  �rr�z;WanVideoGenerator._get_color_from_prompt.<locals>.<genexpr>&s(�����I�I�$���%�I�I�I�I�I�Ir)�fire�red�hot)rlrjrj)�r�rl)�lower�any)rr,r�s  @rrnz(WanVideoGenerator._get_color_from_prompts�����|�|�~�~���M�M�M�M�0L�M�M�M�M�M�		#� �=�=� �
�M�M�M�M�2L�M�M�M�
M�
M�	#� �=�=� �
�P�P�P�P�2O�P�P�P�
P�
P�	#� �=�=� �
�I�I�I�I�2H�I�I�I�
I�
I�	#� �=�=� �"�?�?�"rr#c��|std���|djdd�\}}tjd�}tj|||||f��}|D]Q}t|j��dkr tj|tj��}	n|}	|�|	���R|�	��t�d|����dS)z Convert frame list to video filezNo frames to convertrNr�mp4vrhzVideo saved to: )�97ValueErrorr�rw�VideoWriter_fourcc�VideoWriter�len�cvtColor�
COLOR_RGB2BGR�write�releaserr)98rr3r4r0r2r1�fourcc�outr~�	frame_bgrs99          rr+z"WanVideoGenerator._frames_to_video+s����	5��3�4�4�4��q�	�����+�
����'��0���o�k�6�3����H�H���	!�	!�E��5�;���1�$�
"��L���0A�B�B�	�	�!�	��I�I�i� � � � �	���
�
�
����4�{�4�4�5�5�5�5�5r)r)rr N)r6rr )r@)rC)r#)�__name__�100__module__�__qualname__�__doc__rrr5r?rfr)r<rPrnr+r�rrrrs���������"�"�"�"����2*�*�*�*�X/�/�/�/�bE�E�E�E�N���:���>101�102�103�104�
#�
#�
#�6�6�6�6�6�6rr)r
�numpyrp�	diffusersrrw�PILr�moviepy.editor�editorrM�os�logging�	getLoggerr�rrr�rr�<module>r�s�����������'�'�'�'�'�'�105106107108�������������	�	�	�	�����	��	�8�	$�	$��w6�w6�w6�w6�w6�w6�w6�w6�w6�w6r