File size: 7,452 Bytes
653ce35
7ca5351
c93a0cb
 
 
 
454eedf
39489bd
0cbec0b
acf84db
 
 
 
 
 
 
 
 
 
3a66e37
 
12c01c3
c05f3f8
 
12c01c3
 
 
 
c05f3f8
48ed28b
3a66e37
833e264
 
 
 
65a9a4b
28ef21d
833e264
28ef21d
833e264
449e320
 
 
 
 
 
 
f103a90
de0aaee
449e320
e021aac
449e320
f103a90
449e320
 
f103a90
449e320
f557c03
de0aaee
9f98966
 
 
 
 
 
 
 
 
013210c
e908561
013210c
e908561
013210c
d4f2f5c
de0aaee
9f98966
 
 
 
 
 
 
0be744f
9f98966
 
 
90e6ba4
9f98966
 
 
 
 
11cb0d7
e908561
98aad5a
e908561
52b6321
16af56d
e57581d
618e51c
6fcb174
 
6a87ed0
c93a0cb
cc3607e
6a87ed0
ba65f56
 
 
 
 
 
4cba54c
ba65f56
4cba54c
ba65f56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a87ed0
 
 
 
07f7f7b
618e51c
65a9a4b
0479125
65a9a4b
de1d7d7
c93a0cb
de1d7d7
 
c93a0cb
de1d7d7
454eedf
de1d7d7
454eedf
65a9a4b
e23155a
65a9a4b
 
6a046f7
65a9a4b
 
 
b5a5c95
a2a8df5
48ed28b
 
bce3142
 
 
 
 
 
 
 
 
48ed28b
cad53bd
48ed28b
98aad5a
39489bd
7ca5351
0269ee9
 
39489bd
0269ee9
eaf8a3c
106f93a
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import gradio as gr
import os
import subprocess
import cv2
from moviepy.editor import VideoFileClip, concatenate_videoclips
import math

from huggingface_hub import snapshot_download

model_ids = [
    'runwayml/stable-diffusion-v1-5',
    'lllyasviel/sd-controlnet-depth', 
    'lllyasviel/sd-controlnet-canny', 
    'lllyasviel/sd-controlnet-openpose',
]
for model_id in model_ids:
    model_name = model_id.split('/')[-1]
    snapshot_download(model_id, local_dir=f'checkpoints/{model_name}')



def get_frame_count_in_duration(filepath):
    video = cv2.VideoCapture(filepath)
    fps = video.get(cv2.CAP_PROP_FPS)
    frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    duration = frame_count / fps
    width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    video.release()
    return gr.update(visible=False), gr.update(visible=True), gr.update(maximum=frame_count)

def get_video_dimension(filepath):
    video = cv2.VideoCapture(filepath)
    width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(video.get(cv2.CAP_PROP_FPS))
    frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    video.release()
    return width, height, fps, frame_count

def adjust_to_multiple_of_12(number):
    remainder = number % 12
    if remainder != 0:
        adjustment = 12 - remainder
        number += adjustment
    return number
    
def resize_video(input_file):
    # Load the video clip
    clip = VideoFileClip(input_file)
    print(f"WIDTH TARGET: 512")
    # Calculate the aspect ratio
    ratio = 512 / clip.size[0]
    new_height = int(clip.size[1] * ratio)
    new_height_adjusted = adjust_to_multiple_of_12(new_height)
    new_width_adjusted = adjust_to_multiple_of_12(512)
    print(f"OLD H: {new_height} | NEW H: {new_height_adjusted}")
    print(f"OLD W: 512 | NEW W: {new_width_adjusted}")

    # Close the video clip
    clip.close()

    # Open the input video file
    video = cv2.VideoCapture(input_file)

    # Create a VideoWriter object to write the resized video
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # Codec for the output video
   
    # Check if the file already exists
    if os.path.exists('video_resized.mp4'):
        # Delete the existing file
        os.remove('video_resized.mp4')
        
    output_video = cv2.VideoWriter('video_resized.mp4', fourcc, 8.0, (512, 512))

    while True:
        # Read a frame from the input video
        ret, frame = video.read()
        if not ret:
            break

        # Resize the frame to the desired dimensions
        resized_frame = cv2.resize(frame, (512, 512))

        # Write the resized frame to the output video file
        output_video.write(resized_frame)

    # Release the video objects
    video.release()
    output_video.release()

    
    
    #final_video_resized = os.path.join(temp_output_path, 'video_resized.mp4')
    test_w, test_h, fps, frame_count = get_video_dimension('video_resized.mp4')
    print(f"resized clip dims : {test_w}, {test_h}, {fps}")
    return gr.update(visible=False), gr.update(value='video_resized.mp4', visible=True), gr.update(maximum=frame_count)
        
