davertor commited on
Commit
b31d3c9
β€’
1 Parent(s): 067974b

added multiple files processing

Browse files
__pycache__/app_utils.cpython-37.pyc ADDED
Binary file (3.35 kB). View file
app.py CHANGED
@@ -1,4 +1,4 @@
1
- #importing the libraries
2
  import os, sys, re
3
  import streamlit as st
4
  import PIL
@@ -6,19 +6,8 @@ from PIL import Image
6
  import cv2
7
  import numpy as np
8
  import uuid
9
-
10
- # Import torch libraries
11
- import fastai
12
- import torch
13
-
14
- # Import util functions from app_utils
15
- from app_utils import download
16
- from app_utils import generate_random_filename
17
- from app_utils import clean_me
18
- from app_utils import clean_all
19
- from app_utils import create_directory
20
- from app_utils import get_model_bin
21
- from app_utils import convertToJPG
22
 
23
  # Import util functions from deoldify
24
  # NOTE: This must be the first call in order to work properly!
@@ -28,6 +17,10 @@ from deoldify.device_id import DeviceId
28
  device.set(device=DeviceId.CPU)
29
  from deoldify.visualize import *
30
 
 
 
 
 
31
 
32
  ####### INPUT PARAMS ###########
33
  model_folder = 'models/'
@@ -66,42 +59,134 @@ def resize_img(input_img, max_size):
66
 
67
  return img
68
 
69
- def get_image_download_link(img,filename,text):
70
  button_uuid = str(uuid.uuid4()).replace('-', '')
71
  button_id = re.sub('\d+', '', button_uuid)
72
-
73
- custom_css = f"""
74
- <style>
75
- #{button_id} {{
76
- background-color: rgb(255, 255, 255);
77
- color: rgb(38, 39, 48);
78
- padding: 0.25em 0.38em;
79
- position: relative;
80
- text-decoration: none;
81
- border-radius: 4px;
82
- border-width: 1px;
83
- border-style: solid;
84
- border-color: rgb(230, 234, 241);
85
- border-image: initial;
86
-
87
- }}
88
- #{button_id}:hover {{
89
- border-color: rgb(246, 51, 102);
90
- color: rgb(246, 51, 102);
91
- }}
92
- #{button_id}:active {{
93
- box-shadow: none;
94
- background-color: rgb(246, 51, 102);
95
- color: white;
96
- }}
97
- </style> """
98
 
99
  buffered = BytesIO()
100
  img.save(buffered, format="JPEG")
101
  img_str = base64.b64encode(buffered.getvalue()).decode()
102
- href = custom_css + f'<a href="data:file/txt;base64,{img_str}" id="{button_id}" download="{filename}">{text}</a>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  return href
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  # General configuration
107
  # st.set_page_config(layout="centered")
