sunwaee commited on
Commit
0516a4f
1 Parent(s): bd7b3a7

updated page for uploaded files

Browse files
app.py CHANGED
@@ -7,6 +7,7 @@ import gdown
7
  import numpy as np
8
  import streamlit as st
9
  import torch
 
10
 
11
 
12
  def load_classes(csv_reader):
@@ -105,7 +106,7 @@ def load_model(model_path, prefix: str = 'model/'):
105
  return model
106
 
107
 
108
- def process_img(model, image, labels, caption: bool = True):
109
  """
110
  Process img given a model.
111
 
@@ -113,6 +114,7 @@ def process_img(model, image, labels, caption: bool = True):
113
  :param image: image to process
114
  :param model: inference model
115
  :param labels: given labels
 
116
  :return:
117
  """
118
 
@@ -174,7 +176,8 @@ def process_img(model, image, labels, caption: bool = True):
174
  }
175
  cap = '{}'.format(label_name) if caption else ''
176
  draw_caption(image_orig, (x1, y1, x2, y2), cap)
177
- cv2.rectangle(image_orig, (x1, y1), (x2, y2), color=colors[label_name], thickness=2)
 
178
 
179
  return image_orig
180
 
@@ -194,6 +197,7 @@ ids = {
194
  # Download all models from drive
195
  download_models(ids)
196
 
 
197
  left, right = st.columns([5, 3])
198
 
199
  # Model selection
@@ -204,15 +208,23 @@ model = load_model(model_path=model_path) if model_path != '' else None
204
  # Display example selection
205
  index = left.number_input('', min_value=0, max_value=852, value=495, help='Choose an image. ')
206
 
207
- # Get corresponding image and transform it
208
- image = cv2.imread(f'data/validation/image/maksssksksss{str(index)}.jpg')
209
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
 
 
 
 
 
 
 
210
 
211
  # Process img
212
  with st.spinner('Please wait while the image is being processed... This may take a while. '):
213
  image = process_img(model, image, labels, caption=False)
214
 
215
- left.image(image)
216
 
217
  # Write labels dict and device on right
218
  right.write({
@@ -222,3 +234,9 @@ right.write({
222
  })
223
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
224
  right.write(device)
 
 
 
 
 
 
7
  import numpy as np
8
  import streamlit as st
9
  import torch
10
+ from PIL import Image
11
 
12
 
13
  def load_classes(csv_reader):
106
  return model
107
 
108
 
109
+ def process_img(model, image, labels, caption: bool = True, thickness=2):
110
  """
111
  Process img given a model.
112
 
114
  :param image: image to process
115
  :param model: inference model
116
  :param labels: given labels
117
+ :param thickness: thickness of bboxes
118
  :return:
119
  """
120
 
176
  }
177
  cap = '{}'.format(label_name) if caption else ''
178
  draw_caption(image_orig, (x1, y1, x2, y2), cap)
179
+ cv2.rectangle(image_orig, (x1, y1), (x2, y2), color=colors[label_name],
180
+ thickness=int(1 * (smallest_side / 100)))
181
 
182
  return image_orig
183
 
197
  # Download all models from drive
198
  download_models(ids)
199
 
200
+ # Split page into columns
201
  left, right = st.columns([5, 3])
202
 
203
  # Model selection
208
  # Display example selection
209
  index = left.number_input('', min_value=0, max_value=852, value=495, help='Choose an image. ')
210
 
211
+ # Uploader
212
+ uploaded = st.file_uploader("Try it out with your own image!", type=['.jpg', '.png', '.jfif'])
213
+
214
+ if uploaded is not None:
215
+ # Convert file to image
216
+ image = Image.open(uploaded)
217
+ image = np.array(image)
218
+ else:
219
+ # Get corresponding image and transform it
220
+ image = cv2.imread(f'data/validation/image/maksssksksss{str(index)}.jpg')
221
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
222
 
223
  # Process img
224
  with st.spinner('Please wait while the image is being processed... This may take a while. '):
225
  image = process_img(model, image, labels, caption=False)
226
 
227
+ left.image(cv2.resize(image, (450, 300)))
228
 
229
  # Write labels dict and device on right
230
  right.write({
234
  })
235
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
236
  right.write(device)
237
+
238
+ captions = [image for image in os.listdir('data/examples/')]
239
+ images = [Image.open(f'data/examples/{image}') for image in os.listdir('data/examples/')]
240
+
241
+ # Display examples
242
+ st.image(images, width=350)
data/examples/ex1.jfif ADDED
Binary file (102 kB). View file
data/examples/ex3.jpg ADDED
data/examples/ex5.jfif ADDED
Binary file (173 kB). View file
data/examples/ex6.jpg ADDED
data/examples/ex7.jpg ADDED
data/examples/ex8.jpeg ADDED