Spaces:
Build error
Build error
Create pages/03_🖼️_Input_Images.py
Browse files- pages/03_🖼️_Input_Images.py +111 -0
pages/03_🖼️_Input_Images.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from streamlit_lottie import st_lottie
|
| 7 |
+
|
| 8 |
+
from models.deep_colorization.colorizers import eccv16
|
| 9 |
+
from utils import colorize_image, change_model, load_lottieurl
|
| 10 |
+
|
| 11 |
+
st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
loaded_model = eccv16(pretrained=True).eval()
|
| 15 |
+
current_model = "None"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
col1, col2 = st.columns([1, 3])
|
| 19 |
+
with col1:
|
| 20 |
+
lottie = load_lottieurl("https://assets5.lottiefiles.com/packages/lf20_RHdEuzVfEL.json")
|
| 21 |
+
st_lottie(lottie)
|
| 22 |
+
|
| 23 |
+
with col2:
|
| 24 |
+
st.write("""
|
| 25 |
+
## B&W Images Colorizer
|
| 26 |
+
##### Input a black and white image and get a colorized version of it.
|
| 27 |
+
###### ➠ If you want to colorize multiple images just upload them all at once.
|
| 28 |
+
###### ➠ Uploading already colored images won't raise errors but images won't look good.
|
| 29 |
+
###### ➠ I recommend starting with the first model and then experimenting with the second one.""")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def main():
|
| 33 |
+
model = st.selectbox(
|
| 34 |
+
"Select Model (Both models have their pros and cons, I recommend trying both and keeping the best for you task)",
|
| 35 |
+
["ECCV16", "SIGGRAPH17"], index=0)
|
| 36 |
+
|
| 37 |
+
# Make the user select a model
|
| 38 |
+
loaded_model = change_model(current_model, model)
|
| 39 |
+
st.write(f"Model is now {model}")
|
| 40 |
+
|
| 41 |
+
# Ask the user if he wants to see colorization
|
| 42 |
+
display_results = st.checkbox('Display results in real time', value=True)
|
| 43 |
+
|
| 44 |
+
# Input for the user to upload images
|
| 45 |
+
uploaded_file = st.file_uploader("Upload your images here...", type=['jpg', 'png', 'jpeg'],
|
| 46 |
+
accept_multiple_files=True)
|
| 47 |
+
|
| 48 |
+
# If the user clicks on the button
|
| 49 |
+
if st.button("Colorize"):
|
| 50 |
+
# If the user uploaded images
|
| 51 |
+
if uploaded_file is not None:
|
| 52 |
+
if display_results:
|
| 53 |
+
col1, col2 = st.columns([0.5, 0.5])
|
| 54 |
+
with col1:
|
| 55 |
+
st.markdown('<p style="text-align: center;">Before</p>', unsafe_allow_html=True)
|
| 56 |
+
with col2:
|
| 57 |
+
st.markdown('<p style="text-align: center;">After</p>', unsafe_allow_html=True)
|
| 58 |
+
else:
|
| 59 |
+
col1, col2, col3 = st.columns(3)
|
| 60 |
+
|
| 61 |
+
for i, file in enumerate(uploaded_file):
|
| 62 |
+
file_extension = os.path.splitext(file.name)[1].lower()
|
| 63 |
+
if file_extension in ['.jpg', '.png', '.jpeg']:
|
| 64 |
+
image = Image.open(file)
|
| 65 |
+
if display_results:
|
| 66 |
+
with col1:
|
| 67 |
+
st.image(image, use_column_width="always")
|
| 68 |
+
with col2:
|
| 69 |
+
with st.spinner("Colorizing image..."):
|
| 70 |
+
out_img, new_img = colorize_image(file, loaded_model)
|
| 71 |
+
new_img.save("IMG_" + str(i+1) + ".jpg")
|
| 72 |
+
st.image(out_img, use_column_width="always")
|
| 73 |
+
|
| 74 |
+
else:
|
| 75 |
+
out_img, new_img = colorize_image(file, loaded_model)
|
| 76 |
+
new_img.save("IMG_" + str(i+1) + ".jpg")
|
| 77 |
+
|
| 78 |
+
if len(uploaded_file) > 1:
|
| 79 |
+
# Create a zip file
|
| 80 |
+
zip_filename = "colorized_images.zip"
|
| 81 |
+
with zipfile.ZipFile(zip_filename, "w") as zip_file:
|
| 82 |
+
# Add colorized images to the zip file
|
| 83 |
+
for i in range(len(uploaded_file)):
|
| 84 |
+
zip_file.write("IMG_" + str(i + 1) + ".jpg", "IMG_" + str(i) + ".jpg")
|
| 85 |
+
with col2:
|
| 86 |
+
# Provide the zip file data for download
|
| 87 |
+
st.download_button(
|
| 88 |
+
label="Download Colorized Images" if len(uploaded_file) > 1 else "Download Colorized Image",
|
| 89 |
+
data=open(zip_filename, "rb").read(),
|
| 90 |
+
file_name=zip_filename,
|
| 91 |
+
)
|
| 92 |
+
else:
|
| 93 |
+
with col2:
|
| 94 |
+
st.download_button(
|
| 95 |
+
label="Download Colorized Image",
|
| 96 |
+
data=open("IMG_1.jpg", "rb").read(),
|
| 97 |
+
file_name="IMG_1.jpg",
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
else:
|
| 101 |
+
st.warning('Upload a file', icon="⚠️")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
main()
|
| 106 |
+
st.markdown(
|
| 107 |
+
"###### Made with :heart: by [Clément Delteil](https://www.linkedin.com/in/clementdelteil/) [](https://www.buymeacoffee.com/clementdelteil)")
|
| 109 |
+
st.markdown(
|
| 110 |
+
"###### [Blog post of the project](https://medium.com/geekculture/creating-a-web-app-to-colorize-images-and-youtube-videos-80f5be2d0f68)"
|
| 111 |
+
)
|