File size: 2,640 Bytes
6f026d4
c3500d3
6f026d4
c3500d3
6f026d4
c3500d3
 
6f026d4
 
1459414
4e2e497
 
 
853952c
6f026d4
 
6040d2d
6f026d4
 
 
 
 
 
 
 
 
 
 
 
4e2e497
 
6f026d4
 
 
 
 
 
4e2e497
6f026d4
4e2e497
6f026d4
 
6040d2d
6f026d4
 
 
 
 
 
 
 
6040d2d
 
6f026d4
 
 
 
 
 
 
6040d2d
 
fdf3c4a
 
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
67
# 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/"
PALETTE_PNG = Path(TARGET_DIR) / "palette.png"
OUTPUT_GIF = Path(TARGET_DIR) / "output.gif"

os.makedirs(TARGET_DIR, exist_ok=True)

def interpolate_image(img_a_path: str, img_b_path: 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",
        "-frames:v", "1",
        str(PALETTE_PNG)
    ], check=True)

    # 3) Generate final GIF using palette
    subprocess.run([
        "ffmpeg", "-y", "-r", "14", "-f", "image2",
        "-i", f"{TARGET_DIR}img%d.png",
        "-i", str(PALETTE_PNG),
        "-lavfi", "paletteuse",
        str(OUTPUT_GIF)
    ], check=True)

    return str(OUTPUT_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])

    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])
        gr.HTML("""<div style="margin: 0.75em 0;"><a href="https://www.buymeacoffee.com/Artgen" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a></div>
        <div style="margin: 0.75em 0;">But what would really help me is a <strong>PRO subscription</strong> to Google Colab, Kaggle or Hugging Face. Many thanks.</div>""")

demo.launch()