Javi commited on
Commit
5359e07
1 Parent(s): b502aa1

Added image preprocessing

Browse files
Files changed (1) hide show
  1. streamlit_app.py +22 -1
streamlit_app.py CHANGED
@@ -59,6 +59,25 @@ def is_valid_prediction_state(state: SessionState) -> bool:
59
  return True
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  class Sections:
63
  @staticmethod
64
  def header():
@@ -113,7 +132,9 @@ class Sections:
113
  if not accept_multiple_files:
114
  uploaded_images = [uploaded_images]
115
  for uploaded_image in uploaded_images:
116
- images.append(Image.open(uploaded_image))
 
 
117
  state.images = images
118
 
119
 
 
59
  return True
60
 
61
 
62
+ def preprocess_image(image: Image.Image, max_size: int = 1200) -> Image.Image:
63
+ """Set up a max size because otherwise the API sometimes breaks"""
64
+ width_0, height_0 = image.size
65
+
66
+ if max((width_0, height_0)) <= max_size:
67
+ return image
68
+
69
+ if width_0 > height_0:
70
+ aspect_ratio = max_size / float(width_0)
71
+ new_height = int(float(height_0) * float(aspect_ratio))
72
+ image = image.resize((max_size, new_height), Image.ANTIALIAS)
73
+ return image
74
+ else:
75
+ aspect_ratio = max_size / float(height_0)
76
+ new_width = int(float(width_0) * float(aspect_ratio))
77
+ image = image.resize((max_size, new_width), Image.ANTIALIAS)
78
+ return image
79
+
80
+
81
  class Sections:
82
  @staticmethod
83
  def header():
 
132
  if not accept_multiple_files:
133
  uploaded_images = [uploaded_images]
134
  for uploaded_image in uploaded_images:
135
+ pil_image = Image.open(uploaded_image)
136
+ pil_image = preprocess_image(pil_image)
137
+ images.append(pil_image)
138
  state.images = images
139
 
140