Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
from pathlib import Path
|
4 |
+
from PIL import Image
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
# ---- CONFIG ----
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Streamlit iCodeIdoia",
|
10 |
+
page_icon="images/ilpicon1.png",
|
11 |
+
layout="wide",
|
12 |
+
initial_sidebar_state="expanded"
|
13 |
+
)
|
14 |
+
|
15 |
+
st.image("images/banner.jpg")
|
16 |
+
|
17 |
+
# ---- PATHS ----
|
18 |
+
FRAME1 = Path("demo/frame1.png")
|
19 |
+
FRAME2 = Path("demo/frame2.png")
|
20 |
+
TARGET_DIR = Path("/home/user/app/output/")
|
21 |
+
PALETTE_PNG = TARGET_DIR / "palette.png"
|
22 |
+
OUTPUT_GIF = TARGET_DIR / "output.gif"
|
23 |
+
|
24 |
+
os.makedirs(TARGET_DIR, exist_ok=True)
|
25 |
+
|
26 |
+
# ---- FUNCTION ----
|
27 |
+
def interpolate_image(img_a_path: str, img_b_path: str) -> str:
|
28 |
+
subprocess.run([
|
29 |
+
"python3", "inference_img.py",
|
30 |
+
"--img", str(img_a_path), str(img_b_path),
|
31 |
+
"--exp", "4"
|
32 |
+
], check=True)
|
33 |
+
|
34 |
+
subprocess.run([
|
35 |
+
"ffmpeg", "-y", "-r", "14", "-f", "image2",
|
36 |
+
"-i", f"{TARGET_DIR}/img%d.png",
|
37 |
+
"-vf", "palettegen=stats_mode=single",
|
38 |
+
"-frames:v", "1",
|
39 |
+
str(PALETTE_PNG)
|
40 |
+
], check=True)
|
41 |
+
|
42 |
+
subprocess.run([
|
43 |
+
"ffmpeg", "-y", "-r", "14", "-f", "image2",
|
44 |
+
"-i", f"{TARGET_DIR}/img%d.png",
|
45 |
+
"-i", str(PALETTE_PNG),
|
46 |
+
"-lavfi", "paletteuse",
|
47 |
+
str(OUTPUT_GIF)
|
48 |
+
], check=True)
|
49 |
+
|
50 |
+
return str(OUTPUT_GIF)
|
51 |
+
|
52 |
+
# ---- TABS ----
|
53 |
+
tab1, tab2 = st.tabs(["Demo", "Upload your images"])
|
54 |
+
|
55 |
+
with tab1:
|
56 |
+
st.subheader("Demo: Preloaded images")
|
57 |
+
st.image(str(FRAME1), caption="Image A")
|
58 |
+
st.image(str(FRAME2), caption="Image B")
|
59 |
+
|
60 |
+
if st.button("Run Interpolation Demo"):
|
61 |
+
gif_path = interpolate_image(FRAME1, FRAME2)
|
62 |
+
st.image(gif_path, caption="Interpolated GIF")
|
63 |
+
st.text(f"Output path: {gif_path}")
|
64 |
+
|
65 |
+
with tab2:
|
66 |
+
st.subheader("Upload any two images")
|
67 |
+
uploaded_a = st.file_uploader("Upload Image A", type=["png", "jpg", "jpeg"])
|
68 |
+
uploaded_b = st.file_uploader("Upload Image B", type=["png", "jpg", "jpeg"])
|
69 |
+
|
70 |
+
if uploaded_a and uploaded_b:
|
71 |
+
temp_a = TARGET_DIR / "user_a.png"
|
72 |
+
temp_b = TARGET_DIR / "user_b.png"
|
73 |
+
Image.open(uploaded_a).save(temp_a)
|
74 |
+
Image.open(uploaded_b).save(temp_b)
|
75 |
+
|
76 |
+
if st.button("Run Interpolation"):
|
77 |
+
gif_path = interpolate_image(temp_a, temp_b)
|
78 |
+
st.image(gif_path, caption="Interpolated GIF")
|
79 |
+
st.text(f"Output path: {gif_path}")
|