Spaces:
Paused
Paused
Commit
·
e5b16fd
1
Parent(s):
900b160
working on dynamic number of frames and multiple resolutions
Browse files- app.py +43 -22
- video_processor.py +24 -21
app.py
CHANGED
|
@@ -27,7 +27,7 @@ def init_video_processor():
|
|
| 27 |
return video_processor is not None
|
| 28 |
|
| 29 |
def extract_frames_from_video(video_path, output_dir, max_frames=81):
|
| 30 |
-
"""Extract frames from video and ensure we have at least
|
| 31 |
os.makedirs(output_dir, exist_ok=True)
|
| 32 |
|
| 33 |
reader = imageio.get_reader(video_path)
|
|
@@ -57,6 +57,8 @@ def generate_recammaster_video(
|
|
| 57 |
video_file,
|
| 58 |
text_prompt,
|
| 59 |
camera_type,
|
|
|
|
|
|
|
| 60 |
progress=gr.Progress()
|
| 61 |
):
|
| 62 |
"""Main function to generate video with ReCamMaster"""
|
|
@@ -79,17 +81,27 @@ def generate_recammaster_video(
|
|
| 79 |
input_video_path = os.path.join(temp_dir, "input.mp4")
|
| 80 |
shutil.copy(video_file, input_video_path)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
# Extract frames
|
| 83 |
progress(0.2, desc="Extracting video frames...")
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Process with ReCamMaster
|
| 88 |
progress(0.3, desc="Processing with ReCamMaster...")
|
| 89 |
output_video = video_processor.process_video(
|
| 90 |
input_video_path,
|
| 91 |
text_prompt,
|
| 92 |
-
camera_type
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
# Save output video
|
|
@@ -114,13 +126,14 @@ def generate_recammaster_video(
|
|
| 114 |
return None, f"Error: {str(e)}"
|
| 115 |
|
| 116 |
# Create Gradio interface
|
| 117 |
-
with gr.Blocks(title="ReCamMaster
|
| 118 |
|
| 119 |
gr.Markdown(f"""
|
| 120 |
-
# 🎥
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
Upload a video and select a camera transformation to see the magic!
|
| 124 |
""")
|
| 125 |
|
| 126 |
with gr.Row():
|
|
@@ -144,28 +157,36 @@ with gr.Blocks(title="ReCamMaster Demo") as demo:
|
|
| 144 |
value="1"
|
| 145 |
)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# Generate button
|
| 148 |
-
generate_btn = gr.Button("Generate Video", variant="primary")
|
| 149 |
|
| 150 |
with gr.Column():
|
| 151 |
# Output section
|
| 152 |
output_video = gr.Video(label="Output Video")
|
| 153 |
-
status_output = gr.Textbox(label="
|
| 154 |
-
|
| 155 |
-
# Example videos
|
| 156 |
-
gr.Markdown("### Example Videos")
|
| 157 |
-
gr.Examples(
|
| 158 |
-
examples=[
|
| 159 |
-
[f"{TEST_DATA_DIR}/videos/case0.mp4", "A person dancing", "1"],
|
| 160 |
-
[f"{TEST_DATA_DIR}/videos/case1.mp4", "A scenic view", "5"],
|
| 161 |
-
],
|
| 162 |
-
inputs=[video_input, text_prompt, camera_type],
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
# Event handlers
|
| 166 |
generate_btn.click(
|
| 167 |
fn=generate_recammaster_video,
|
| 168 |
-
inputs=[video_input, text_prompt, camera_type],
|
| 169 |
outputs=[output_video, status_output]
|
| 170 |
)
|
| 171 |
|
|
|
|
| 27 |
return video_processor is not None
|
| 28 |
|
| 29 |
def extract_frames_from_video(video_path, output_dir, max_frames=81):
|
| 30 |
+
"""Extract frames from video and ensure we have at least max_frames frames"""
|
| 31 |
os.makedirs(output_dir, exist_ok=True)
|
| 32 |
|
| 33 |
reader = imageio.get_reader(video_path)
|
|
|
|
| 57 |
video_file,
|
| 58 |
text_prompt,
|
| 59 |
camera_type,
|
| 60 |
+
num_frames,
|
| 61 |
+
resolution,
|
| 62 |
progress=gr.Progress()
|
| 63 |
):
|
| 64 |
"""Main function to generate video with ReCamMaster"""
|
|
|
|
| 81 |
input_video_path = os.path.join(temp_dir, "input.mp4")
|
| 82 |
shutil.copy(video_file, input_video_path)
|
| 83 |
|
| 84 |
+
# Parse resolution
|
| 85 |
+
width, height = map(int, resolution.split('x'))
|
| 86 |
+
|
| 87 |
# Extract frames
|
| 88 |
progress(0.2, desc="Extracting video frames...")
|
| 89 |
+
extracted_frames, fps = extract_frames_from_video(
|
| 90 |
+
input_video_path,
|
| 91 |
+
os.path.join(temp_dir, "frames"),
|
| 92 |
+
max_frames=num_frames
|
| 93 |
+
)
|
| 94 |
+
logger.info(f"Extracted {extracted_frames} frames at {fps} fps")
|
| 95 |
|
| 96 |
# Process with ReCamMaster
|
| 97 |
progress(0.3, desc="Processing with ReCamMaster...")
|
| 98 |
output_video = video_processor.process_video(
|
| 99 |
input_video_path,
|
| 100 |
text_prompt,
|
| 101 |
+
camera_type,
|
| 102 |
+
num_frames=num_frames,
|
| 103 |
+
height=height,
|
| 104 |
+
width=width
|
| 105 |
)
|
| 106 |
|
| 107 |
# Save output video
|
|
|
|
| 126 |
return None, f"Error: {str(e)}"
|
| 127 |
|
| 128 |
# Create Gradio interface
|
| 129 |
+
with gr.Blocks(title="ReCamMaster") as demo:
|
| 130 |
|
| 131 |
gr.Markdown(f"""
|
| 132 |
+
# ReCamMaster 🎥
|
| 133 |
+
|
| 134 |
+
This is a demo of [ReCamMaster](https://jianhongbai.github.io/ReCamMaster/), an amazing model that allows you to reshoot any video!
|
| 135 |
|
| 136 |
+
Due to the long generation times (~ 10 min) this space [should be duplicated](https://huggingface.co/spaces/jbilcke-hf/ReCamMaster?duplicate=true) to your own account for the best experience (please select at least a Nvidia L40S).
|
|
|
|
| 137 |
""")
|
| 138 |
|
| 139 |
with gr.Row():
|
|
|
|
| 157 |
value="1"
|
| 158 |
)
|
| 159 |
|
| 160 |
+
# Video settings
|
| 161 |
+
with gr.Group():
|
| 162 |
+
gr.Markdown("### Step 3: Video Settings")
|
| 163 |
+
num_frames = gr.Slider(
|
| 164 |
+
minimum=17,
|
| 165 |
+
maximum=81,
|
| 166 |
+
value=81,
|
| 167 |
+
step=16,
|
| 168 |
+
label="Number of Frames",
|
| 169 |
+
info="Must be 16n+1 (17, 33, 49, 65, 81)"
|
| 170 |
+
)
|
| 171 |
+
resolution = gr.Dropdown(
|
| 172 |
+
choices=["832x480", "480x480", "480x832", "576x320", "320x576"],
|
| 173 |
+
value="832x480",
|
| 174 |
+
label="Resolution",
|
| 175 |
+
info="Output video resolution"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
# Generate button
|
| 179 |
+
generate_btn = gr.Button("Generate Video (~10 min)", variant="primary")
|
| 180 |
|
| 181 |
with gr.Column():
|
| 182 |
# Output section
|
| 183 |
output_video = gr.Video(label="Output Video")
|
| 184 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 185 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
# Event handlers
|
| 187 |
generate_btn.click(
|
| 188 |
fn=generate_recammaster_video,
|
| 189 |
+
inputs=[video_input, text_prompt, camera_type, num_frames, resolution],
|
| 190 |
outputs=[output_video, status_output]
|
| 191 |
)
|
| 192 |
|
video_processor.py
CHANGED
|
@@ -15,21 +15,13 @@ logger = logging.getLogger(__name__)
|
|
| 15 |
class VideoProcessor:
|
| 16 |
def __init__(self, pipe):
|
| 17 |
self.pipe = pipe
|
| 18 |
-
self.
|
| 19 |
-
self.
|
| 20 |
-
|
| 21 |
-
# Create frame processor
|
| 22 |
-
self.frame_process = v2.Compose([
|
| 23 |
-
v2.CenterCrop(size=(self.height, self.width)),
|
| 24 |
-
v2.Resize(size=(self.height, self.width), antialias=True),
|
| 25 |
-
v2.ToTensor(),
|
| 26 |
-
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
| 27 |
-
])
|
| 28 |
|
| 29 |
-
def crop_and_resize(self, image):
|
| 30 |
"""Crop and resize image to match target dimensions"""
|
| 31 |
width_img, height_img = image.size
|
| 32 |
-
scale = max(
|
| 33 |
image = torchvision.transforms.functional.resize(
|
| 34 |
image,
|
| 35 |
(round(height_img*scale), round(width_img*scale)),
|
|
@@ -37,17 +29,25 @@ class VideoProcessor:
|
|
| 37 |
)
|
| 38 |
return image
|
| 39 |
|
| 40 |
-
def load_video_frames(self, video_path):
|
| 41 |
"""Load and process video frames"""
|
| 42 |
reader = imageio.get_reader(video_path)
|
| 43 |
frames = []
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
frame = reader.get_data(i)
|
| 48 |
frame = Image.fromarray(frame)
|
| 49 |
-
frame = self.crop_and_resize(frame)
|
| 50 |
-
frame =
|
| 51 |
frames.append(frame)
|
| 52 |
except:
|
| 53 |
# If we run out of frames, repeat the last one
|
|
@@ -64,14 +64,14 @@ class VideoProcessor:
|
|
| 64 |
|
| 65 |
return video_tensor
|
| 66 |
|
| 67 |
-
def load_camera_trajectory(self, cam_type):
|
| 68 |
"""Load camera trajectory for the selected type"""
|
| 69 |
tgt_camera_path = "./camera_trajectories/camera_extrinsics.json"
|
| 70 |
with open(tgt_camera_path, 'r') as file:
|
| 71 |
cam_data = json.load(file)
|
| 72 |
|
| 73 |
# Get camera trajectory for selected type
|
| 74 |
-
cam_idx = list(range(
|
| 75 |
traj = [parse_matrix(cam_data[f"frame{idx}"][f"cam{int(cam_type):02d}"]) for idx in cam_idx]
|
| 76 |
traj = np.stack(traj).transpose(0, 2, 1)
|
| 77 |
|
|
@@ -94,14 +94,14 @@ class VideoProcessor:
|
|
| 94 |
|
| 95 |
return camera_tensor
|
| 96 |
|
| 97 |
-
def process_video(self, video_path, text_prompt, cam_type):
|
| 98 |
"""Process video through ReCamMaster model"""
|
| 99 |
|
| 100 |
# Load video frames
|
| 101 |
-
video_tensor = self.load_video_frames(video_path)
|
| 102 |
|
| 103 |
# Load camera trajectory
|
| 104 |
-
camera_tensor = self.load_camera_trajectory(cam_type)
|
| 105 |
|
| 106 |
# Generate video with ReCamMaster
|
| 107 |
video = self.pipe(
|
|
@@ -109,6 +109,9 @@ class VideoProcessor:
|
|
| 109 |
negative_prompt=["worst quality, low quality, blurry, jittery, distorted"],
|
| 110 |
source_video=video_tensor,
|
| 111 |
target_camera=camera_tensor,
|
|
|
|
|
|
|
|
|
|
| 112 |
cfg_scale=5.0,
|
| 113 |
num_inference_steps=50,
|
| 114 |
seed=0,
|
|
|
|
| 15 |
class VideoProcessor:
|
| 16 |
def __init__(self, pipe):
|
| 17 |
self.pipe = pipe
|
| 18 |
+
self.default_height = 480
|
| 19 |
+
self.default_width = 832
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def crop_and_resize(self, image, height, width):
|
| 22 |
"""Crop and resize image to match target dimensions"""
|
| 23 |
width_img, height_img = image.size
|
| 24 |
+
scale = max(width / width_img, height / height_img)
|
| 25 |
image = torchvision.transforms.functional.resize(
|
| 26 |
image,
|
| 27 |
(round(height_img*scale), round(width_img*scale)),
|
|
|
|
| 29 |
)
|
| 30 |
return image
|
| 31 |
|
| 32 |
+
def load_video_frames(self, video_path, num_frames=81, height=480, width=832):
|
| 33 |
"""Load and process video frames"""
|
| 34 |
reader = imageio.get_reader(video_path)
|
| 35 |
frames = []
|
| 36 |
|
| 37 |
+
# Create frame processor with specified dimensions
|
| 38 |
+
frame_process = v2.Compose([
|
| 39 |
+
v2.CenterCrop(size=(height, width)),
|
| 40 |
+
v2.Resize(size=(height, width), antialias=True),
|
| 41 |
+
v2.ToTensor(),
|
| 42 |
+
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
for i in range(num_frames):
|
| 46 |
try:
|
| 47 |
frame = reader.get_data(i)
|
| 48 |
frame = Image.fromarray(frame)
|
| 49 |
+
frame = self.crop_and_resize(frame, height, width)
|
| 50 |
+
frame = frame_process(frame)
|
| 51 |
frames.append(frame)
|
| 52 |
except:
|
| 53 |
# If we run out of frames, repeat the last one
|
|
|
|
| 64 |
|
| 65 |
return video_tensor
|
| 66 |
|
| 67 |
+
def load_camera_trajectory(self, cam_type, num_frames=81):
|
| 68 |
"""Load camera trajectory for the selected type"""
|
| 69 |
tgt_camera_path = "./camera_trajectories/camera_extrinsics.json"
|
| 70 |
with open(tgt_camera_path, 'r') as file:
|
| 71 |
cam_data = json.load(file)
|
| 72 |
|
| 73 |
# Get camera trajectory for selected type
|
| 74 |
+
cam_idx = list(range(num_frames))[::4] # Sample every 4 frames
|
| 75 |
traj = [parse_matrix(cam_data[f"frame{idx}"][f"cam{int(cam_type):02d}"]) for idx in cam_idx]
|
| 76 |
traj = np.stack(traj).transpose(0, 2, 1)
|
| 77 |
|
|
|
|
| 94 |
|
| 95 |
return camera_tensor
|
| 96 |
|
| 97 |
+
def process_video(self, video_path, text_prompt, cam_type, num_frames=81, height=480, width=832):
|
| 98 |
"""Process video through ReCamMaster model"""
|
| 99 |
|
| 100 |
# Load video frames
|
| 101 |
+
video_tensor = self.load_video_frames(video_path, num_frames, height, width)
|
| 102 |
|
| 103 |
# Load camera trajectory
|
| 104 |
+
camera_tensor = self.load_camera_trajectory(cam_type, num_frames)
|
| 105 |
|
| 106 |
# Generate video with ReCamMaster
|
| 107 |
video = self.pipe(
|
|
|
|
| 109 |
negative_prompt=["worst quality, low quality, blurry, jittery, distorted"],
|
| 110 |
source_video=video_tensor,
|
| 111 |
target_camera=camera_tensor,
|
| 112 |
+
height=height,
|
| 113 |
+
width=width,
|
| 114 |
+
num_frames=num_frames,
|
| 115 |
cfg_scale=5.0,
|
| 116 |
num_inference_steps=50,
|
| 117 |
seed=0,
|