Wazzzabeee commited on
Commit
aedef5b
1 Parent(s): a4bad15

Update pages/03_🖼️_Input_Images.py

Browse files
Files changed (1) hide show
  1. pages/03_🖼️_Input_Images.py +75 -8
pages/03_🖼️_Input_Images.py CHANGED
@@ -1,12 +1,16 @@
1
  import streamlit as st
2
  from streamlit_lottie import st_lottie
3
- from models.deep_colorization.colorizers import *
 
4
  import requests
 
 
 
5
 
6
  st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")
7
 
8
 
9
- # Define a function that we can use to load lottie files from a link.
10
  def load_lottieurl(url: str):
11
  r = requests.get(url)
12
  if r.status_code != 200:
@@ -18,6 +22,7 @@ loaded_model = eccv16(pretrained=True).eval()
18
  current_model = "None"
19
 
20
 
 
21
  def change_model(current_model, model):
22
  if current_model != model:
23
  if model == "ECCV16":
@@ -36,9 +41,11 @@ with col1:
36
 
37
  with col2:
38
  st.write("""
39
- ## B&W Videos Colorizer
40
- ##### Input a YouTube black and white video link and get a colorized version of it.
41
- ###### I recommend starting with the first model and then experimenting with the second one.""")
 
 
42
 
43
 
44
  def main():
@@ -46,15 +53,75 @@ def main():
46
  "Select Model (Both models have their pros and cons, I recommend to try both and keep the best for you task)",
47
  ["ECCV16", "SIGGRAPH17"], index=0)
48
 
 
49
  loaded_model = change_model(current_model, model)
50
  st.write(f"Model is now {model}")
51
 
52
- link = st.text_input("YouTube Link (The longer the video, the longer the processing time)")
 
 
 
 
 
 
 
53
  if st.button("Colorize"):
54
- print("yo")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
  if __name__ == "__main__":
58
  main()
59
  st.markdown(
60
- "###### Made with :heart: by [Clément Delteil](https://www.linkedin.com/in/clementdelteil/) [![this is an image link](https://i.imgur.com/thJhzOO.png)](https://www.buymeacoffee.com/clementdelteil)")
 
 
1
  import streamlit as st
2
  from streamlit_lottie import st_lottie
3
+ from models.deep_colorization.colorizers import eccv16, siggraph17
4
+ from utils import colorize_image
5
  import requests
6
+ import os
7
+ import zipfile
8
+ from PIL import Image
9
 
10
  st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")
11
 
12
 
13
+ @st.cache_data()
14
  def load_lottieurl(url: str):
15
  r = requests.get(url)
16
  if r.status_code != 200:
 
22
  current_model = "None"
23
 
24
 
25
+ @st.cache_resource()
26
  def change_model(current_model, model):
27
  if current_model != model:
28
  if model == "ECCV16":
 
41
 
42
  with col2:
43
  st.write("""
44
+ ## B&W Images Colorizer
45
+ ##### Input a black and white image and get a colorized version of it.
46
+ ###### If you want to colorize multiple images just upload them all at once.
47
+ ###### ➠ Uploading already colored images won't raise errors but images won't look good.
48
+ ###### ➠ I recommend starting with the first model and then experimenting with the second one.""")
49
 
50
 
51
  def main():
 
53
  "Select Model (Both models have their pros and cons, I recommend to try both and keep the best for you task)",
54
  ["ECCV16", "SIGGRAPH17"], index=0)
55
 
56
+ # Make the user select a model
57
  loaded_model = change_model(current_model, model)
58
  st.write(f"Model is now {model}")
59
 
60
+ # Ask the user if he wants to see colorization
61
+ display_results = st.checkbox('Display results in real time', value=True)
62
+
63
+ # Input for the user to upload images
64
+ uploaded_file = st.file_uploader("Upload your photos here...", type=['jpg', 'png', 'jpeg'],
65
+ accept_multiple_files=True)
66
+
67
+ # If the user clicks on the button
68
  if st.button("Colorize"):
69
+ # If the user uploaded images
70
+ if uploaded_file is not None:
71
+ if display_results:
72
+ col1, col2 = st.columns([0.5, 0.5])
73
+ with col1:
74
+ st.markdown('<p style="text-align: center;">Before</p>', unsafe_allow_html=True)
75
+ with col2:
76
+ st.markdown('<p style="text-align: center;">After</p>', unsafe_allow_html=True)
77
+ else:
78
+ col1, col2, col3 = st.columns(3)
79
+
80
+ for i, file in enumerate(uploaded_file):
81
+ file_extension = os.path.splitext(file.name)[1].lower()
82
+ if file_extension in ['.jpg', '.png', '.jpeg']:
83
+ image = Image.open(file)
84
+ if display_results:
85
+ with col1:
86
+ st.image(image, use_column_width="always")
87
+ with col2:
88
+ with st.spinner("Colorizing image..."):
89
+ out_img, new_img = colorize_image(file, loaded_model)
90
+ new_img.save("IMG_" + str(i) + ".jpg")
91
+ st.image(out_img, use_column_width="always")
92
+
93
+ else:
94
+ out_img, new_img = colorize_image(file, loaded_model)
95
+ new_img.save("IMG_" + str(i + 1) + ".jpg")
96
+
97
+ if len(uploaded_file) > 1:
98
+ # Create a zip file
99
+ zip_filename = "colorized_images.zip"
100
+ with zipfile.ZipFile(zip_filename, "w") as zip_file:
101
+ # Add colorized images to the zip file
102
+ for i in range(len(uploaded_file)):
103
+ zip_file.write("IMG_" + str(i + 1) + ".jpg", "IMG_" + str(i) + ".jpg")
104
+ with col2:
105
+ # Provide the zip file data for download
106
+ st.download_button(
107
+ label="Download Colorized Images" if len(uploaded_file) > 1 else "Download Colorized Image",
108
+ data=open(zip_filename, "rb").read(),
109
+ file_name=zip_filename,
110
+ )
111
+ else:
112
+ with col2:
113
+ st.download_button(
114
+ label="Download Colorized Image",
115
+ data=open("IMG_1.jpg", "rb").read(),
116
+ file_name="IMG_1.jpg",
117
+ )
118
+
119
+ else:
120
+ st.warning('Upload a file', icon="⚠️")
121
 
122
 
123
  if __name__ == "__main__":
124
  main()
125
  st.markdown(
126
+ "###### Made with :heart: by [Clément Delteil](https://www.linkedin.com/in/clementdelteil/) [![this is an "
127
+ "image link](https://i.imgur.com/thJhzOO.png)](https://www.buymeacoffee.com/clementdelteil)")