File size: 3,428 Bytes
8085664
 
 
7e1f9a4
 
8085664
a275306
7e1f9a4
 
8085664
c442a20
 
92587e9
8085664
c442a20
 
a275306
8085664
80af604
8085664
 
 
 
 
 
 
 
 
 
 
 
 
a6f1332
 
a275306
 
 
8085664
a275306
 
 
 
 
a6f1332
8085664
a6f1332
8085664
 
 
 
a6f1332
 
8085664
a275306
a6f1332
a275306
8085664
a275306
 
a6f1332
 
0a43e1c
a6f1332
8085664
 
b9a22ab
8085664
c442a20
b9a22ab
8085664
 
 
78cdc0b
8085664
 
 
 
 
 
9f80de2
80af604
 
8085664
b9a22ab
c442a20
8085664
 
 
 
 
 
 
 
80af604
92587e9
8085664
 
c442a20
80af604
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
# app.py
import os
import uuid
import subprocess
from pathlib import Path
from typing import Tuple
from inference_img import run_imginference
import gradio as gr

# Demo images
FRAME1 = Path("demo/frame1.jpg")
FRAME2 = Path("demo/frame2.jpg")
DEMO = Path("demo/demo.gif")

TMP_PREFIX = "/tmp/gradio/"
TARGET_DIR = f"{TMP_PREFIX}output/"
EXTENSION = "png"

def interpolate_image(img_a_path: str, img_b_path: str) -> Tuple[str, str]:
    """
    3-step pipeline:
    1) inference_img.py --img <A> <B> --exp=4  -> writes frames as /tmp/gradio/output/img%d.png
    2) ffmpeg palettegen
    3) ffmpeg paletteuse -> final GIF at a unique path
    Returns: (gif_filepath_for_preview, gif_filepath_for_download)
    """
    # Ensure temp dirs exist
    os.makedirs(TARGET_DIR, exist_ok=True)

    # Unique output path per run
    interpolated_path = f"{TARGET_DIR}{uuid.uuid4()}.{EXTENSION}"
    palette_path = f"{TMP_PREFIX}palette.png"

    try:

        run_imginference(img_a_path, img_b_path, exp=4)
        
        # 1) Run interpolation (frames -> TARGET_DIR/img1.png ... imgN.png)
        # # subprocess.run([
        #   #   "python3", "inference_img.py",
        #   #   "--img", str(img_a_path), str(img_b_path),
        #       "--exp", "4"
        # ], check=True)

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

        # 3) Use palette to produce final GIF
        #!ffmpeg -r 14 -f image2 -i output/img%d.png -vf "palettegen=stats_mode=single" palette.png
        subprocess.run([
            "ffmpeg", "-y", "-r", "14", "-f",
            "-i", f"{TARGET_DIR}img%d.png",
            str(palettegen=stats_mode=single)",
            str(palette.png)
        ], check=True)

        return interpolated_image, interpolated_path

    except subprocess.CalledProcessError as e:
        raise gr.Error(f"Interpolation failed. {e}")

# Gradio UI
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")
        output_image = gr.Image(type="filepath", value=str(DEMO), label="Demo")
        g_btn = gr.Button("Interpolate")

        output_image = gr.Image(type="filepath", label="Interpolated GIF")
        enhance_image_path = gr.Textbox(label="Output path", interactive=False)

        g_btn.click(
            fn=interpolate_image,
            inputs=[input_imageA, input_imageB],
            outputs=[output_image, DEMO],
        )

    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")
        user_mode = gr.Dropdown(choices=["default"], value="default", label="Mode")
        user_btn = gr.Button("Interpolate")
        user_out = gr.Image(type="filepath", label="Interpolated GIF")
        user_path = gr.Textbox(label="Output path", interactive=False)
        user_btn.click(
            fn=interpolate_image,
            inputs=[user_A, user_B],
            outputs=[user_out, user_path],
        )

app.launch()