Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,10 @@ from huggingface_hub import hf_hub_download
|
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
| 21 |
logger = logging.getLogger(__name__)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Camera transformation types
|
| 24 |
CAMERA_TRANSFORMATIONS = {
|
| 25 |
"1": "Pan Right",
|
|
@@ -39,13 +43,72 @@ model_manager = None
|
|
| 39 |
pipe = None
|
| 40 |
is_model_loaded = False
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""Download ReCamMaster checkpoint from HuggingFace using huggingface_hub"""
|
| 44 |
-
|
| 45 |
-
repo_id = "KwaiVGI/ReCamMaster-Wan2.1"
|
| 46 |
-
filename = "step20000.ckpt"
|
| 47 |
-
checkpoint_dir = Path("models/ReCamMaster/checkpoints")
|
| 48 |
-
checkpoint_path = checkpoint_dir / filename
|
| 49 |
|
| 50 |
# Check if already exists
|
| 51 |
if checkpoint_path.exists():
|
|
@@ -53,28 +116,156 @@ def download_recammaster_checkpoint():
|
|
| 53 |
return checkpoint_path
|
| 54 |
|
| 55 |
# Create directory if it doesn't exist
|
| 56 |
-
|
| 57 |
|
| 58 |
# Download the checkpoint
|
| 59 |
logger.info("Downloading ReCamMaster checkpoint from HuggingFace...")
|
| 60 |
-
logger.info(f"Repository: {
|
| 61 |
-
logger.info(f"File: {
|
| 62 |
logger.info(f"Destination: {checkpoint_path}")
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
try:
|
| 65 |
# Download using huggingface_hub
|
| 66 |
downloaded_path = hf_hub_download(
|
| 67 |
-
repo_id=
|
| 68 |
-
filename=
|
| 69 |
-
local_dir=
|
| 70 |
local_dir_use_symlinks=False
|
| 71 |
)
|
| 72 |
logger.info(f"✓ Successfully downloaded ReCamMaster checkpoint to {downloaded_path}!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return downloaded_path
|
| 74 |
except Exception as e:
|
| 75 |
logger.error(f"✗ Error downloading checkpoint: {e}")
|
| 76 |
raise
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
class Camera(object):
|
| 79 |
def __init__(self, c2w):
|
| 80 |
c2w_mat = np.array(c2w).reshape(4, 4)
|
|
@@ -117,40 +308,70 @@ def load_models(progress_callback=None):
|
|
| 117 |
try:
|
| 118 |
logger.info("Starting model loading...")
|
| 119 |
|
| 120 |
-
# First
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
if progress_callback:
|
| 122 |
-
progress_callback(0.
|
| 123 |
|
| 124 |
try:
|
| 125 |
-
ckpt_path = download_recammaster_checkpoint()
|
| 126 |
logger.info(f"Using checkpoint at {ckpt_path}")
|
| 127 |
except Exception as e:
|
| 128 |
error_msg = f"Error downloading ReCamMaster checkpoint: {str(e)}"
|
| 129 |
logger.error(error_msg)
|
| 130 |
return error_msg
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
if progress_callback:
|
| 133 |
-
progress_callback(0.
|
| 134 |
|
| 135 |
# Load Wan2.1 pre-trained models
|
| 136 |
model_manager = ModelManager(torch_dtype=torch.bfloat16, device="cpu")
|
| 137 |
|
| 138 |
if progress_callback:
|
| 139 |
-
progress_callback(0.
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
if progress_callback:
|
| 148 |
-
progress_callback(0.
|
| 149 |
|
| 150 |
pipe = WanVideoReCamMasterPipeline.from_model_manager(model_manager, device="cuda")
|
| 151 |
|
| 152 |
if progress_callback:
|
| 153 |
-
progress_callback(0.
|
| 154 |
|
| 155 |
# Initialize additional modules introduced in ReCamMaster
|
| 156 |
dim = pipe.dit.blocks[0].self_attn.q.weight.shape[0]
|
|
@@ -262,7 +483,7 @@ def process_video_for_recammaster(video_path, text_prompt, cam_type, height=480,
|
|
| 262 |
video_tensor = frames.unsqueeze(0) # Add batch dimension
|
| 263 |
|
| 264 |
# Load camera trajectory
|
| 265 |
-
tgt_camera_path = "./
|
| 266 |
with open(tgt_camera_path, 'r') as file:
|
| 267 |
cam_data = json.load(file)
|
| 268 |
|
|
@@ -360,23 +581,13 @@ def generate_recammaster_video(
|
|
| 360 |
return None, f"Error: {str(e)}"
|
| 361 |
|
| 362 |
# Create Gradio interface
|
| 363 |
-
with gr.Blocks(title="ReCamMaster
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
value="Loading models, please wait...",
|
| 368 |
-
interactive=False,
|
| 369 |
-
visible=True
|
| 370 |
-
)
|
| 371 |
-
|
| 372 |
-
gr.Markdown("""
|
| 373 |
-
# 🎥 ReCamMaster Demo
|
| 374 |
|
| 375 |
ReCamMaster allows you to re-capture videos with novel camera trajectories.
|
| 376 |
Upload a video and select a camera transformation to see the magic!
|
| 377 |
-
|
| 378 |
-
**Note:** The ReCamMaster checkpoint will be automatically downloaded from HuggingFace when you start the app.
|
| 379 |
-
You still need to download Wan2.1 models using `python download_wan2.1.py` before running this demo.
|
| 380 |
""")
|
| 381 |
|
| 382 |
with gr.Row():
|
|
@@ -407,24 +618,8 @@ with gr.Blocks(title="ReCamMaster Demo") as demo:
|
|
| 407 |
# Output section
|
| 408 |
output_video = gr.Video(label="Output Video")
|
| 409 |
status_output = gr.Textbox(label="Generation Status", interactive=False)
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
gr.Markdown("### Example Videos")
|
| 413 |
-
gr.Examples(
|
| 414 |
-
examples=[
|
| 415 |
-
["example_test_data/videos/case0.mp4", "A person dancing", "1"],
|
| 416 |
-
["example_test_data/videos/case1.mp4", "A scenic view", "5"],
|
| 417 |
-
],
|
| 418 |
-
inputs=[video_input, text_prompt, camera_type],
|
| 419 |
-
)
|
| 420 |
-
|
| 421 |
-
# Load models automatically when the interface loads
|
| 422 |
-
def on_load():
|
| 423 |
-
status = load_models()
|
| 424 |
-
return gr.update(value=status, visible=True if "Error" in status else False)
|
| 425 |
-
|
| 426 |
-
demo.load(on_load, outputs=[loading_status])
|
| 427 |
-
|
| 428 |
# Event handlers
|
| 429 |
generate_btn.click(
|
| 430 |
fn=generate_recammaster_video,
|
|
@@ -433,4 +628,5 @@ with gr.Blocks(title="ReCamMaster Demo") as demo:
|
|
| 433 |
)
|
| 434 |
|
| 435 |
if __name__ == "__main__":
|
|
|
|
| 436 |
demo.launch(share=True)
|
|
|
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
| 21 |
logger = logging.getLogger(__name__)
|
| 22 |
|
| 23 |
+
# Get model storage path from environment variable or use default
|
| 24 |
+
MODELS_ROOT_DIR = os.environ.get("RECAMMASTER_MODELS_DIR", "/data/models")
|
| 25 |
+
logger.info(f"Using models root directory: {MODELS_ROOT_DIR}")
|
| 26 |
+
|
| 27 |
# Camera transformation types
|
| 28 |
CAMERA_TRANSFORMATIONS = {
|
| 29 |
"1": "Pan Right",
|
|
|
|
| 43 |
pipe = None
|
| 44 |
is_model_loaded = False
|
| 45 |
|
| 46 |
+
# Define model repositories and files
|
| 47 |
+
WAN21_REPO_ID = "Wan-AI/Wan2.1-T2V-1.3B"
|
| 48 |
+
WAN21_LOCAL_DIR = f"{MODELS_ROOT_DIR}/Wan-AI/Wan2.1-T2V-1.3B"
|
| 49 |
+
WAN21_FILES = [
|
| 50 |
+
"diffusion_pytorch_model.safetensors",
|
| 51 |
+
"models_t5_umt5-xxl-enc-bf16.pth",
|
| 52 |
+
"Wan2.1_VAE.pth"
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
RECAMMASTER_REPO_ID = "KwaiVGI/ReCamMaster-Wan2.1"
|
| 56 |
+
RECAMMASTER_CHECKPOINT_FILE = "step20000.ckpt"
|
| 57 |
+
RECAMMASTER_LOCAL_DIR = f"{MODELS_ROOT_DIR}/ReCamMaster/checkpoints"
|
| 58 |
+
|
| 59 |
+
# Define test data directory
|
| 60 |
+
TEST_DATA_DIR = "example_test_data"
|
| 61 |
+
|
| 62 |
+
def download_wan21_models(progress_callback=None):
|
| 63 |
+
"""Download Wan2.1 model files from HuggingFace"""
|
| 64 |
+
|
| 65 |
+
total_files = len(WAN21_FILES)
|
| 66 |
+
downloaded_paths = []
|
| 67 |
+
|
| 68 |
+
# Create directory if it doesn't exist
|
| 69 |
+
Path(WAN21_LOCAL_DIR).mkdir(parents=True, exist_ok=True)
|
| 70 |
+
|
| 71 |
+
for i, filename in enumerate(WAN21_FILES):
|
| 72 |
+
local_path = Path(WAN21_LOCAL_DIR) / filename
|
| 73 |
+
|
| 74 |
+
# Update progress
|
| 75 |
+
if progress_callback:
|
| 76 |
+
progress_callback(i/total_files, desc=f"Checking Wan2.1 file {i+1}/{total_files}: {filename}")
|
| 77 |
+
|
| 78 |
+
# Check if already exists
|
| 79 |
+
if local_path.exists():
|
| 80 |
+
logger.info(f"✓ {filename} already exists at {local_path}")
|
| 81 |
+
downloaded_paths.append(str(local_path))
|
| 82 |
+
continue
|
| 83 |
+
|
| 84 |
+
# Download the file
|
| 85 |
+
logger.info(f"Downloading {filename} from {WAN21_REPO_ID}...")
|
| 86 |
+
|
| 87 |
+
if progress_callback:
|
| 88 |
+
progress_callback(i/total_files, desc=f"Downloading Wan2.1 file {i+1}/{total_files}: {filename}")
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
# Download using huggingface_hub
|
| 92 |
+
downloaded_path = hf_hub_download(
|
| 93 |
+
repo_id=WAN21_REPO_ID,
|
| 94 |
+
filename=filename,
|
| 95 |
+
local_dir=WAN21_LOCAL_DIR,
|
| 96 |
+
local_dir_use_symlinks=False
|
| 97 |
+
)
|
| 98 |
+
logger.info(f"✓ Successfully downloaded {filename} to {downloaded_path}!")
|
| 99 |
+
downloaded_paths.append(downloaded_path)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
logger.error(f"✗ Error downloading {filename}: {e}")
|
| 102 |
+
raise
|
| 103 |
+
|
| 104 |
+
if progress_callback:
|
| 105 |
+
progress_callback(1.0, desc=f"All Wan2.1 models downloaded successfully!")
|
| 106 |
+
|
| 107 |
+
return downloaded_paths
|
| 108 |
+
|
| 109 |
+
def download_recammaster_checkpoint(progress_callback=None):
|
| 110 |
"""Download ReCamMaster checkpoint from HuggingFace using huggingface_hub"""
|
| 111 |
+
checkpoint_path = Path(RECAMMASTER_LOCAL_DIR) / RECAMMASTER_CHECKPOINT_FILE
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# Check if already exists
|
| 114 |
if checkpoint_path.exists():
|
|
|
|
| 116 |
return checkpoint_path
|
| 117 |
|
| 118 |
# Create directory if it doesn't exist
|
| 119 |
+
Path(RECAMMASTER_LOCAL_DIR).mkdir(parents=True, exist_ok=True)
|
| 120 |
|
| 121 |
# Download the checkpoint
|
| 122 |
logger.info("Downloading ReCamMaster checkpoint from HuggingFace...")
|
| 123 |
+
logger.info(f"Repository: {RECAMMASTER_REPO_ID}")
|
| 124 |
+
logger.info(f"File: {RECAMMASTER_CHECKPOINT_FILE}")
|
| 125 |
logger.info(f"Destination: {checkpoint_path}")
|
| 126 |
+
|
| 127 |
+
if progress_callback:
|
| 128 |
+
progress_callback(0.0, desc=f"Downloading ReCamMaster checkpoint...")
|
| 129 |
|
| 130 |
try:
|
| 131 |
# Download using huggingface_hub
|
| 132 |
downloaded_path = hf_hub_download(
|
| 133 |
+
repo_id=RECAMMASTER_REPO_ID,
|
| 134 |
+
filename=RECAMMASTER_CHECKPOINT_FILE,
|
| 135 |
+
local_dir=RECAMMASTER_LOCAL_DIR,
|
| 136 |
local_dir_use_symlinks=False
|
| 137 |
)
|
| 138 |
logger.info(f"✓ Successfully downloaded ReCamMaster checkpoint to {downloaded_path}!")
|
| 139 |
+
|
| 140 |
+
if progress_callback:
|
| 141 |
+
progress_callback(1.0, desc=f"ReCamMaster checkpoint downloaded successfully!")
|
| 142 |
+
|
| 143 |
return downloaded_path
|
| 144 |
except Exception as e:
|
| 145 |
logger.error(f"✗ Error downloading checkpoint: {e}")
|
| 146 |
raise
|
| 147 |
|
| 148 |
+
def create_test_data_structure(progress_callback=None):
|
| 149 |
+
"""Create sample camera extrinsics data for testing"""
|
| 150 |
+
|
| 151 |
+
if progress_callback:
|
| 152 |
+
progress_callback(0.0, desc="Creating test data structure...")
|
| 153 |
+
|
| 154 |
+
# Create directories
|
| 155 |
+
data_dir = Path(f"{TEST_DATA_DIR}/cameras")
|
| 156 |
+
videos_dir = Path(f"{TEST_DATA_DIR}/videos")
|
| 157 |
+
data_dir.mkdir(parents=True, exist_ok=True)
|
| 158 |
+
videos_dir.mkdir(parents=True, exist_ok=True)
|
| 159 |
+
|
| 160 |
+
camera_file = data_dir / "camera_extrinsics.json"
|
| 161 |
+
|
| 162 |
+
# Skip if file already exists
|
| 163 |
+
if camera_file.exists():
|
| 164 |
+
logger.info(f"✓ Camera extrinsics already exist at {camera_file}")
|
| 165 |
+
|
| 166 |
+
if progress_callback:
|
| 167 |
+
progress_callback(1.0, desc="Test data structure already exists")
|
| 168 |
+
|
| 169 |
+
return
|
| 170 |
+
|
| 171 |
+
if progress_callback:
|
| 172 |
+
progress_callback(0.3, desc="Generating camera extrinsics data...")
|
| 173 |
+
|
| 174 |
+
# Generate sample camera data
|
| 175 |
+
camera_data = {}
|
| 176 |
+
|
| 177 |
+
# Create 81 frames with 10 camera trajectories each
|
| 178 |
+
for frame_idx in range(81):
|
| 179 |
+
frame_key = f"frame{frame_idx}"
|
| 180 |
+
camera_data[frame_key] = {}
|
| 181 |
+
|
| 182 |
+
for cam_idx in range(1, 11): # Camera types 1-10
|
| 183 |
+
# Create a sample camera matrix (this is just an example - replace with actual logic if needed)
|
| 184 |
+
# In reality, these would be calculated based on specific camera movement patterns
|
| 185 |
+
|
| 186 |
+
# Create a base identity matrix
|
| 187 |
+
base_matrix = np.eye(4)
|
| 188 |
+
|
| 189 |
+
# Add some variation based on frame and camera type
|
| 190 |
+
# This is a simplistic example - real camera movements would be more complex
|
| 191 |
+
if cam_idx == 1: # Pan Right
|
| 192 |
+
base_matrix[0, 3] = 0.01 * frame_idx # Move right over time
|
| 193 |
+
elif cam_idx == 2: # Pan Left
|
| 194 |
+
base_matrix[0, 3] = -0.01 * frame_idx # Move left over time
|
| 195 |
+
elif cam_idx == 3: # Tilt Up
|
| 196 |
+
# Rotate around X-axis
|
| 197 |
+
angle = 0.005 * frame_idx
|
| 198 |
+
base_matrix[1, 1] = np.cos(angle)
|
| 199 |
+
base_matrix[1, 2] = -np.sin(angle)
|
| 200 |
+
base_matrix[2, 1] = np.sin(angle)
|
| 201 |
+
base_matrix[2, 2] = np.cos(angle)
|
| 202 |
+
elif cam_idx == 4: # Tilt Down
|
| 203 |
+
# Rotate around X-axis (opposite direction)
|
| 204 |
+
angle = -0.005 * frame_idx
|
| 205 |
+
base_matrix[1, 1] = np.cos(angle)
|
| 206 |
+
base_matrix[1, 2] = -np.sin(angle)
|
| 207 |
+
base_matrix[2, 1] = np.sin(angle)
|
| 208 |
+
base_matrix[2, 2] = np.cos(angle)
|
| 209 |
+
elif cam_idx == 5: # Zoom In
|
| 210 |
+
base_matrix[2, 3] = -0.01 * frame_idx # Move forward over time
|
| 211 |
+
elif cam_idx == 6: # Zoom Out
|
| 212 |
+
base_matrix[2, 3] = 0.01 * frame_idx # Move backward over time
|
| 213 |
+
elif cam_idx == 7: # Translate Up (with rotation)
|
| 214 |
+
base_matrix[1, 3] = 0.01 * frame_idx # Move up over time
|
| 215 |
+
angle = 0.003 * frame_idx
|
| 216 |
+
base_matrix[0, 0] = np.cos(angle)
|
| 217 |
+
base_matrix[0, 2] = np.sin(angle)
|
| 218 |
+
base_matrix[2, 0] = -np.sin(angle)
|
| 219 |
+
base_matrix[2, 2] = np.cos(angle)
|
| 220 |
+
elif cam_idx == 8: # Translate Down (with rotation)
|
| 221 |
+
base_matrix[1, 3] = -0.01 * frame_idx # Move down over time
|
| 222 |
+
angle = -0.003 * frame_idx
|
| 223 |
+
base_matrix[0, 0] = np.cos(angle)
|
| 224 |
+
base_matrix[0, 2] = np.sin(angle)
|
| 225 |
+
base_matrix[2, 0] = -np.sin(angle)
|
| 226 |
+
base_matrix[2, 2] = np.cos(angle)
|
| 227 |
+
elif cam_idx == 9: # Arc Left (with rotation)
|
| 228 |
+
angle = 0.005 * frame_idx
|
| 229 |
+
radius = 2.0
|
| 230 |
+
base_matrix[0, 3] = -radius * np.sin(angle)
|
| 231 |
+
base_matrix[2, 3] = -radius * np.cos(angle) + radius
|
| 232 |
+
# Rotate to look at center
|
| 233 |
+
look_angle = angle + np.pi
|
| 234 |
+
base_matrix[0, 0] = np.cos(look_angle)
|
| 235 |
+
base_matrix[0, 2] = np.sin(look_angle)
|
| 236 |
+
base_matrix[2, 0] = -np.sin(look_angle)
|
| 237 |
+
base_matrix[2, 2] = np.cos(look_angle)
|
| 238 |
+
elif cam_idx == 10: # Arc Right (with rotation)
|
| 239 |
+
angle = -0.005 * frame_idx
|
| 240 |
+
radius = 2.0
|
| 241 |
+
base_matrix[0, 3] = -radius * np.sin(angle)
|
| 242 |
+
base_matrix[2, 3] = -radius * np.cos(angle) + radius
|
| 243 |
+
# Rotate to look at center
|
| 244 |
+
look_angle = angle + np.pi
|
| 245 |
+
base_matrix[0, 0] = np.cos(look_angle)
|
| 246 |
+
base_matrix[0, 2] = np.sin(look_angle)
|
| 247 |
+
base_matrix[2, 0] = -np.sin(look_angle)
|
| 248 |
+
base_matrix[2, 2] = np.cos(look_angle)
|
| 249 |
+
|
| 250 |
+
# Format the matrix as a string (as expected by the app)
|
| 251 |
+
matrix_str = ' '.join([' '.join([str(base_matrix[i, j]) for j in range(4)]) for i in range(4)])
|
| 252 |
+
matrix_str = '[ ' + matrix_str.replace(' ', ' ] [ ', 3) + ' ]'
|
| 253 |
+
|
| 254 |
+
camera_data[frame_key][f"cam{cam_idx:02d}"] = matrix_str
|
| 255 |
+
|
| 256 |
+
if progress_callback:
|
| 257 |
+
progress_callback(0.7, desc="Saving camera extrinsics data...")
|
| 258 |
+
|
| 259 |
+
# Save camera extrinsics to JSON file
|
| 260 |
+
with open(camera_file, 'w') as f:
|
| 261 |
+
json.dump(camera_data, f, indent=2)
|
| 262 |
+
|
| 263 |
+
logger.info(f"Created sample camera extrinsics at {camera_file}")
|
| 264 |
+
logger.info(f"Created directory for example videos at {videos_dir}")
|
| 265 |
+
|
| 266 |
+
if progress_callback:
|
| 267 |
+
progress_callback(1.0, desc="Test data structure created successfully!")
|
| 268 |
+
|
| 269 |
class Camera(object):
|
| 270 |
def __init__(self, c2w):
|
| 271 |
c2w_mat = np.array(c2w).reshape(4, 4)
|
|
|
|
| 308 |
try:
|
| 309 |
logger.info("Starting model loading...")
|
| 310 |
|
| 311 |
+
# First create the test data structure
|
| 312 |
+
if progress_callback:
|
| 313 |
+
progress_callback(0.05, desc="Setting up test data structure...")
|
| 314 |
+
|
| 315 |
+
try:
|
| 316 |
+
create_test_data_structure(progress_callback)
|
| 317 |
+
except Exception as e:
|
| 318 |
+
error_msg = f"Error creating test data structure: {str(e)}"
|
| 319 |
+
logger.error(error_msg)
|
| 320 |
+
return error_msg
|
| 321 |
+
|
| 322 |
+
# Second, ensure the checkpoint is downloaded
|
| 323 |
if progress_callback:
|
| 324 |
+
progress_callback(0.1, desc="Checking for ReCamMaster checkpoint...")
|
| 325 |
|
| 326 |
try:
|
| 327 |
+
ckpt_path = download_recammaster_checkpoint(progress_callback)
|
| 328 |
logger.info(f"Using checkpoint at {ckpt_path}")
|
| 329 |
except Exception as e:
|
| 330 |
error_msg = f"Error downloading ReCamMaster checkpoint: {str(e)}"
|
| 331 |
logger.error(error_msg)
|
| 332 |
return error_msg
|
| 333 |
|
| 334 |
+
# Third, download Wan2.1 models if needed
|
| 335 |
+
if progress_callback:
|
| 336 |
+
progress_callback(0.2, desc="Checking for Wan2.1 models...")
|
| 337 |
+
|
| 338 |
+
try:
|
| 339 |
+
wan21_paths = download_wan21_models(progress_callback)
|
| 340 |
+
logger.info(f"Using Wan2.1 models: {wan21_paths}")
|
| 341 |
+
except Exception as e:
|
| 342 |
+
error_msg = f"Error downloading Wan2.1 models: {str(e)}"
|
| 343 |
+
logger.error(error_msg)
|
| 344 |
+
return error_msg
|
| 345 |
+
|
| 346 |
+
# Now, load the models
|
| 347 |
if progress_callback:
|
| 348 |
+
progress_callback(0.4, desc="Loading model manager...")
|
| 349 |
|
| 350 |
# Load Wan2.1 pre-trained models
|
| 351 |
model_manager = ModelManager(torch_dtype=torch.bfloat16, device="cpu")
|
| 352 |
|
| 353 |
if progress_callback:
|
| 354 |
+
progress_callback(0.5, desc="Loading Wan2.1 models...")
|
| 355 |
+
|
| 356 |
+
# Build full paths for the model files
|
| 357 |
+
model_files = [f"{WAN21_LOCAL_DIR}/{filename}" for filename in WAN21_FILES]
|
| 358 |
|
| 359 |
+
for model_file in model_files:
|
| 360 |
+
logger.info(f"Loading model from: {model_file}")
|
| 361 |
+
if not os.path.exists(model_file):
|
| 362 |
+
error_msg = f"Error: Model file not found: {model_file}"
|
| 363 |
+
logger.error(error_msg)
|
| 364 |
+
return error_msg
|
| 365 |
+
|
| 366 |
+
model_manager.load_models(model_files)
|
| 367 |
|
| 368 |
if progress_callback:
|
| 369 |
+
progress_callback(0.7, desc="Creating pipeline...")
|
| 370 |
|
| 371 |
pipe = WanVideoReCamMasterPipeline.from_model_manager(model_manager, device="cuda")
|
| 372 |
|
| 373 |
if progress_callback:
|
| 374 |
+
progress_callback(0.8, desc="Initializing ReCamMaster modules...")
|
| 375 |
|
| 376 |
# Initialize additional modules introduced in ReCamMaster
|
| 377 |
dim = pipe.dit.blocks[0].self_attn.q.weight.shape[0]
|
|
|
|
| 483 |
video_tensor = frames.unsqueeze(0) # Add batch dimension
|
| 484 |
|
| 485 |
# Load camera trajectory
|
| 486 |
+
tgt_camera_path = f"./{TEST_DATA_DIR}/cameras/camera_extrinsics.json"
|
| 487 |
with open(tgt_camera_path, 'r') as file:
|
| 488 |
cam_data = json.load(file)
|
| 489 |
|
|
|
|
| 581 |
return None, f"Error: {str(e)}"
|
| 582 |
|
| 583 |
# Create Gradio interface
|
| 584 |
+
with gr.Blocks(title="ReCamMaster") as demo:
|
| 585 |
+
|
| 586 |
+
gr.Markdown(f"""
|
| 587 |
+
# 🎥 ReCamMaster
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
|
| 589 |
ReCamMaster allows you to re-capture videos with novel camera trajectories.
|
| 590 |
Upload a video and select a camera transformation to see the magic!
|
|
|
|
|
|
|
|
|
|
| 591 |
""")
|
| 592 |
|
| 593 |
with gr.Row():
|
|
|
|
| 618 |
# Output section
|
| 619 |
output_video = gr.Video(label="Output Video")
|
| 620 |
status_output = gr.Textbox(label="Generation Status", interactive=False)
|
| 621 |
+
|
| 622 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
# Event handlers
|
| 624 |
generate_btn.click(
|
| 625 |
fn=generate_recammaster_video,
|
|
|
|
| 628 |
)
|
| 629 |
|
| 630 |
if __name__ == "__main__":
|
| 631 |
+
load_models()
|
| 632 |
demo.launch(share=True)
|