File size: 7,010 Bytes
f0cc3b2
 
 
 
 
 
 
 
2932acc
 
 
 
 
f0cc3b2
 
 
 
2932acc
 
 
f0cc3b2
2932acc
 
 
 
 
 
f0cc3b2
 
e5b16fd
f0cc3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b16fd
 
f0cc3b2
 
 
 
2932acc
f0cc3b2
 
2932acc
 
 
f0cc3b2
 
 
 
 
 
 
 
 
 
5dd7501
f0cc3b2
e5b16fd
 
 
f0cc3b2
 
e5b16fd
 
 
 
 
 
f0cc3b2
 
 
2932acc
f0cc3b2
 
e5b16fd
 
 
 
f0cc3b2
 
 
 
 
2932acc
f0cc3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b16fd
2932acc
a383ee8
e5b16fd
 
 
f0cc3b2
e5b16fd
f0cc3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5b16fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0cc3b2
e5b16fd
f0cc3b2
 
 
 
e5b16fd
 
f0cc3b2
 
 
e5b16fd
f0cc3b2
 
 
 
2932acc
f0cc3b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import gradio as gr
import torch
import os
import tempfile
import shutil
import imageio
import logging
from pathlib import Path

# Import from our modules
from model_loader import ModelLoader, MODELS_ROOT_DIR
from video_processor import VideoProcessor
from config import CAMERA_TRANSFORMATIONS, TEST_DATA_DIR

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Global model loader instance
model_loader = ModelLoader()
video_processor = None

def init_video_processor():
    """Initialize video processor"""
    global video_processor
    if model_loader.is_loaded and video_processor is None:
        video_processor = VideoProcessor(model_loader.pipe)
    return video_processor is not None

def extract_frames_from_video(video_path, output_dir, max_frames=81):
    """Extract frames from video and ensure we have at least max_frames frames"""
    os.makedirs(output_dir, exist_ok=True)
    
    reader = imageio.get_reader(video_path)
    fps = reader.get_meta_data()['fps']
    total_frames = reader.count_frames()
    
    frames = []
    for i, frame in enumerate(reader):
        frames.append(frame)
    reader.close()
    
    # If we have fewer than required frames, repeat the last frame
    if len(frames) < max_frames:
        logger.info(f"Video has {len(frames)} frames, padding to {max_frames} frames")
        last_frame = frames[-1]
        while len(frames) < max_frames:
            frames.append(last_frame)
    
    # Save frames
    for i, frame in enumerate(frames[:max_frames]):
        frame_path = os.path.join(output_dir, f"frame_{i:04d}.png")
        imageio.imwrite(frame_path, frame)
    
    return len(frames[:max_frames]), fps

def generate_recammaster_video(
    video_file,
    text_prompt,
    camera_type,
    num_frames,
    resolution,
    progress=gr.Progress()
):
    """Main function to generate video with ReCamMaster"""
    
    if not model_loader.is_loaded:
        return None, "Error: Models not loaded! Please load models first."
    
    if not init_video_processor():
        return None, "Error: Failed to initialize video processor."
    
    if video_file is None:
        return None, "Please upload a video file."
    
    try:
        # Create temporary directory for processing
        with tempfile.TemporaryDirectory() as temp_dir:
            progress(0.1, desc="Processing input video...")
            
            # Copy uploaded video to temp directory
            input_video_path = os.path.join(temp_dir, "input.mp4")
            shutil.copy(video_file, input_video_path)
            
            # Parse resolution
            width, height = map(int, resolution.split('x'))
            
            # Extract frames
            progress(0.2, desc="Extracting video frames...")
            extracted_frames, fps = extract_frames_from_video(
                input_video_path, 
                os.path.join(temp_dir, "frames"),
                max_frames=num_frames
            )
            logger.info(f"Extracted {extracted_frames} frames at {fps} fps")
            
            # Process with ReCamMaster
            progress(0.3, desc="Processing with ReCamMaster...")
            output_video = video_processor.process_video(
                input_video_path,
                text_prompt,
                camera_type,
                num_frames=num_frames,
                height=height,
                width=width
            )
            
            # Save output video
            progress(0.9, desc="Saving output video...")
            output_path = os.path.join(temp_dir, "output.mp4")
            from diffsynth import save_video
            save_video(output_video, output_path, fps=30, quality=5)
            
            # Copy to persistent location
            final_output_path = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False).name
            shutil.copy(output_path, final_output_path)
            
            progress(1.0, desc="Done!")
            
            transformation_name = CAMERA_TRANSFORMATIONS.get(str(camera_type), "Unknown")
            status_msg = f"Successfully generated video with '{transformation_name}' camera movement!"
            
            return final_output_path, status_msg
    
    except Exception as e:
        logger.error(f"Error generating video: {str(e)}")
        return None, f"Error: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="ReCamMaster") as demo:

    gr.Markdown(f"""
    # ReCamMaster 🎥

    This is a demo of [ReCamMaster](https://jianhongbai.github.io/ReCamMaster/), an amazing model that allows you to reshoot any video!
    
    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).
    """)
    
    with gr.Row():
        with gr.Column():
            # Video input section
            with gr.Group():
                gr.Markdown("### Step 1: Upload Video")
                video_input = gr.Video(label="Input Video")
                text_prompt = gr.Textbox(
                    label="Text Prompt (describe your video)",
                    placeholder="A person walking in the street",
                    value="A dynamic scene"
                )
            
            # Camera selection
            with gr.Group():
                gr.Markdown("### Step 2: Select Camera Movement")
                camera_type = gr.Radio(
                    choices=[(v, k) for k, v in CAMERA_TRANSFORMATIONS.items()],
                    label="Camera Transformation",
                    value="1"
                )
            
            # Video settings
            with gr.Group():
                gr.Markdown("### Step 3: Video Settings")
                num_frames = gr.Slider(
                    minimum=17,
                    maximum=81,
                    value=81,
                    step=16,
                    label="Number of Frames",
                    info="Must be 16n+1 (17, 33, 49, 65, 81)"
                )
                resolution = gr.Dropdown(
                    choices=["832x480", "480x480", "480x832", "576x320", "320x576"],
                    value="832x480",
                    label="Resolution",
                    info="Output video resolution"
                )
            
            # Generate button
            generate_btn = gr.Button("Generate Video (~10 min)", variant="primary")
        
        with gr.Column():
            # Output section
            output_video = gr.Video(label="Output Video")
            status_output = gr.Textbox(label="Status", interactive=False)
   
    # Event handlers
    generate_btn.click(
        fn=generate_recammaster_video,
        inputs=[video_input, text_prompt, camera_type, num_frames, resolution],
        outputs=[output_video, status_output]
    )

if __name__ == "__main__":
    model_loader.load_models()
    demo.launch(share=True)