File size: 6,024 Bytes
9e86fac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753aec8
9e86fac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185b25b
9e86fac
185b25b
 
9e86fac
185b25b
 
9e86fac
 
 
 
 
 
2c4099a
9e86fac
2c4099a
 
 
 
 
 
 
9e86fac
 
2c4099a
9e86fac
2c4099a
 
9e86fac
2c4099a
 
9e86fac
2c4099a
 
 
 
 
 
 
9e86fac
 
 
 
 
 
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
import math
import time
import numpy as np
import streamlit as st
from PIL import Image
import cv2

hide_streamlit_style = """
<style>
    #root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0rem;
    padding-left: 1%;
    }
</style>

"""

st.set_page_config(layout="wide")



def loadModel(n):
    super_res = cv2.dnn_superres.DnnSuperResImpl_create()
    super_res.readModel('models/ESPCN_x'+n+'.pb')
    return super_res

# on removing (show_spinner=False), it will show that fuction is running on web app
@st.experimental_memo(show_spinner=False)
def upscale(file,task):
    with open(file.name, "wb") as f:
        f.write(file.getbuffer())
        print('No file found, so added in list files')
    if isinstance(task,str):
        super_res = loadModel(task)
        super_res.setModel('espcn', int(task))
        if file.type.split('/')[0] == 'image':
            img = cv2.imread(file.name)
            upscaled_image = super_res.upsample(img)
            print('I upscaled upto',task,'times')
            cv2.imwrite("processed_"+file.name,upscaled_image)
        return True
    else:
        req_width,req_height = int(task[0]),int(task[1])
        if file.type.split('/')[0] == 'image':
            img = cv2.imread(file.name)
            actual_width,actual_height = img.shape[1],img.shape[0]
            w_ratio,h_ratio = req_width/actual_width , req_height/actual_height
            if min([w_ratio,h_ratio]) <= 1.0:
                img = cv2.resize(img,(req_width,req_height))
                print("I did resizing only!")
                cv2.imwrite("processed_" + file.name, img)
                return True
            # rounding off the ratios
            w_ratio,h_ratio = math.ceil(w_ratio),math.ceil(h_ratio)
            # find bigger number
            upscale_number = max(w_ratio,h_ratio)

            # task can be greater than 4 but we can upscale upto 4. So setting task to 4.
            if upscale_number >= 4:
                upscale_number = 4

            super_res = loadModel(str(upscale_number))
            super_res.setModel('espcn', int(upscale_number))
            upscaled_image = super_res.upsample(img)
            print("Before resizing ",(upscaled_image.shape[1], upscaled_image.shape[0]))
            upscaled_image = cv2.resize(upscaled_image,(task[0],task[1]))
            print("Final size got: ",(upscaled_image.shape[1],upscaled_image.shape[0]))

            print("I upscale upto", upscale_number , "times and then resize it.")

            cv2.imwrite("processed_" + file.name, upscaled_image)

            return True

        return "It's second"


if 'disable_opt2' not in st.session_state:
    st.session_state.disable_opt2 = True
if 'disable_opt1' not in st.session_state:
    st.session_state.disable_opt1 = False
if 'disable_download' not in st.session_state:
    st.session_state.disable_download = True


st.markdown(hide_streamlit_style, unsafe_allow_html=True)

col1,_,col2 = st.columns([6,1,3],gap="small")

def toggle_state_opt1():

    if st.session_state.get("opt1") == True:
        st.session_state.opt2 = False
        st.session_state.disable_opt2 = True

    else:
        st.session_state.opt2 = True
        st.session_state.disable_opt2 = False

def toggle_state_opt2():
    if st.session_state.get("opt2") == True:
        st.session_state.opt1 = False
        st.session_state.disable_opt1 = True
    else:
        st.session_state.opt1 = True
        st.session_state.disable_opt1 = False

# Update the states based on user selection before drawing the widgets in the web page
toggle_state_opt2()
toggle_state_opt1()

with col1:
    file = st.file_uploader("",type=['png','jpeg','jpg','pgm','jpe'])
    if file is not None:
        # writing file and saving its details in dict for further processing



        if file.type.split('/')[0] == "image":
            image = Image.open(file)
            st.image(image,caption="Upload Image", use_column_width=True)
        elif file.type.split('/')[0] == 'video':
            st.video(file)


with col2:
    st.markdown("\n")
    st.markdown("\n")
    st.markdown("\n")

    st.subheader(" UPSCALE RESOLUTION UP TO")
    st.markdown("\n")
    st.markdown("\n")

    opt1 = st.checkbox("MULTIPLES OF",key="opt1",value=True,on_change=toggle_state_opt1)
    st.selectbox("SELECT", ["2", "3", "4"],key="opt1_selBox",disabled=st.session_state.disable_opt1)

    st.markdown("\n")
    st.markdown("\n")
    opt2 = st.checkbox("CUSTOM SIZE",key="opt2",on_change=toggle_state_opt2)

    

    
    st.number_input("Width", step=1, min_value=150,max_value=3840, value=900, key="width",disabled=st.session_state.disable_opt2)

    
    st.number_input("Height", step=1, min_value=150,max_value=2160, value=900, key="height",disabled=st.session_state.disable_opt2)

    st.markdown("\n")
    st.markdown("\n")

    _, dcol, _ = st.columns([1,5,1],gap="small")

    

    if st.button("PROCEED",use_container_width=True) and file is not None:
        if st.session_state.get('opt1') == True:
            task = st.session_state.opt1_selBox
        else:
            task = [st.session_state.width, st.session_state.height]
        print(task)
        st.session_state.disable_download = not upscale(file,task)


        #print(resulted_file.shape)

    st.markdown("\n")
    st.markdown("\n")

    if file is None:
        st.session_state.disable_download = True

    if st.session_state.disable_download == True:
        st.button("DOWNLOAD FILE",disabled=True,use_container_width=True)
    else:
        with open('processed_'+file.name, "rb") as download_file:
            st.download_button(label="Download image", data=download_file,
                                file_name= 'processed_'+file.name, mime= "image/png",
                                use_container_width=True, disabled=st.session_state.disable_download)



st.markdown("\n")
st.markdown("\n")
st.info("DESCRIPTION :   This web app is a free tool that allow user to upscale or resize media file resolution.")