@@ -136,32 +221,21 @@ st_color_option = st.sidebar.selectbox('Select colorizer mode',
136
  try:
137
  colorizer = load_model(model_folder, st_color_option)
138
  except:
139
- colorizer = None
140
- print('Error while loading the model. Please refresh the page')
 
141
 
142
  if colorizer is not None:
143
  st_title_message.markdown("**To begin, please upload an image** πŸ‘‡")
144
 
145
  #Choose your own image
146
- uploaded_file = st_file_uploader.file_uploader("Upload a black and white photo", type=['png', 'jpg', 'jpeg'])
147
-
148
- if uploaded_file is not None:
149
- img_name = uploaded_file.name
150
-
151
- pil_img = PIL.Image.open(uploaded_file)
152
- img_rgb = np.array(pil_img)
153
-
154
- resized_img_rgb = resize_img(img_rgb, max_img_size)
155
- resized_pil_img = PIL.Image.fromarray(resized_img_rgb)
156
-
157
- st_title_message.markdown("**Processing your image, please wait** βŒ›")
158
 
159
- output_pil_img = colorizer.plot_transformed_pil_image(resized_pil_img, render_factor=35, compare=False)
160
-
161
- st_title_message.markdown("**To begin, please upload an image** πŸ‘‡")
 
162
 
163
- # Plot images
164
- st_input_img.image(resized_pil_img, 'Input image', use_column_width=True)
165
- st_output_img.image(output_pil_img, 'Output image', use_column_width=True)
166
 
167
- st_download_button.markdown(get_image_download_link(output_pil_img, img_name, 'Download Image'), unsafe_allow_html=True)
1
+ # Import general purpose libraries
2
  import os, sys, re
3
  import streamlit as st
4
  import PIL
6
  import cv2
7
  import numpy as np
8
  import uuid
9
+ from zipfile import ZipFile, ZIP_DEFLATED
10
+ from io import BytesIO
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # Import util functions from deoldify
13
  # NOTE: This must be the first call in order to work properly!
17
  device.set(device=DeviceId.CPU)
18
  from deoldify.visualize import *
19
 
20
+ # Import util functions from app_utils
21
+ from app_utils import get_model_bin
22
+
23
+
24
 
25
  ####### INPUT PARAMS ###########
26
  model_folder = 'models/'
59
 
60
  return img
61
 
62
+ def get_image_download_link(img, filename, button_text):
63
  button_uuid = str(uuid.uuid4()).replace('-', '')
64
  button_id = re.sub('\d+', '', button_uuid)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  buffered = BytesIO()
67
  img.save(buffered, format="JPEG")
68
  img_str = base64.b64encode(buffered.getvalue()).decode()
69
+
70
+ return get_button_html_code(img_str, filename, 'txt', button_id, button_text)
71
+
72
+ def get_button_html_code(data_str, filename, filetype, button_id, button_txt='Download file'):
73
+ custom_css = f"""
74
+ <style>
75
+ #{button_id} {{
76
+ background-color: rgb(255, 255, 255);
77
+ color: rgb(38, 39, 48);
78
+ padding: 0.25em 0.38em;
79
+ position: relative;
80
+ text-decoration: none;
81
+ border-radius: 4px;
82
+ border-width: 1px;
83
+ border-style: solid;
84
+ border-color: rgb(230, 234, 241);
85
+ border-image: initial;
86
+
87
+ }}
88
+ #{button_id}:hover {{
89
+ border-color: rgb(246, 51, 102);
90
+ color: rgb(246, 51, 102);
91
+ }}
92
+ #{button_id}:active {{
93
+ box-shadow: none;
94
+ background-color: rgb(246, 51, 102);
95
+ color: white;
96
+ }}
97
+ </style> """
98
+
99
+ href = custom_css + f'<a href="data:file/{filetype};base64,{data_str}" id="{button_id}" download="{filename}">{button_txt}</a>'
100
  return href
101
 
102
+ def display_single_image(uploaded_file, img_size=800):
103
+ print('Type: ', type(uploaded_file))
104
+ st_title_message.markdown("**Processing your image, please wait** βŒ›")
105
+ img_name = uploaded_file.name
106
+
107
+ # Open the image
108
+ pil_img = PIL.Image.open(uploaded_file)
109
+ img_rgb = np.array(pil_img)
110
+ resized_img_rgb = resize_img(img_rgb, img_size)
111
+ resized_pil_img = PIL.Image.fromarray(resized_img_rgb)
112
+
113
+ # Send the image to the model
114
+ output_pil_img = colorizer.plot_transformed_pil_image(resized_pil_img, render_factor=35, compare=False)
115
+
116
+ # Plot images
117
+ st_input_img.image(resized_pil_img, 'Input image', use_column_width=True)
118
+ st_output_img.image(output_pil_img, 'Output image', use_column_width=True)
119
+
120
+ # Show download button
121
+ st_download_button.markdown(get_image_download_link(output_pil_img, img_name, 'Download Image'), unsafe_allow_html=True)
122
+
123
+ # Reset the message
124
+ st_title_message.markdown("**To begin, please upload an image** πŸ‘‡")
125
+
126
+ def process_multiple_images(uploaded_files, img_size=800):
127
+ num_imgs = len(uploaded_files)
128
+
129
+ output_images_list = []
130
+ img_names_list = []
131
+ idx = 1
132
+ for idx, uploaded_file in enumerate(uploaded_files, start=1):
133
+
134
+ st_title_message.markdown("**Processing image {}/{}. Please wait** βŒ›".format(idx,
135
+ num_imgs))
136
+
137
+ img_name = uploaded_file.name
138
+ img_type = uploaded_file.type
139
+
140
+ # Open the image
141
+ pil_img = PIL.Image.open(uploaded_file)
142
+ img_rgb = np.array(pil_img)
143
+ resized_img_rgb = resize_img(img_rgb, img_size)
144
+ resized_pil_img = PIL.Image.fromarray(resized_img_rgb)
145
+
146
+ # Send the image to the model
147
+ # output_pil_img = colorizer.plot_transformed_pil_image(resized_pil_img, render_factor=35, compare=False)
148
+ output_pil_img = resized_pil_img
149
+
150
+ output_images_list.append(output_pil_img)
151
+ img_names_list.append(img_name.split('.')[0])
152
+
153
+ # Zip output files
154
+ zip_path = 'processed_images.zip'
155
+ zip_buf = zip_multiple_images(output_images_list, img_names_list, zip_path)
156
+
157
+ st_download_button.download_button(
158
+ label='Download ZIP file',
159
+ data=zip_buf.read(),
160
+ file_name=zip_path,
161
+ mime="application/zip"
162
+ )
163
+
164
+ # Show message
165
+ st_title_message.markdown("**Images are ready for download** πŸ’Ύ")
166
+
167
+ def zip_multiple_images(pil_images_list, img_names_list, dest_path):
168
+ # Create zip file on memory
169
+ zip_buf = io.BytesIO()
170
+
171
+ with ZipFile(zip_buf, 'w', ZIP_DEFLATED) as zipObj:
172
+ for pil_img, img_name in zip(pil_images_list, img_names_list):
173
+ with io.BytesIO() as output:
174
+ # Save image in memory
175
+ pil_img.save(output, format="PNG")
176
+
177
+ # Read data
178
+ contents = output.getvalue()
179
+
180
+ # Write it to zip file
181
+ zipObj.writestr(img_name+".png", contents)
182
+ zip_buf.seek(0)
183
+ return zip_buf
184
+
185
+
186
+
187
+ ###########################
188
+ ###### STREAMLIT CODE #####
189
+ ###########################
190
 
191
  # General configuration
192
  # st.set_page_config(layout="centered")
221
  try:
222
  colorizer = load_model(model_folder, st_color_option)
223
  except:
224
+ pass
225
+ colorizer = True
226
+ # print('Error while loading the model. Please refresh the page')
227
 
228
  if colorizer is not None:
229
  st_title_message.markdown("**To begin, please upload an image** πŸ‘‡")
230
 
231
  #Choose your own image
232
+ uploaded_files = st_file_uploader.file_uploader("Upload a black and white photo",
233
+ type=['png', 'jpg', 'jpeg'],
234
+ accept_multiple_files=True)
 
 
 
 
 
 
 
 
 
235
 
236
+ if len(uploaded_files) == 1:
237
+ display_single_image(uploaded_files[0], max_img_size)
238
+ elif len(uploaded_files) > 1:
239
+ process_multiple_images(uploaded_files, max_img_size)
240
 
 
 
 
241
 
 
deoldify/__pycache__/__init__.cpython-37.pyc ADDED
Binary file (218 Bytes). View file
deoldify/__pycache__/_device.cpython-37.pyc ADDED
Binary file (1.32 kB). View file
deoldify/__pycache__/augs.cpython-37.pyc ADDED
Binary file (867 Bytes). View file
deoldify/__pycache__/critics.cpython-37.pyc ADDED
Binary file (1.52 kB). View file
deoldify/__pycache__/dataset.cpython-37.pyc ADDED
Binary file (1.53 kB). View file
deoldify/__pycache__/device_id.cpython-37.pyc ADDED
Binary file (510 Bytes). View file
deoldify/__pycache__/filters.cpython-37.pyc ADDED
Binary file (4.9 kB). View file
deoldify/__pycache__/generators.cpython-37.pyc ADDED
Binary file (3.15 kB). View file
deoldify/__pycache__/layers.cpython-37.pyc ADDED
Binary file (1.43 kB). View file
deoldify/__pycache__/loss.cpython-37.pyc ADDED
Binary file (6.47 kB). View file
deoldify/__pycache__/unet.cpython-37.pyc ADDED
Binary file (8.21 kB). View file
deoldify/__pycache__/visualize.cpython-37.pyc ADDED
Binary file (6.62 kB). View file