File size: 6,395 Bytes
421360e
 
 
 
 
 
 
 
 
 
b9ebc8c
 
 
421360e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82b9a2a
 
 
421360e
 
 
 
b9ebc8c
 
 
 
 
 
 
 
 
 
 
421360e
 
 
 
 
 
b9ebc8c
421360e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9ebc8c
421360e
 
 
 
 
 
 
b9ebc8c
 
421360e
b9ebc8c
 
 
 
 
 
 
 
 
 
421360e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9ebc8c
421360e
 
 
 
 
b9ebc8c
 
 
 
 
 
421360e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2b7754
 
 
 
 
 
 
421360e
 
 
 
e2b7754
421360e
 
 
 
e2b7754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421360e
 
 
 
b9ebc8c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import streamlit as st
from PIL import Image
import cv2 as cv
import numpy as np
from random import randint
import threading

# style.py
import style

# utils.py
from utils import *

st.set_page_config(page_title="Animefy", page_icon="images/animefy_logo.png")

model_lock = threading.Lock()

# remove "Made with Streamlit" footer text
# uncomment "#MainMenu {visibility: hidden;}" to also remove the default Streamlit hamburger menu
hide_streamlit = """
<style>
    #MainMenu {
        visibility: hidden;
    }
    footer {
        visibility: hidden;
    }
    header {
        visibility: hidden;
    }
    button[title='View fullscreen'] {
        visibility: hidden;
    }
</style>
"""
st.markdown(hide_streamlit, unsafe_allow_html=True)

def imageResCheck (image):
    # load and preprocess input image as a NumPy array
    image = np.asarray(load_input_image(image))

    image_height, image_width = image.shape[:2]

    if (image_height <= 2160 and image_width <= 2160):
        return True
    else:
        return False

# randomizer. a workaround for clearing the contents of the file_uploader
if 'uploader_key' not in st.session_state:
    st.session_state['uploader_key'] = str(randint(1000, 100000000))

# home page title and caption
st.markdown("""
    # ✨ Animefy ✨

    Convert your photos into **anime** with _ease_ using **AnimeGAN**.
""")

st.markdown('---')

# main container of the page
page_container = st.empty()

# store home page contents inside page_container
home_page = page_container.container()

# step #1
home_page.markdown("""
    ### Step #1: Upload the photo that you would like to process!
""")

# just some notes for the user
with home_page.expander("📣 Here are some things to take note of...", expanded=True):
    st.write("""    
        * Do note that AnimeGAN works best with images containing **sceneries without people**. 
        * For best results, use images that **do not** contain human subjects.
        * Fore more information on AnimeGAN, click [here](https://github.com/TachibanaYoshino/AnimeGAN).
    """)

# upload image functionality
uploaded_image = home_page.file_uploader(
        "If you're ready, you can now upload your image here:", type=['png','jpg','jpeg'], key=st.session_state['uploader_key']
    )

# some checking if the image resolution is valid
# being done only for performance purposes.
if uploaded_image is not None:
    isValidImage = imageResCheck(uploaded_image)
else:
    isValidImage = False

# warning
if uploaded_image is not None and not isValidImage:
    st.caption("**Warning:** For better performance, please upload an image that will not exceed a resolution of **2160x2160**.")

# if there is an uploaded image, show next steps
if isValidImage:
    # just a preview of the uploaded image
    home_page.markdown("""
        #### Uploaded Image

        Here's your photo! Just upload another one if you would like to change it 😉
    """)
    home_page.image(Image.open(uploaded_image))

    home_page.write("---")

    # step #2
    home_page.markdown("""
        ### Step #2: Now, select your preferred animation style!
    """)

    # drop down list for anime style to be applied to image
    anime_style = home_page.selectbox (
            'Your preferred animation style:',
            ('Paprika', 'Shinkai', 'Hayao')
        )

    # just some more notes for the user
    with home_page.expander("🤔 What are these animation styles?", expanded=False):
        st.markdown("""
            These styles were derived from the works of various directors! Some of these might be familiar to you:   
            * Satoshi Kon: **Paprika**
            * Makoto **Shinkai**: Your Name, 5 Centimeters per Second, Weathering with You
            * **Hayao** Miyazaki: Spirited Away, My Neighbor Totoro, Princess Mononoke
        """)
        st.write("---")
        st.markdown("""
            🔍 Here are some sample images for you:
        """)

        st.image(Image.open('images/sample.png'), caption="Image from https://github.com/TachibanaYoshino/AnimeGAN")

    home_page.write("---")
    
    # stylize image
    home_page.markdown("If you're all set, then let's proceed! 😄")
    stylize_btn = home_page.button("Stylize!")

    # if "stylize" button is clicked,
    if stylize_btn:
        # remove processing page contents
        page_container.empty()

        with st.spinner('Hold on... Please do not close this tab....'):
            model_lock.acquire()

        # spinner (while processing image)
        with st.spinner('Hold on... Processing your image...'):
            # stylize input image and produce output
            output_image = style.stylize(anime_style, uploaded_image)
            model_lock.release()

        # step #3
        st.markdown("""
            ### Step #3: Download your image!
        """)

        # display original and output images
        st.markdown("""
            Here's a before and after!
        """)

        # prepare output image for downloading
        imageRGB = cv.cvtColor(output_image, cv.COLOR_BGR2RGB)
        img_encode = cv.imencode('.jpg', imageRGB)[1]
        data_encode = np.array(img_encode)
        byte_encode = data_encode.tobytes()

        before_col, after_col = st.columns(2)
        with before_col:
            # clamp and channels are used since OpenCV was used in processing the image
            st.image(uploaded_image, clamp=True, channels='RGB')

        with after_col:
            # clamp and channels are used since OpenCV was used in processing the image
            st.image(output_image, clamp=True, channels='RGB')

        col1, col2, col3, col4 = st.columns(4)

        with col1:
            pass
        with col2:
            # retry message
            # st.markdown('Not satisfied? Click this to retry!')
            # retry button
            retry_btn = st.button("Try another image")

            if retry_btn:
                page_container.empty()
        with col3:
            # some instruction for downloading
            # st.write("Finally, just download your _anime-fied_ image!")
            # download button
            st.download_button('Download Image', byte_encode, 'output.jpg', 'jpg')
        with col4:
            pass

        st.write("---")
        
        # randomizer. just another workaround.
        st.session_state['uploader_key'] = str(randint(1000, 100000000))