|
|
|
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: |
|
|
|
subprocess.run([ |
|
"python3", "inference_img.py", |
|
"--img", str(img_a_path), str(img_b_path), |
|
"--exp", "4" |
|
], check=True) |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
def load_description(path): |
|
with open(path, "r", encoding="utf-8") as f: |
|
return f.read() |
|
|
|
description_text = load_description("TITLE.md") |
|
|
|
with gr.Blocks(title="RIFE Image 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") |
|
|
|
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") |
|
|
|
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() |
|
|