Fanu2 commited on
Commit
f79b705
ยท
verified ยท
1 Parent(s): 4f4562f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -15
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 paddle
6
- from ppgan.apps import AnimeGANPredictor
 
7
 
8
- st.set_page_config(page_title="AnimeGAN", layout="centered")
9
- st.title("๐ŸŽจ Convert Your Photo to Anime Style")
 
 
 
10
 
11
  @st.cache_resource
12
- def load_model():
13
- return AnimeGANPredictor()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- predictor = load_model()
 
 
 
 
 
 
 
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.image(image, caption="Original Image", use_column_width=True)
21
-
22
- if st.button("Generate Anime"):
23
- img_np = np.array(image)
24
- img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
25
- result = predictor.run(img_bgr)
26
- result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
27
- st.image(result_rgb, caption="Anime Style", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
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.*")