Spaces:
Running
Running
File size: 3,655 Bytes
8085664 7e1f9a4 8085664 7b51134 7e1f9a4 8085664 c442a20 92587e9 fd2ef5c 463c565 c442a20 463c565 8085664 80af604 8085664 a6f1332 a275306 e7cfbe4 41f9040 3387a89 463c565 e7cfbe4 a6f1332 463c565 a52af5f 8085664 a6f1332 8085664 a6f1332 8085664 a275306 a6f1332 a275306 8085664 18a5492 a6f1332 0a43e1c a6f1332 8085664 b9a22ab 8085664 c442a20 b9a22ab 8085664 78cdc0b 8085664 9f80de2 7b51134 8085664 b9a22ab c442a20 8085664 a52af5f 8085664 6b58d86 a52af5f 8085664 80af604 a52af5f 8085664 c442a20 136fd88 |
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 101 102 103 104 105 106 107 108 109 110 111 112 |
# app.py
import os
import uuid
import subprocess
from pathlib import Path
from typing import Tuple
from inference_img import *
import gradio as gr
# Demo images
FRAME1 = Path("demo/frame1.jpg")
FRAME2 = Path("demo/frame2.jpg")
DEMO = Path("demo/demo.gif")
MODEL = Path("train_log/")
TMP_PREFIX = "demo/"
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:
#inference_img(img=[Path(img_a_path), Path(img_b_path)],exp=4)
print("ok")
subprocess.run([
"ffmpeg", "-y", "-r", "14", "-f", "image2",
"-i", f"{TARGET_DIR}img%d.png",
"-vf", "palettegen=stats_mode=single",
palette_path
], check=True)
# 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)
print("ok")
# 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",
"palettegen=stats_mode=single",
palette_path
], 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]
)
with gr.Tab("Upload your images"):
gr.Markdown("### Upload any two images")
input_imageA = gr.Image(type="filepath", label="Image A")
input_imageB = gr.Image(type="filepath", label="Image B")
user_btn = gr.Button("Interpolate")
user_out = gr.Image(type="filepath", value=str(TARGET_DIR), label="Interpolated GIF")
user_path = gr.Textbox(label="Output path", value=str(TARGET_DIR), interactive=False)
user_btn.click(
fn=interpolate_image,
inputs=[input_imageA, input_imageB],
outputs=[user_out, user_path],
)
demo.launch()
|