Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,64 @@ import streamlit as st
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
| 6 |
-
from
|
|
|
|
| 7 |
|
| 8 |
-
st.set_page_config(page_title="
|
| 9 |
-
st.title("
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@st.cache_resource
|
| 12 |
-
def load_model():
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
uploaded_file = st.file_uploader("Upload a photo", type=["jpg", "jpeg", "png"])
|
| 18 |
if uploaded_file:
|
| 19 |
image = Image.open(uploaded_file).convert("RGB")
|
| 20 |
-
st.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from openvino.runtime import Core
|
| 7 |
+
import random
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="Anime & Cartoon Stylizer", layout="wide")
|
| 10 |
+
st.title("๐ผ๏ธ Anime & Cartoon Stylizer with OpenVINO")
|
| 11 |
+
|
| 12 |
+
persona = st.radio("Choose your vibe:", ["Classic", "ChatBoy ๐", "Poetic ๐ธ"], horizontal=True)
|
| 13 |
+
style = st.selectbox("Choose your style:", ["AnimeGAN", "CartoonGAN"])
|
| 14 |
|
| 15 |
@st.cache_resource
|
| 16 |
+
def load_model(style_name):
|
| 17 |
+
ie = Core()
|
| 18 |
+
model_path = "animegan.xml" if style_name == "AnimeGAN" else "cartoongan.xml"
|
| 19 |
+
model = ie.read_model(model=model_path)
|
| 20 |
+
compiled_model = ie.compile_model(model=model, device_name="CPU")
|
| 21 |
+
return compiled_model, compiled_model.input(0), compiled_model.output(0)
|
| 22 |
+
|
| 23 |
+
def preprocess(image: Image.Image):
|
| 24 |
+
img = np.array(image.resize((256, 256))).astype(np.float32)
|
| 25 |
+
img = img / 127.5 - 1.0
|
| 26 |
+
img = np.transpose(img, (2, 0, 1))
|
| 27 |
+
return np.expand_dims(img, axis=0)
|
| 28 |
+
|
| 29 |
+
def postprocess(output):
|
| 30 |
+
result = output.squeeze().transpose(1, 2, 0)
|
| 31 |
+
result = (result + 1.0) * 127.5
|
| 32 |
+
return np.clip(result, 0, 255).astype(np.uint8)
|
| 33 |
|
| 34 |
+
def get_compliment():
|
| 35 |
+
return random.choice([
|
| 36 |
+
"โจ You look like the protagonist of a dreamy anime romance.",
|
| 37 |
+
"๐ That transformation? Utterly magical.",
|
| 38 |
+
"๐ธ Your photo just bloomed into a masterpiece.",
|
| 39 |
+
"๐ If Studio Ghibli saw this, they'd cast you instantly.",
|
| 40 |
+
"๐ซถ This anime version of you? It's giving main character energy."
|
| 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 |
+
output = compiled_model([input_tensor])[output_layer]
|
| 54 |
+
result = postprocess(output)
|
| 55 |
+
result_pil = Image.fromarray(result)
|
| 56 |
+
col2.image(result_pil, caption=f"{style} Style", use_column_width=True)
|
| 57 |
+
|
| 58 |
+
buf = BytesIO()
|
| 59 |
+
result_pil.save(buf, format="PNG")
|
| 60 |
+
st.download_button("Download Stylized Image", data=buf.getvalue(), file_name=f"{style.lower()}_style.png", mime="image/png")
|
| 61 |
+
|
| 62 |
+
if persona == "ChatBoy ๐":
|
| 63 |
+
st.markdown(f"**{get_compliment()}**")
|
| 64 |
+
elif persona == "Poetic ๐ธ":
|
| 65 |
+
st.markdown("๐ *Your image now dances in the moonlight of a painted dream.*")
|