File size: 2,463 Bytes
d49f7bc
 
 
 
e5e07c1
d49f7bc
 
 
e5e07c1
d49f7bc
 
 
 
e5e07c1
 
d49f7bc
e5e07c1
 
 
 
 
 
d49f7bc
 
e5e07c1
 
d49f7bc
 
 
e5e07c1
 
d49f7bc
 
e5e07c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d49f7bc
e5e07c1
d49f7bc
 
 
 
 
e5e07c1
d49f7bc
 
 
58ca87d
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
import gradio as gr
import os
import subprocess

# Define directories for uploads and outputs
UPLOAD_FOLDER = 'uploads_gradio'
OUTPUT_FOLDER = 'outputs_gradio'

# Create directories if they don't exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)

def animate_image(file_path):
    """
    Function to process the uploaded image and generate an animated GIF.
    
    Args:
        file_path (str): Path to the uploaded file.
    
    Returns:
        str: Path to the generated GIF.
    """
    if not file_path:
        raise ValueError("No file uploaded.")

    # 'file_path' is a string path to the uploaded file
    input_path = file_path
    filename = os.path.basename(input_path)
    base, ext = os.path.splitext(filename)
    
    # Define the annotation directory for this specific image
    char_anno_dir = os.path.join(OUTPUT_FOLDER, f"{base}_out")
    os.makedirs(char_anno_dir, exist_ok=True)
    
    try:
        # Validate file extension
        allowed_extensions = ['.png', '.jpg', '.jpeg', '.bmp']
        if ext.lower() not in allowed_extensions:
            raise ValueError("Unsupported file type. Please upload an image file (png, jpg, jpeg, bmp).")
        
        # Run the image_to_animation.py script with required arguments
        subprocess.run([
            'python', 'examples/image_to_animation.py',
            input_path, char_anno_dir
            # Optionally, add motion_cfg_fn and retarget_cfg_fn here if needed
        ], check=True)
        
        # Path to the generated GIF
        gif_path = os.path.join(char_anno_dir, "video.gif")
        
        if os.path.exists(gif_path):
            return gif_path
        else:
            raise FileNotFoundError("Animation failed to generate. Please ensure the input image contains clear humanoid drawings.")
    
    except subprocess.CalledProcessError as e:
        raise RuntimeError(f"Error during processing: {e}")
    except Exception as e:
        raise RuntimeError(f"Unexpected error: {e}")

# Define the Gradio interface using the updated API
iface = gr.Interface(
    fn=animate_image,
    inputs=gr.Image(label="Upload Drawing", type="filepath", sources=["upload", "webcam"]),
    outputs=gr.Image(label="Animated GIF"),
    title="Animated Drawings",
    description="Upload your drawing or take a photo, and get an animated GIF."
)

if __name__ == "__main__":
    iface.launch(server_name="0.0.0.0", server_port=7860)