File size: 7,413 Bytes
6f2c7f0 |
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
import argparse
import os
from datetime import datetime
from pathlib import Path
from typing import List
import numpy as np
import torch
from PIL import Image
import gradio as gr
import json
import imageio
# Mock imports for demo - replace with actual imports when models are available
try:
from huggingface_hub import snapshot_download
from diffusers import AutoencoderKL, DDIMScheduler
from transformers import CLIPVisionModelWithProjection
from omegaconf import OmegaConf
import spaces
HAS_MODELS = True
except ImportError as e:
print(f"Warning: Some dependencies not available: {e}")
HAS_MODELS = False
MOTION_TRIGGER_WORD = {
'sports_basketball_gym': 'Basketball in Gym',
'sports_nba_pass': 'NBA Pass',
'sports_nba_dunk': 'NBA Dunk',
'movie_BruceLee1': 'Bruce Lee Style',
'shorts_kungfu_match1': 'Kung Fu Match',
'shorts_kungfu_desert1': 'Desert Kung Fu',
'parkour_climbing': 'Parkour Climbing',
'dance_indoor_1': 'Indoor Dance',
}
css_style = "#fixed_size_img {height: 500px;}"
def download_models():
"""Download required models from Hugging Face - simplified for demo"""
print("Model downloading simulation...")
# Create directory structure
os.makedirs('./pretrained_weights', exist_ok=True)
os.makedirs('./assets/masks', exist_ok=True)
os.makedirs('./assets/test_image', exist_ok=True)
os.makedirs('./assets/video_template', exist_ok=True)
if HAS_MODELS:
# Add actual model downloading logic here
pass
else:
print("Skipping model download - dependencies not available")
class MIMODemo():
def __init__(self):
self.device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {self.device}")
try:
download_models()
print("MIMO demo initialized")
except Exception as e:
print(f"Initialization warning: {e}")
def generate_video(self, image, motion_template):
"""Generate video from image and motion template"""
try:
if image is None:
return None, "β οΈ Please upload an image first."
print(f"Processing with template: {motion_template}")
# Create a simple demo video (replace with actual MIMO inference)
frames = []
for i in range(30): # 30 frames for demo
# Create a simple animation effect
img_array = np.array(image)
# Add some simple transformation for demo
shift = int(10 * np.sin(i * 0.2))
transformed = np.roll(img_array, shift, axis=1)
frames.append(transformed)
# Save video
save_dir = 'output'
os.makedirs(save_dir, exist_ok=True)
case = datetime.now().strftime("%Y%m%d%H%M%S")
outpath = f"{save_dir}/{case}.mp4"
imageio.mimsave(outpath, frames, fps=15, quality=8)
print(f'Demo video saved to: {outpath}')
return outpath, f"β
Generated demo animation for {MOTION_TRIGGER_WORD[motion_template]}!"
except Exception as e:
print(f"Error in video generation: {e}")
return None, f"β Error: {str(e)}"
def create_interface():
"""Create Gradio interface compatible with v3.41.2"""
# Initialize MIMO
mimo = MIMODemo()
# Custom CSS
css = """
#fixed_size_img {
height: 500px !important;
max-height: 500px !important;
}
.gradio-container {
max-width: 1200px !important;
margin: auto !important;
}
"""
with gr.Blocks(css=css, title="MIMO Demo") as demo:
# Title
gr.HTML("""
<div style="text-align: center; margin-bottom: 20px;">
<h1>π MIMO Demo - Controllable Character Video Synthesis</h1>
<p>Transform character images into animated videos with controllable motion and scenes</p>
<p>
<a href="https://menyifang.github.io/projects/MIMO/index.html" target="_blank">Project Page</a> |
<a href="https://arxiv.org/abs/2409.16160" target="_blank">Paper</a> |
<a href="https://github.com/menyifang/MIMO" target="_blank">GitHub</a>
</p>
</div>
""")
# Instructions
with gr.Accordion("π§ Instructions", open=True):
gr.Markdown("""
### How to use:
1. **Upload a character image**: Use a full-body, front-facing image with clear visibility
2. **Select motion template**: Choose from the available motion templates
3. **Generate**: Click "Generate Animation" to create your character animation
### Tips:
- Best results with clear, well-lit character images
- Processing may take 1-2 minutes depending on video length
- This is a demo version - full functionality requires GPU resources
""")
with gr.Row():
with gr.Column():
# Input image
img_input = gr.Image(
label='Upload Character Image',
type="pil",
elem_id="fixed_size_img"
)
# Motion template selector
motion_dropdown = gr.Dropdown(
choices=list(MOTION_TRIGGER_WORD.keys()),
value=list(MOTION_TRIGGER_WORD.keys())[0],
label="Select Motion Template",
)
# Generate button
submit_btn = gr.Button("π¬ Generate Animation", variant='primary')
# Status display
status_text = gr.Textbox(
label="Status",
interactive=False,
value="Ready to generate... (Demo mode)"
)
with gr.Column():
# Output video
output_video = gr.Video(
label="Generated Animation",
elem_id="fixed_size_img"
)
# Event handlers
submit_btn.click(
fn=mimo.generate_video,
inputs=[img_input, motion_dropdown],
outputs=[output_video, status_text],
)
# Example images (if available)
example_dir = './assets/test_image'
if os.path.exists(example_dir):
example_files = [f for f in os.listdir(example_dir) if f.endswith(('.jpg', '.png', '.jpeg'))]
if example_files:
example_paths = [[os.path.join(example_dir, f)] for f in example_files[:5]]
gr.Examples(
examples=example_paths,
inputs=[img_input],
label="Example Images"
)
return demo
if __name__ == "__main__":
print("π Starting MIMO Demo...")
# Create and launch interface
demo = create_interface()
# Launch with settings optimized for HF Spaces
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True,
quiet=False
) |