Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,10 @@ def get_frame_count(filepath):
|
|
| 24 |
video = cv2.VideoCapture(filepath)
|
| 25 |
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 26 |
video.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return gr.update(maximum=frame_count)
|
| 28 |
|
| 29 |
def get_video_dimension(filepath):
|
|
@@ -105,98 +109,15 @@ def normalize_and_save_video(input_video_path, output_video_path):
|
|
| 105 |
print(f"NORMALIZE DONE!")
|
| 106 |
return output_video_path
|
| 107 |
|
| 108 |
-
def chunkify(video_path, fps, nb_frames):
|
| 109 |
-
chunks_array = []
|
| 110 |
-
|
| 111 |
-
video_capture = cv2.VideoCapture(video_path)
|
| 112 |
-
chunk_start_frame = 0
|
| 113 |
-
frames_per_chunk = 12
|
| 114 |
-
|
| 115 |
-
while chunk_start_frame < nb_frames:
|
| 116 |
-
chunk_end_frame = min(chunk_start_frame + frames_per_chunk, nb_frames)
|
| 117 |
-
|
| 118 |
-
video_capture.set(cv2.CAP_PROP_POS_FRAMES, chunk_start_frame)
|
| 119 |
-
success, frame = video_capture.read()
|
| 120 |
-
if not success:
|
| 121 |
-
break
|
| 122 |
-
|
| 123 |
-
chunk_name = f"chunk_{chunk_start_frame}-{chunk_end_frame}.mp4"
|
| 124 |
-
chunk_video = cv2.VideoWriter(chunk_name, cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame.shape[1], frame.shape[0]))
|
| 125 |
-
|
| 126 |
-
for frame_number in range(chunk_start_frame, chunk_end_frame):
|
| 127 |
-
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
| 128 |
-
success, frame = video_capture.read()
|
| 129 |
-
if not success:
|
| 130 |
-
break
|
| 131 |
-
|
| 132 |
-
chunk_video.write(frame)
|
| 133 |
-
|
| 134 |
-
chunk_video.release()
|
| 135 |
-
chunks_array.append(chunk_name)
|
| 136 |
-
|
| 137 |
-
chunk_start_frame += frames_per_chunk
|
| 138 |
-
|
| 139 |
-
video_capture.release()
|
| 140 |
-
print(f"CHUNKS: {chunks_array}")
|
| 141 |
-
return chunks_array
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
def run_inference_by_chunkify(prompt, video_path, condition, video_length):
|
| 145 |
-
|
| 146 |
-
# DOESN'T WORK
|
| 147 |
-
|
| 148 |
-
# Get FPS of original video input
|
| 149 |
-
target_fps = get_video_dimension(video_path)[2]
|
| 150 |
-
print(f"INPUT FPS: {target_fps}")
|
| 151 |
-
|
| 152 |
-
# Count total frames according to fps
|
| 153 |
-
total_frames = get_video_dimension(video_path)[3]
|
| 154 |
-
|
| 155 |
-
# Resize the video
|
| 156 |
-
resized = resize_video(video_path, 'resized.mp4', 512, 512, target_fps)
|
| 157 |
-
|
| 158 |
-
# Chunkify the video into 12 frames chunks
|
| 159 |
-
chunks = chunkify(resized, target_fps, total_frames)
|
| 160 |
-
|
| 161 |
-
output_path = 'output/'
|
| 162 |
-
os.makedirs(output_path, exist_ok=True)
|
| 163 |
-
|
| 164 |
-
processed_chunks = []
|
| 165 |
|
| 166 |
-
for index, chunk_path in enumerate(chunks):
|
| 167 |
-
if index == 0 :
|
| 168 |
-
print(f"Chunk #{index}: {chunk_path}")
|
| 169 |
-
|
| 170 |
-
# Check if the file already exists
|
| 171 |
-
if os.path.exists(os.path.join(output_path, f"{index}.mp4")):
|
| 172 |
-
# Delete the existing file
|
| 173 |
-
os.remove(os.path.join(output_path, f"{index}.mp4"))
|
| 174 |
-
|
| 175 |
-
#if video_length > 12:
|
| 176 |
-
# 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"
|
| 177 |
-
#else:
|
| 178 |
-
command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{chunk_path}' --output_path '{output_path}' --temp_chunk_path '{index}' --width 512 --height 512 --fps 8 --video_length {video_length} --is_long_video"
|
| 179 |
-
subprocess.run(command, shell=True)
|
| 180 |
-
|
| 181 |
-
# Construct the video path
|
| 182 |
-
video_path_output = os.path.join(output_path, f"{index}.mp4")
|
| 183 |
-
|
| 184 |
-
# Append processed chunk to final array
|
| 185 |
-
processed_chunks.append(video_path_output)
|
| 186 |
-
else:
|
| 187 |
-
print("finished")
|
| 188 |
-
|
| 189 |
-
print(f"PROCESSED CHUNKS: {processed_chunks}")
|
| 190 |
-
|
| 191 |
-
return "done", processed_chunks[0]
|
| 192 |
|
| 193 |
-
|
| 194 |
-
def run_inference(prompt, video_path, condition, video_length):
|
| 195 |
-
|
| 196 |
-
|
| 197 |
|
| 198 |
# Get FPS of original video input
|
| 199 |
target_fps = get_video_dimension(video_path)[2]
|
|
|
|
|
|
|
|
|
|
| 200 |
print(f"INPUT FPS: {target_fps}")
|
| 201 |
|
| 202 |
# Count total frames according to fps
|
|
@@ -218,16 +139,21 @@ def run_inference(prompt, video_path, condition, video_length):
|
|
| 218 |
|
| 219 |
print(f"RUNNING INFERENCE ...")
|
| 220 |
if video_length > 12:
|
| 221 |
-
command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{normalized}' --output_path '{output_path}' --temp_chunk_path 'result' --width 512 --height 512 --fps {target_fps} --video_length {video_length} --is_long_video"
|
| 222 |
else:
|
| 223 |
-
command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{normalized}' --output_path '{output_path}' --temp_chunk_path 'result' --width 512 --height 512 --fps {target_fps} --video_length {video_length}"
|
| 224 |
subprocess.run(command, shell=True)
|
| 225 |
|
| 226 |
# Construct the video path
|
| 227 |
video_path_output = os.path.join(output_path, f"result.mp4")
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
print(f"FINISHED !")
|
| 230 |
-
return "done",
|
| 231 |
|
| 232 |
|
| 233 |
|
|
@@ -244,10 +170,10 @@ with gr.Blocks(css=css) as demo:
|
|
| 244 |
#video_in = gr.Video(source="upload", type="filepath", visible=True)
|
| 245 |
video_path = gr.Video(source="upload", type="filepath", visible=True)
|
| 246 |
prompt = gr.Textbox(label="prompt")
|
| 247 |
-
with gr.
|
| 248 |
condition = gr.Dropdown(label="Condition", choices=["depth", "canny", "pose"], value="depth")
|
| 249 |
video_length = gr.Slider(label="Video length", info="How many frames do you want to process ?", minimum=1, maximum=12, step=1, value=2)
|
| 250 |
-
|
| 251 |
submit_btn = gr.Button("Submit")
|
| 252 |
with gr.Column():
|
| 253 |
video_res = gr.Video(label="result")
|
|
@@ -260,7 +186,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 260 |
inputs=[prompt,
|
| 261 |
video_path,
|
| 262 |
condition,
|
| 263 |
-
video_length
|
|
|
|
| 264 |
],
|
| 265 |
outputs=[status, video_res])
|
| 266 |
|
|
|
|
| 24 |
video = cv2.VideoCapture(filepath)
|
| 25 |
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 26 |
video.release()
|
| 27 |
+
|
| 28 |
+
#LIMITS
|
| 29 |
+
if frame_count > 12 :
|
| 30 |
+
frame_count = 24 # limit to 24 frames to avoid cuDNN errors
|
| 31 |
return gr.update(maximum=frame_count)
|
| 32 |
|
| 33 |
def get_video_dimension(filepath):
|
|
|
|
| 109 |
print(f"NORMALIZE DONE!")
|
| 110 |
return output_video_path
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
def run_inference(prompt, video_path, condition, video_length, seed):
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# Get FPS of original video input
|
| 117 |
target_fps = get_video_dimension(video_path)[2]
|
| 118 |
+
if target_fps > 12 :
|
| 119 |
+
print(f"FPS is too high")
|
| 120 |
+
target_fps = 12
|
| 121 |
print(f"INPUT FPS: {target_fps}")
|
| 122 |
|
| 123 |
# Count total frames according to fps
|
|
|
|
| 139 |
|
| 140 |
print(f"RUNNING INFERENCE ...")
|
| 141 |
if video_length > 12:
|
| 142 |
+
command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{normalized}' --output_path '{output_path}' --temp_chunk_path 'result' --width 512 --height 512 --fps {target_fps} --seed {seed} --video_length {video_length} --is_long_video"
|
| 143 |
else:
|
| 144 |
+
command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{normalized}' --output_path '{output_path}' --temp_chunk_path 'result' --width 512 --height 512 --fps {target_fps} --seed {seed} --video_length {video_length}"
|
| 145 |
subprocess.run(command, shell=True)
|
| 146 |
|
| 147 |
# Construct the video path
|
| 148 |
video_path_output = os.path.join(output_path, f"result.mp4")
|
| 149 |
|
| 150 |
+
# Resize to original video input size
|
| 151 |
+
o_width = get_video_dimension(video_path)[0]
|
| 152 |
+
o_height = get_video_dimension(video_path)[1]
|
| 153 |
+
resize_video(video_path_output, 'resized_final.mp4', o_width, o_height, target_fps)
|
| 154 |
+
|
| 155 |
print(f"FINISHED !")
|
| 156 |
+
return "done", 'resized_final.mp4'
|
| 157 |
|
| 158 |
|
| 159 |
|
|
|
|
| 170 |
#video_in = gr.Video(source="upload", type="filepath", visible=True)
|
| 171 |
video_path = gr.Video(source="upload", type="filepath", visible=True)
|
| 172 |
prompt = gr.Textbox(label="prompt")
|
| 173 |
+
with gr.Column():
|
| 174 |
condition = gr.Dropdown(label="Condition", choices=["depth", "canny", "pose"], value="depth")
|
| 175 |
video_length = gr.Slider(label="Video length", info="How many frames do you want to process ?", minimum=1, maximum=12, step=1, value=2)
|
| 176 |
+
seed = gr.Number(label="seed", value=42)
|
| 177 |
submit_btn = gr.Button("Submit")
|
| 178 |
with gr.Column():
|
| 179 |
video_res = gr.Video(label="result")
|
|
|
|
| 186 |
inputs=[prompt,
|
| 187 |
video_path,
|
| 188 |
condition,
|
| 189 |
+
video_length,
|
| 190 |
+
seed
|
| 191 |
],
|
| 192 |
outputs=[status, video_res])
|
| 193 |
|