fffiloni commited on
Commit
1c8c6b0
·
1 Parent(s): d4f2f5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -83
app.py CHANGED
@@ -19,15 +19,11 @@ for model_id in model_ids:
19
 
20
 
21
 
22
- def get_frame_count_in_duration(filepath):
23
- video = cv2.VideoCapture(filepath)
24
- fps = video.get(cv2.CAP_PROP_FPS)
25
  frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
26
- duration = frame_count / fps
27
- width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
28
- height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
29
  video.release()
30
- return gr.update(visible=False), gr.update(visible=True), gr.update(maximum=frame_count)
31
 
32
  def get_video_dimension(filepath):
33
  video = cv2.VideoCapture(filepath)
@@ -38,40 +34,17 @@ def get_video_dimension(filepath):
38
  video.release()
39
  return width, height, fps, frame_count
40
 
41
- def adjust_to_multiple_of_12(number):
42
- remainder = number % 12
43
- if remainder != 0:
44
- adjustment = 12 - remainder
45
- number += adjustment
46
- return number
47
-
48
- def resize_video(input_file):
49
- # Load the video clip
50
- clip = VideoFileClip(input_file)
51
- print(f"WIDTH TARGET: 512")
52
- # Calculate the aspect ratio
53
- ratio = 512 / clip.size[0]
54
- new_height = int(clip.size[1] * ratio)
55
- new_height_adjusted = adjust_to_multiple_of_12(new_height)
56
- new_width_adjusted = adjust_to_multiple_of_12(512)
57
- print(f"OLD H: {new_height} | NEW H: {new_height_adjusted}")
58
- print(f"OLD W: 512 | NEW W: {new_width_adjusted}")
59
-
60
- # Close the video clip
61
- clip.close()
62
-
63
  # Open the input video file
64
- video = cv2.VideoCapture(input_file)
 
 
 
 
65
 
66
  # Create a VideoWriter object to write the resized video
67
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for the output video
68
-
69
- # Check if the file already exists
70
- if os.path.exists('video_resized.mp4'):
71
- # Delete the existing file
72
- os.remove('video_resized.mp4')
73
-
74
- output_video = cv2.VideoWriter('video_resized.mp4', fourcc, 8.0, (512, 512))
75
 
76
  while True:
77
  # Read a frame from the input video
@@ -80,7 +53,7 @@ def resize_video(input_file):
80
  break
81
 
82
  # Resize the frame to the desired dimensions
83
- resized_frame = cv2.resize(frame, (512, 512))
84
 
85
  # Write the resized frame to the output video file
86
  output_video.write(resized_frame)
@@ -89,56 +62,62 @@ def resize_video(input_file):
89
  video.release()
90
  output_video.release()
91
 
92
-
93
-
94
- #final_video_resized = os.path.join(temp_output_path, 'video_resized.mp4')
95
- test_w, test_h, fps, frame_count = get_video_dimension('video_resized.mp4')
96
- print(f"resized clip dims : {test_w}, {test_h}, {fps}")
97
- return gr.update(visible=False), gr.update(value='video_resized.mp4', visible=True), gr.update(maximum=frame_count)
98
-
99
- def run_inference(prompt, video_path, condition, video_length):
100
 
101
- output_path = 'output/'
102
- os.makedirs(output_path, exist_ok=True)
103
 
104
- # Construct the final video path
105
- video_path_output = os.path.join(output_path, f"{prompt}.mp4")
 
106
 
107
- # Check if the file already exists
108
- if os.path.exists(video_path_output):
109
- # Delete the existing file
110
- os.remove(video_path_output)
111
 
112
- if video_length > 12:
113
- 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"
114
- else:
115
- 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}"
116
- subprocess.run(command, shell=True)
117
 
118
- # Construct the video path
119
- video_path_output = os.path.join(output_path, f"{prompt}.mp4")
 
 
 
 
 
 
 
 
 
 
 
120
 
 
 
 
 
 
 
121
 
 
122
 
123
- return "done", video_path_output
 
 
 
 
 
124
 
125
- def run_inference_chunks(prompt, video_path, condition, video_length):
 
126
 
127
- # Specify the input and output paths
128
- input_vid = video_path
129
- resized_vid = 'resized.mp4'
130
 
131
- # Call the function to resize the video
132
- video_path = resize_video(input_vid, resized_vid, width=512)
133
- width, height, fps = get_video_dimension(video_path)
134
 
135
- print(f"{width} x {height} | {fps}")
136
 
137
- # Split the video into chunks mp4 of 12 frames at video fps
138
- # Store chunks as mp4 paths in an array
139
 
140
- # For each mp4 chunks in chunks arrays, run command
141
- # store video result in processed chunks array
142
 
143
  output_path = 'output/'
144
  os.makedirs(output_path, exist_ok=True)
@@ -152,18 +131,18 @@ def run_inference_chunks(prompt, video_path, condition, video_length):
152
  os.remove(video_path_output)
153
 
154
  if video_length > 12:
155
- 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"
156
  else:
157
- 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}"
158
  subprocess.run(command, shell=True)
159
 
160
  # Construct the video path
161
  video_path_output = os.path.join(output_path, f"{prompt}.mp4")
162
 
163
-
164
-
165
  return "done", video_path_output
166
 
 
 
167
  css="""
168
  #col-container {max-width: 810px; margin-left: auto; margin-right: auto;}
169
  """
