File size: 2,277 Bytes
3b6c254
e3c2cfd
1a76268
 
 
3b6c254
d15e784
 
 
 
3b6c254
 
d15e784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6c254
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import streamlit as st
import os
import torch
from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import load_image, export_to_video

# Function to get the list of images in a specified folder without extensions
def get_image_list(folder_path):
    return [os.path.splitext(f)[0] for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) and f.lower().endswith(('png', 'jpg', 'jpeg'))]

# Main function
def CREATEGIF():
    st.title("Create Photoshoot Visual Video")

    # Folder containing images
    image_folder = "Assets/output/images"

    # Get the list of images from the folder
    image_list = get_image_list(image_folder)

    # Selectbox to choose an image
    selected_image_name = st.selectbox("Select an image:", image_list)

    # Button to start the video generation
    if st.button("Generate Video"):
        # Full path of the selected image (adding back the extensions for loading the image)
        for ext in ['png', 'jpg', 'jpeg']:
            selected_image_path = os.path.join(image_folder, f"{selected_image_name}.{ext}")
            if os.path.exists(selected_image_path):
                break

        # Load the image and resize it
        image = load_image(selected_image_path)
        image = image.resize((1024, 576))

        # Spinner during video generation
        with st.spinner("Generating video..."):
            pipeline = StableVideoDiffusionPipeline.from_pretrained(
                "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
            )
            pipeline.enable_model_cpu_offload()

            generator = torch.manual_seed(42)
            frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]

            # Create output directory if it doesn't exist
            output_dir = "Assets/output/videos"
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)
            output_path = os.path.join(output_dir, "generated.mp4")
            export_to_video(frames, output_path, fps=7)

        # Display the generated video
        video_file = open(output_path, 'rb')
        video_bytes = video_file.read()
        st.video(video_bytes)

if __name__ == "__main__":
    CREATEGIF()