muhammadzain's picture
Update app.py
c470f1d
raw
history blame
6.41 kB
import math
import time
import numpy as np
import streamlit as st
from PIL import Image
import cv2
st.set_page_config(layout="wide")
hide_streamlit_style = """
<style>
#root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0rem;
padding-left: 1%;
}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
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
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(43*"&nbsp;"+"PROCEED"+"&nbsp;"*43) 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(36*"&nbsp;"+"DOWNLOAD FILE"+"&nbsp;"*36,disabled=True)
else:
with open('processed_'+file.name, "rb") as download_file:
st.download_button(label=36*"&nbsp;"+"DOWNLOAD FILE"+"&nbsp;"*36, data=download_file,
file_name= 'processed_'+file.name, mime= "image/png",
disabled=st.session_state.disable_download)
st.markdown("\n")
st.markdown("\n")
st.markdown("\n")
st.info("DESCRIPTION : This web app is a free tool a free tool designed to enhance your media experience by allowing users to upscale or resize image resolution.
While the app is still undergoing development, I am delighted to offer you the opportunity. In the event that you encounter any issues while using our web application,
please accept my apologies..\n I welcome your feedback and suggestions, and encourage you to contact me at zain.18j2000@gmail.com to share your thoughts.")