File size: 2,378 Bytes
6f026d4
c3500d3
6f026d4
c3500d3
6f026d4
c3500d3
 
6f026d4
 
1459414
6f026d4
5731d7f
6f026d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import os
import uuid
import subprocess
from pathlib import Path
import gradio as gr

FRAME1 = Path("demo/frame1.png")
FRAME2 = Path("demo/frame2.png")

TARGET_DIR = "/home/user/app/output/"     # absolute path that inference writes to
PALETTE_PNG = "demo/palette.png"
OUTPUT_GIF_DIR = "output"
os.makedirs("demo", exist_ok=True)
os.makedirs(TARGET_DIR, exist_ok=True)
os.makedirs(OUTPUT_GIF_DIR, exist_ok=True)

def interpolate_image(img_a_path: str, img_b_path: str) -> tuple[str, str]:
    # 1) Run inference to generate frames into TARGET_DIR/img%d.png
    subprocess.run([
        "python3", "inference_img.py",
        "--img", str(img_a_path), str(img_b_path),
        "--exp", "4"
    ], check=True)

    # 2) Generate palette from frames
    subprocess.run([
        "ffmpeg", "-y", "-r", "14", "-f", "image2",
        "-i", f"{TARGET_DIR}img%d.png",
        "-vf", "palettegen=stats_mode=single",
        PALETTE_PNG
    ], check=True)

    # 3) Generate final GIF using palette
    out_gif = os.path.join(OUTPUT_GIF_DIR, f"{uuid.uuid4()}.gif")
    subprocess.run([
        "ffmpeg", "-y", "-r", "14", "-f", "image2",
        "-i", f"{TARGET_DIR}img%d.png",
        "-i", PALETTE_PNG,
        "-lavfi", "paletteuse",
        out_gif
    ], check=True)

    return out_gif, out_gif

with gr.Blocks(title="RIFE Interpolation") as demo:
    with gr.Tab("Demo"):
        gr.Markdown("### Demo: Preloaded images")
        input_imageA = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
        input_imageB = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
        run_btn = gr.Button("Interpolate")
        result_img = gr.Image(type="filepath", label="Interpolated GIF")
        result_path = gr.Textbox(label="Output path", interactive=False)
        run_btn.click(interpolate_image, [input_imageA, input_imageB], [result_img, result_path])

    with gr.Tab("Upload your images"):
        gr.Markdown("### Upload any two images")
        user_A = gr.Image(type="filepath", label="Image A")
        user_B = gr.Image(type="filepath", label="Image B")
        run_btn2 = gr.Button("Interpolate")
        user_img = gr.Image(type="filepath", label="Interpolated GIF")
        user_path = gr.Textbox(label="Output path", interactive=False)
        run_btn2.click(interpolate_image, [user_A, user_B], [user_img, user_path])

demo.launch()