@@ -174,8 +153,8 @@ with gr.Blocks(css=css) as demo:
174
  """)
175
  with gr.Row():
176
  with gr.Column():
177
- video_in = gr.Video(source="upload", type="filepath", visible=True)
178
- video_path = gr.Video(source="upload", type="filepath", visible=False)
179
  prompt = gr.Textbox(label="prompt")
180
  with gr.Row():
181
  condition = gr.Dropdown(label="Condition", choices=["depth", "canny", "pose"], value="depth")
@@ -185,9 +164,9 @@ with gr.Blocks(css=css) as demo:
185
  with gr.Column():
186
  video_res = gr.Video(label="result")
187
  status = gr.Textbox(label="result")
188
- video_in.change(fn=resize_video,
189
- inputs=[video_in],
190
- outputs=[video_in, video_path, video_length]
191
  )
192
  submit_btn.click(fn=run_inference,
193
  inputs=[prompt,
 
19
 
20
 
21
 
22
+ def get_frame_count(filepath):
23
+ video = cv2.VideoCapture(filepath)
 
24
  frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
 
 
 
25
  video.release()
26
+ return gr.update(maximum=frame_count)
27
 
28
  def get_video_dimension(filepath):
29
  video = cv2.VideoCapture(filepath)
 
34
  video.release()
35
  return width, height, fps, frame_count
36
 
37
+ def resize_video(input_vid, output_vid, width, height, fps):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Open the input video file
39
+ video = cv2.VideoCapture(input_vid)
40
+
41
+ # Get the original video's width and height
42
+ original_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
43
+ original_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
44
 
45
  # Create a VideoWriter object to write the resized video
46
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for the output video
47
+ output_video = cv2.VideoWriter(output_vid, fourcc, fps, (width, height))
 
 
 
 
 
 
48
 
49
  while True:
50
  # Read a frame from the input video
 
53
  break
54
 
55
  # Resize the frame to the desired dimensions
56
+ resized_frame = cv2.resize(frame, (width, height))
57
 
58
  # Write the resized frame to the output video file
59
  output_video.write(resized_frame)
 
62
  video.release()
63
  output_video.release()
64
 
65
+ return output_vid
 
 
 
 
 
 
 
66
 
67
+ def chunkify(video_path, fps, nb_frames):
68
+ chunks_array = []
69
 
70
+ video_capture = cv2.VideoCapture(video_path)
71
+ chunk_start_frame = 0
72
+ frames_per_chunk = 12
73
 
74
+ while chunk_start_frame < nb_frames:
75
+ chunk_end_frame = min(chunk_start_frame + frames_per_chunk, total_frames)
 
 
76
 
77
+ video_capture.set(cv2.CAP_PROP_POS_FRAMES, chunk_start_frame)
78
+ success, frame = video_capture.read()
79
+ if not success:
80
+ break
 
81
 
82
+ chunk_name = f"chunk_{chunk_start_frame}-{chunk_end_frame}.mp4"
83
+ chunk_video = cv2.VideoWriter(chunk_name, cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame.shape[1], frame.shape[0]))
84
+
85
+ for frame_number in range(chunk_start_frame, chunk_end_frame):
86
+ video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
87
+ success, frame = video_capture.read()
88
+ if not success:
89
+ break
90
+
91
+ chunk_video.write(frame)
92
+
93
+ chunk_video.release()
94
+ chunks_array.append(chunk_name)
95
 
96
+ chunk_start_frame += frames_per_chunk
97
+
98
+ video_capture.release()
99
+ print(f"CHUNKS: {chunks_array}")
100
+ return chunks_array
101
+
102
 
103
+ def run_inference(prompt, video_path, condition, video_length):
104
 
105
+ # Get FPS of original video input
106
+ target_fps = get_video_dimension(video_path)[2]
107
+ print(f"INPUT FPS: {target_fps}")
108
+
109
+ # Count total frames according to fps
110
+ total_frames = get_video_dimension(video_path)[3]
111
 
112
+ # Resize the video
113
+ resized = resize_video(video_path, 'resized.mp4', 512, 512, target_fps)
114
 
115
+ # Chunkify the video into 12 frames chunks
116
+ chunks = chunkify(resized, target_fps, total_frames)
 
117
 
 
 
 
118
 
 
119
 
 
 
120
 
 
 
121
 
122
  output_path = 'output/'
123
  os.makedirs(output_path, exist_ok=True)
 
131
  os.remove(video_path_output)
132
 
133
  if video_length > 12:
134
+ 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"
135
  else:
136
+ 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}"
137
  subprocess.run(command, shell=True)
138
 
139
  # Construct the video path
140
  video_path_output = os.path.join(output_path, f"{prompt}.mp4")
141
 
 
 
142
  return "done", video_path_output
143
 
144
+
145
+
146
  css="""
147
  #col-container {max-width: 810px; margin-left: auto; margin-right: auto;}
148
  """
 
153
  """)
154
  with gr.Row():
155
  with gr.Column():
156
+ #video_in = gr.Video(source="upload", type="filepath", visible=True)
157
+ video_path = gr.Video(source="upload", type="filepath", visible=True)
158
  prompt = gr.Textbox(label="prompt")
159
  with gr.Row():
160
  condition = gr.Dropdown(label="Condition", choices=["depth", "canny", "pose"], value="depth")
 
164
  with gr.Column():
165
  video_res = gr.Video(label="result")
166
  status = gr.Textbox(label="result")
167
+ video_path.change(fn=get_frame_count,
168
+ inputs=[video_path],
169
+ outputs=[video_length]
170
  )
171
  submit_btn.click(fn=run_inference,
172
  inputs=[prompt,