def run_inference(prompt, video_path, condition, video_length):

    output_path = 'output/'
    os.makedirs(output_path, exist_ok=True)

    # Construct the final video path
    video_path_output = os.path.join(output_path, f"{prompt}.mp4")

    # Check if the file already exists
    if os.path.exists(video_path_output):
        # Delete the existing file
        os.remove(video_path_output)

    if video_length > 12:
        command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --width 512 --height 512 --fps 8 --video_length {video_length} --is_long_video"
    else:
        command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --width 512 --height 512 --fps 8 --video_length {video_length}"
    subprocess.run(command, shell=True)

    # Construct the video path
    video_path_output = os.path.join(output_path, f"{prompt}.mp4")

        

    return "done", video_path_output 

def run_inference_chunks(prompt, video_path, condition, video_length):

    # Specify the input and output paths
    input_vid = video_path
    resized_vid = 'resized.mp4'

    # Call the function to resize the video
    video_path = resize_video(input_vid, resized_vid, width=512)
    width, height, fps = get_video_dimension(video_path)

    print(f"{width} x {height} | {fps}")

    # Split the video into chunks mp4 of 12 frames at video fps
    # Store chunks as mp4 paths in an array

    # For each mp4 chunks in chunks arrays, run command
    # store video result in processed chunks array
    
    output_path = 'output/'
    os.makedirs(output_path, exist_ok=True)

    # Construct the final video path
    video_path_output = os.path.join(output_path, f"{prompt}.mp4")

    # Check if the file already exists
    if os.path.exists(video_path_output):
        # Delete the existing file
        os.remove(video_path_output)

    if video_length > 12:
        command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --width {width} --height {height} --fps {fps} --video_length {video_length} --is_long_video"
    else:
        command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --width {width} --height {height} --fps {fps} --video_length {video_length}"
    subprocess.run(command, shell=True)

    # Construct the video path
    video_path_output = os.path.join(output_path, f"{prompt}.mp4")

        

    return "done", video_path_output 

css="""
#col-container {max-width: 810px; margin-left: auto; margin-right: auto;}
"""
with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("""
            <h1 style="text-align: center;">ControlVideo</h1>
        """)
        with gr.Row():
            with gr.Column():
                video_in = gr.Video(source="upload", type="filepath", visible=True)
                video_path = gr.Video(source="upload", type="filepath", visible=False)
                prompt = gr.Textbox(label="prompt")
                with gr.Row():
                    condition = gr.Dropdown(label="Condition", choices=["depth", "canny", "pose"], value="depth")
                    video_length = gr.Slider(label="Video length", info="How many frames do you want to process ?", minimum=1, maximum=12, step=1, value=2)
                #seed = gr.Number(label="seed", value=42)
                submit_btn = gr.Button("Submit")
            with gr.Column():
                video_res = gr.Video(label="result")
                status = gr.Textbox(label="result")
    video_in.change(fn=resize_video,
                      inputs=[video_in],
                      outputs=[video_in, video_path, video_length]
                     )
    submit_btn.click(fn=run_inference, 
                     inputs=[prompt,
                             video_path,
                             condition,
                             video_length
                            ],
                    outputs=[status, video_res])

demo.queue(max_size=12).launch()