Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,6 +35,7 @@ def get_video_dimension(filepath):
|
|
| 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 |
|
|
@@ -61,9 +62,45 @@ def resize_video(input_vid, output_vid, width, height, fps):
|
|
| 61 |
# Release the video objects
|
| 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 |
|
|
@@ -164,6 +201,9 @@ def run_inference(prompt, video_path, condition, video_length):
|
|
| 164 |
# Resize the video
|
| 165 |
resized = resize_video(video_path, 'resized.mp4', 512, 512, target_fps)
|
| 166 |
|
|
|
|
|
|
|
|
|
|
| 167 |
output_path = 'output/'
|
| 168 |
os.makedirs(output_path, exist_ok=True)
|
| 169 |
|
|
@@ -171,16 +211,18 @@ def run_inference(prompt, video_path, condition, video_length):
|
|
| 171 |
if os.path.exists(os.path.join(output_path, f"result.mp4")):
|
| 172 |
# Delete the existing file
|
| 173 |
os.remove(os.path.join(output_path, f"result.mp4"))
|
| 174 |
-
|
|
|
|
| 175 |
if video_length > 12:
|
| 176 |
-
command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{
|
| 177 |
else:
|
| 178 |
-
command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{
|
| 179 |
subprocess.run(command, shell=True)
|
| 180 |
|
| 181 |
# Construct the video path
|
| 182 |
video_path_output = os.path.join(output_path, f"result.mp4")
|
| 183 |
|
|
|
|
| 184 |
return "done", video_path_output
|
| 185 |
|
| 186 |
|
|
|
|
| 35 |
return width, height, fps, frame_count
|
| 36 |
|
| 37 |
def resize_video(input_vid, output_vid, width, height, fps):
|
| 38 |
+
print(f"RESIZING ...")
|
| 39 |
# Open the input video file
|
| 40 |
video = cv2.VideoCapture(input_vid)
|
| 41 |
|
|
|
|
| 62 |
# Release the video objects
|
| 63 |
video.release()
|
| 64 |
output_video.release()
|
| 65 |
+
print(f"RESIZE VIDEO DONE!")
|
| 66 |
return output_vid
|
| 67 |
|
| 68 |
+
def normalize_and_save_video(input_video_path, output_video_path):
|
| 69 |
+
print(f"NORMALIZING ...")
|
| 70 |
+
cap = cv2.VideoCapture(input_video_path)
|
| 71 |
+
|
| 72 |
+
# Get video properties
|
| 73 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 74 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 75 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 76 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 77 |
+
|
| 78 |
+
# Create VideoWriter object to save the normalized video
|
| 79 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Specify the codec (e.g., 'mp4v', 'XVID', 'MPEG')
|
| 80 |
+
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
| 81 |
+
|
| 82 |
+
# Iterate through each frame in the video
|
| 83 |
+
for _ in range(frame_count):
|
| 84 |
+
ret, frame = cap.read()
|
| 85 |
+
if not ret:
|
| 86 |
+
break
|
| 87 |
+
|
| 88 |
+
# Convert frame to floating point
|
| 89 |
+
frame = frame.astype(np.float32)
|
| 90 |
+
|
| 91 |
+
# Normalize pixel values to the range [0, 1]
|
| 92 |
+
frame /= 255.0
|
| 93 |
+
|
| 94 |
+
# Write the normalized frame to the output video file
|
| 95 |
+
out.write(frame)
|
| 96 |
+
|
| 97 |
+
# Release the VideoCapture and VideoWriter objects
|
| 98 |
+
cap.release()
|
| 99 |
+
out.release()
|
| 100 |
+
|
| 101 |
+
print(f"NORMALIZE DONE!")
|
| 102 |
+
return output_video_path
|
| 103 |
+
|
| 104 |
def chunkify(video_path, fps, nb_frames):
|
| 105 |
chunks_array = []
|
| 106 |
|
|
|
|
| 201 |
# Resize the video
|
| 202 |
resized = resize_video(video_path, 'resized.mp4', 512, 512, target_fps)
|
| 203 |
|
| 204 |
+
# normalize pixels
|
| 205 |
+
normalized = normalize_and_save_video(resized, 'normalized.mp4')
|
| 206 |
+
|
| 207 |
output_path = 'output/'
|
| 208 |
os.makedirs(output_path, exist_ok=True)
|
| 209 |
|
|
|
|
| 211 |
if os.path.exists(os.path.join(output_path, f"result.mp4")):
|
| 212 |
# Delete the existing file
|
| 213 |
os.remove(os.path.join(output_path, f"result.mp4"))
|
| 214 |
+
|
| 215 |
+
print(f"RUNNING INFERENCE ...")
|
| 216 |
if video_length > 12:
|
| 217 |
+
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"
|
| 218 |
else:
|
| 219 |
+
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}"
|
| 220 |
subprocess.run(command, shell=True)
|
| 221 |
|
| 222 |
# Construct the video path
|
| 223 |
video_path_output = os.path.join(output_path, f"result.mp4")
|
| 224 |
|
| 225 |
+
print(f"FINISHED !")
|
| 226 |
return "done", video_path_output
|
| 227 |
|
| 228 |
|