Update app.py
Browse files
app.py
CHANGED
|
@@ -41,74 +41,7 @@ def get_compliment():
|
|
| 41 |
])
|
| 42 |
|
| 43 |
uploaded_file = st.file_uploader("Upload a photo", type=["jpg", "jpeg", "png"])
|
| 44 |
-
if uploaded_file:
|
| 45 |
-
image = Image.open(uploaded_file).convert("RGB")
|
| 46 |
-
st.subheader("πΈ Original vs Stylized")
|
| 47 |
-
col1, col2 = st.columns(2)
|
| 48 |
-
col1.image(image, caption="Original", use_column_width=True)
|
| 49 |
-
|
| 50 |
-
if st.button("Generate Style"):
|
| 51 |
-
compiled_model, input_layer, output_layer = load_model(style)
|
| 52 |
-
input_tensor = preprocess(image)
|
| 53 |
-
|
| 54 |
-
# β
Correct inference method with list call
|
| 55 |
-
result = compiled_model([input_tensor])[output_layer]
|
| 56 |
-
|
| 57 |
-
result = postprocess(result)
|
| 58 |
-
result_pil = Image.fromarray(result)
|
| 59 |
-
col2.image(result_pil, caption=f"{style} Style", use_column_width=True)
|
| 60 |
-
|
| 61 |
-
buf = BytesIO()
|
| 62 |
-
result_pil.save(buf, format="PNG")
|
| 63 |
-
st.download_button("Download Stylized Image", data=buf.getvalue(), file_name=f"{style.lower()}_style.png", mime="image/png")
|
| 64 |
|
| 65 |
-
if persona == "ChatBoy π":
|
| 66 |
-
st.markdown(f"**{get_compliment()}**")
|
| 67 |
-
elif persona == "Poetic πΈ":
|
| 68 |
-
st.markdown("π *Your image now dances in the moonlight of a painted dream.*")
|
| 69 |
-
import streamlit as st
|
| 70 |
-
import cv2
|
| 71 |
-
import numpy as np
|
| 72 |
-
from PIL import Image
|
| 73 |
-
from io import BytesIO
|
| 74 |
-
from openvino.runtime import Core
|
| 75 |
-
import random
|
| 76 |
-
|
| 77 |
-
st.set_page_config(page_title="Anime & Cartoon Stylizer", layout="wide")
|
| 78 |
-
st.title("πΌοΈ Anime & Cartoon Stylizer with OpenVINO")
|
| 79 |
-
|
| 80 |
-
persona = st.radio("Choose your vibe:", ["Classic", "ChatBoy π", "Poetic πΈ"], horizontal=True)
|
| 81 |
-
style = st.selectbox("Choose your style:", ["AnimeGAN", "CartoonGAN"])
|
| 82 |
-
|
| 83 |
-
@st.cache_resource
|
| 84 |
-
def load_model(style_name):
|
| 85 |
-
ie = Core()
|
| 86 |
-
model_path = "animegan.xml" if style_name == "AnimeGAN" else "cartoongan.xml"
|
| 87 |
-
model = ie.read_model(model=model_path)
|
| 88 |
-
compiled_model = ie.compile_model(model=model, device_name="CPU")
|
| 89 |
-
return compiled_model, compiled_model.input(0), compiled_model.output(0)
|
| 90 |
-
|
| 91 |
-
def preprocess(image: Image.Image):
|
| 92 |
-
img = np.array(image.resize((256, 256))).astype(np.float32)
|
| 93 |
-
img = img / 127.5 - 1.0
|
| 94 |
-
img = np.transpose(img, (2, 0, 1))
|
| 95 |
-
return np.expand_dims(img, axis=0)
|
| 96 |
-
|
| 97 |
-
def postprocess(output):
|
| 98 |
-
result = output.squeeze().transpose(1, 2, 0)
|
| 99 |
-
result = (result + 1.0) * 127.5
|
| 100 |
-
return np.clip(result, 0, 255).astype(np.uint8)
|
| 101 |
-
|
| 102 |
-
def get_compliment():
|
| 103 |
-
return random.choice([
|
| 104 |
-
"β¨ You look like the protagonist of a dreamy anime romance.",
|
| 105 |
-
"π That transformation? Utterly magical.",
|
| 106 |
-
"πΈ Your photo just bloomed into a masterpiece.",
|
| 107 |
-
"π If Studio Ghibli saw this, they'd cast you instantly.",
|
| 108 |
-
"π«Ά This anime version of you? It's giving main character energy."
|
| 109 |
-
])
|
| 110 |
-
|
| 111 |
-
uploaded_file = st.file_uploader("Upload a photo", type=["jpg", "jpeg", "png"])
|
| 112 |
if uploaded_file:
|
| 113 |
image = Image.open(uploaded_file).convert("RGB")
|
| 114 |
st.subheader("πΈ Original vs Stylized")
|
|
@@ -119,7 +52,7 @@ if uploaded_file:
|
|
| 119 |
compiled_model, input_layer, output_layer = load_model(style)
|
| 120 |
input_tensor = preprocess(image)
|
| 121 |
|
| 122 |
-
# β
Correct inference syntax
|
| 123 |
result_dict = compiled_model.infer({input_layer: input_tensor})
|
| 124 |
output = result_dict[output_layer]
|
| 125 |
|
|
@@ -129,7 +62,12 @@ if uploaded_file:
|
|
| 129 |
|
| 130 |
buf = BytesIO()
|
| 131 |
result_pil.save(buf, format="PNG")
|
| 132 |
-
st.download_button(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
if persona == "ChatBoy π":
|
| 135 |
st.markdown(f"**{get_compliment()}**")
|
|
|
|
| 41 |
])
|
| 42 |
|
| 43 |
uploaded_file = st.file_uploader("Upload a photo", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if uploaded_file:
|
| 46 |
image = Image.open(uploaded_file).convert("RGB")
|
| 47 |
st.subheader("πΈ Original vs Stylized")
|
|
|
|
| 52 |
compiled_model, input_layer, output_layer = load_model(style)
|
| 53 |
input_tensor = preprocess(image)
|
| 54 |
|
| 55 |
+
# β
Correct inference syntax using infer()
|
| 56 |
result_dict = compiled_model.infer({input_layer: input_tensor})
|
| 57 |
output = result_dict[output_layer]
|
| 58 |
|
|
|
|
| 62 |
|
| 63 |
buf = BytesIO()
|
| 64 |
result_pil.save(buf, format="PNG")
|
| 65 |
+
st.download_button(
|
| 66 |
+
"Download Stylized Image",
|
| 67 |
+
data=buf.getvalue(),
|
| 68 |
+
file_name=f"{style.lower()}_style.png",
|
| 69 |
+
mime="image/png"
|
| 70 |
+
)
|
| 71 |
|
| 72 |
if persona == "ChatBoy π":
|
| 73 |
st.markdown(f"**{get_compliment()}**")
|