fcakyon commited on
Commit
4452beb
β€’
1 Parent(s): 7d22bdf

stable version

Browse files
Files changed (2) hide show
  1. app.py +77 -73
  2. utils.py +44 -5
app.py CHANGED
@@ -3,34 +3,21 @@ import sahi.utils.mmdet
3
  import sahi.model
4
  from PIL import Image
5
  import random
6
- from utils import imagecompare
7
  from utils import sahi_mmdet_inference
8
- import pathlib
9
- import os
10
 
11
  MMDET_YOLACT_MODEL_URL = "https://download.openmmlab.com/mmdetection/v2.0/yolact/yolact_r50_1x8_coco/yolact_r50_1x8_coco_20200908-f38d58df.pth"
12
  MMDET_YOLOX_MODEL_URL = "https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_tiny_8x8_300e_coco/yolox_tiny_8x8_300e_coco_20210806_234250-4ff3b67e.pth"
13
  MMDET_FASTERRCNN_MODEL_URL = "https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_2x_coco/faster_rcnn_r50_fpn_2x_coco_bbox_mAP-0.384_20200504_210434-a5d8aa15.pth"
14
 
15
- # Images
16
- sahi.utils.file.download_from_url(
17
- "https://user-images.githubusercontent.com/34196005/142730935-2ace3999-a47b-49bb-83e0-2bdd509f1c90.jpg",
18
- "apple_tree.jpg",
19
- )
20
- sahi.utils.file.download_from_url(
21
- "https://user-images.githubusercontent.com/34196005/142730936-1b397756-52e5-43be-a949-42ec0134d5d8.jpg",
22
- "highway.jpg",
23
- )
24
-
25
- sahi.utils.file.download_from_url(
26
- "https://user-images.githubusercontent.com/34196005/142742871-bf485f84-0355-43a3-be86-96b44e63c3a2.jpg",
27
- "highway2.jpg",
28
- )
29
-
30
- sahi.utils.file.download_from_url(
31
- "https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg",
32
- "highway3.jpg",
33
- )
34
 
35
 
36
  @st.cache(allow_output_mutation=True, show_spinner=False)
@@ -72,6 +59,34 @@ def get_mmdet_model(model_name: str):
72
  return detection_model
73
 
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  st.set_page_config(
76
  page_title="Small Object Detection with SAHI + YOLOX",
77
  page_icon="πŸš€",
@@ -79,6 +94,19 @@ st.set_page_config(
79
  initial_sidebar_state="auto",
80
  )
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  st.markdown(
83
  """
84
  <h2 style='text-align: center'>
@@ -93,7 +121,7 @@ st.markdown(
93
  <p style='text-align: center'>
94
  <a href='https://github.com/obss/sahi' target='_blank'>SAHI Github</a> | <a href='https://github.com/open-mmlab/mmdetection/tree/master/configs/yolox' target='_blank'>YOLOX Github</a> | <a href='https://huggingface.co/spaces/fcakyon/sahi-yolov5' target='_blank'>SAHI+YOLOv5 Demo</a>
95
  <br />
96
- Follow me on <a href='https://twitter.com/fcakyon' target='_blank'>twitter</a>, <a href='https://www.linkedin.com/in/fcakyon/' target='_blank'>linkedin</a> and <a href='https://fcakyon.medium.com/' target='_blank'>medium</a> for more..
97
  </p>
98
  """,
99
  unsafe_allow_html=True,
@@ -105,10 +133,12 @@ col1, col2, col3 = st.columns([6, 1, 6])
105
  with col1:
106
  st.markdown(f"##### Set input image:")
107
 
 
108
  image_file = st.file_uploader(
109
  "Upload an image to test:", type=["jpg", "jpeg", "png"]
110
  )
111
 
 
112
  def slider_func(option):
113
  option_to_id = {
114
  "apple_tree.jpg": str(1),
@@ -122,9 +152,16 @@ with col1:
122
  "Or select from example images:",
123
  options=["apple_tree.jpg", "highway.jpg", "highway2.jpg", "highway3.jpg"],
124
  format_func=slider_func,
 
125
  )
126
- image = Image.open(slider)
127
- st.image(image, caption=slider, width=300)
 
 
 
 
 
 
128
  with col3:
129
  st.markdown(f"##### Set SAHI parameters:")
130
 
@@ -148,43 +185,6 @@ col1, col2, col3 = st.columns([6, 1, 6])
148
  with col2:
149
  submit = st.button("Submit")
150
 
151
- if image_file is not None:
152
- image = Image.open(image_file)
153
- else:
154
- image = Image.open(slider)
155
-
156
-
157
- class SpinnerTexts:
158
- def __init__(self):
159
- self.ind_history_list = []
160
- self.text_list = [
161
- "Meanwhile check out [MMDetection Colab notebook of SAHI](https://colab.research.google.com/github/obss/sahi/blob/main/demo/inference_for_mmdetection.ipynb)!",
162
- "Meanwhile check out [YOLOv5 Colab notebook of SAHI](https://colab.research.google.com/github/obss/sahi/blob/main/demo/inference_for_yolov5.ipynb)!",
163
- "Meanwhile check out [aerial object detection with SAHI](https://blog.ml6.eu/how-to-detect-small-objects-in-very-large-images-70234bab0f98?gi=b434299595d4)!",
164
- "Meanwhile check out [COCO Utilities of SAHI](https://github.com/obss/sahi/blob/main/docs/COCO.md)!",
165
- "Meanwhile check out [FiftyOne utilities of SAHI](https://github.com/obss/sahi#fiftyone-utilities)!",
166
- "Meanwhile [give a Github star to SAHI](https://github.com/obss/sahi/stargazers)!",
167
- "Meanwhile see [how easy is to install SAHI](https://github.com/obss/sahi#getting-started)!",
168
- "Meanwhile check out [Medium blogpost of SAHI](https://medium.com/codable/sahi-a-vision-library-for-performing-sliced-inference-on-large-images-small-objects-c8b086af3b80)!",
169
- "Meanwhile try out [YOLOv5 HF Spaces demo of SAHI](https://huggingface.co/spaces/fcakyon/sahi-yolov5)!",
170
- ]
171
-
172
- def _store(self, ind):
173
- if len(self.ind_history_list) == 6:
174
- self.ind_history_list.pop(0)
175
- self.ind_history_list.append(ind)
176
-
177
- def get(self):
178
- ind = 0
179
- while ind in self.ind_history_list:
180
- ind = random.randint(0, len(self.text_list) - 1)
181
- self._store(ind)
182
- return self.text_list[ind]
183
-
184
-
185
- if "last_spinner_texts" not in st.session_state:
186
- st.session_state["last_spinner_texts"] = SpinnerTexts()
187
-
188
  if submit:
189
  # perform prediction
190
  with st.spinner(
@@ -215,14 +215,18 @@ if submit:
215
  postprocess_class_agnostic=postprocess_class_agnostic,
216
  )
217
 
218
- st.markdown(f"##### YOLOX Standard vs SAHI Prediction:")
219
- static_component = imagecompare(
220
- output_1,
221
- output_2,
222
- label1="YOLOX",
223
- label2="SAHI+YOLOX",
224
- width=700,
225
- starting_position=50,
226
- show_labels=True,
227
- make_responsive=True,
228
- )
 
 
 
 
3
  import sahi.model
4
  from PIL import Image
5
  import random
6
+ from utils import image_compare
7
  from utils import sahi_mmdet_inference
 
 
8
 
9
  MMDET_YOLACT_MODEL_URL = "https://download.openmmlab.com/mmdetection/v2.0/yolact/yolact_r50_1x8_coco/yolact_r50_1x8_coco_20200908-f38d58df.pth"
10
  MMDET_YOLOX_MODEL_URL = "https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_tiny_8x8_300e_coco/yolox_tiny_8x8_300e_coco_20210806_234250-4ff3b67e.pth"
11
  MMDET_FASTERRCNN_MODEL_URL = "https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_2x_coco/faster_rcnn_r50_fpn_2x_coco_bbox_mAP-0.384_20200504_210434-a5d8aa15.pth"
12
 
13
+ IMAGE_TO_URL = {
14
+ "apple_tree.jpg": "https://user-images.githubusercontent.com/34196005/142730935-2ace3999-a47b-49bb-83e0-2bdd509f1c90.jpg",
15
+ "highway.jpg": "https://user-images.githubusercontent.com/34196005/142730936-1b397756-52e5-43be-a949-42ec0134d5d8.jpg",
16
+ "highway2.jpg": "https://user-images.githubusercontent.com/34196005/142742871-bf485f84-0355-43a3-be86-96b44e63c3a2.jpg",
17
+ "highway3.jpg": "https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg",
18
+ "highway2-yolox.jpg": "https://user-images.githubusercontent.com/34196005/143309873-c0c1f31c-c42e-4a36-834e-da0a2336bb19.jpg",
19
+ "highway2-sahi.jpg": "https://user-images.githubusercontent.com/34196005/143309867-42841f5a-9181-4d22-b570-65f90f2da231.jpg",
20
+ }
 
 
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  @st.cache(allow_output_mutation=True, show_spinner=False)
59
  return detection_model
60
 
61
 
62
+ class SpinnerTexts:
63
+ def __init__(self):
64
+ self.ind_history_list = []
65
+ self.text_list = [
66
+ "Meanwhile check out [MMDetection Colab notebook of SAHI](https://colab.research.google.com/github/obss/sahi/blob/main/demo/inference_for_mmdetection.ipynb)!",
67
+ "Meanwhile check out [YOLOv5 Colab notebook of SAHI](https://colab.research.google.com/github/obss/sahi/blob/main/demo/inference_for_yolov5.ipynb)!",
68
+ "Meanwhile check out [aerial object detection with SAHI](https://blog.ml6.eu/how-to-detect-small-objects-in-very-large-images-70234bab0f98?gi=b434299595d4)!",
69
+ "Meanwhile check out [COCO Utilities of SAHI](https://github.com/obss/sahi/blob/main/docs/COCO.md)!",
70
+ "Meanwhile check out [FiftyOne utilities of SAHI](https://github.com/obss/sahi#fiftyone-utilities)!",
71
+ "Meanwhile [give a Github star to SAHI](https://github.com/obss/sahi/stargazers)!",
72
+ "Meanwhile see [how easy is to install SAHI](https://github.com/obss/sahi#getting-started)!",
73
+ "Meanwhile check out [Medium blogpost of SAHI](https://medium.com/codable/sahi-a-vision-library-for-performing-sliced-inference-on-large-images-small-objects-c8b086af3b80)!",
74
+ "Meanwhile try out [YOLOv5 HF Spaces demo of SAHI](https://huggingface.co/spaces/fcakyon/sahi-yolov5)!",
75
+ ]
76
+
77
+ def _store(self, ind):
78
+ if len(self.ind_history_list) == 6:
79
+ self.ind_history_list.pop(0)
80
+ self.ind_history_list.append(ind)
81
+
82
+ def get(self):
83
+ ind = 0
84
+ while ind in self.ind_history_list:
85
+ ind = random.randint(0, len(self.text_list) - 1)
86
+ self._store(ind)
87
+ return self.text_list[ind]
88
+
89
+
90
  st.set_page_config(
91
  page_title="Small Object Detection with SAHI + YOLOX",
92
  page_icon="πŸš€",
94
  initial_sidebar_state="auto",
95
  )
96
 
97
+ if "last_spinner_texts" not in st.session_state:
98
+ st.session_state["last_spinner_texts"] = SpinnerTexts()
99
+
100
+ if "output_1" not in st.session_state:
101
+ st.session_state["output_1"] = sahi.utils.cv.read_image_as_pil(
102
+ IMAGE_TO_URL["highway2-yolox.jpg"]
103
+ )
104
+
105
+ if "output_2" not in st.session_state:
106
+ st.session_state["output_2"] = sahi.utils.cv.read_image_as_pil(
107
+ IMAGE_TO_URL["highway2-sahi.jpg"]
108
+ )
109
+
110
  st.markdown(
111
  """
112
  <h2 style='text-align: center'>
121
  <p style='text-align: center'>
122
  <a href='https://github.com/obss/sahi' target='_blank'>SAHI Github</a> | <a href='https://github.com/open-mmlab/mmdetection/tree/master/configs/yolox' target='_blank'>YOLOX Github</a> | <a href='https://huggingface.co/spaces/fcakyon/sahi-yolov5' target='_blank'>SAHI+YOLOv5 Demo</a>
123
  <br />
124
+ Follow me for more! <a href='https://twitter.com/fcakyon' target='_blank'>twitter</a> | <a href='https://www.linkedin.com/in/fcakyon/' target='_blank'>linkedin</a> | <a href='https://fcakyon.medium.com/' target='_blank'>medium</a>
125
  </p>
126
  """,
127
  unsafe_allow_html=True,
133
  with col1:
134
  st.markdown(f"##### Set input image:")
135
 
136
+ # set input image by upload
137
  image_file = st.file_uploader(
138
  "Upload an image to test:", type=["jpg", "jpeg", "png"]
139
  )
140
 
141
+ # set input image from exapmles
142
  def slider_func(option):
143
  option_to_id = {
144
  "apple_tree.jpg": str(1),
152
  "Or select from example images:",
153
  options=["apple_tree.jpg", "highway.jpg", "highway2.jpg", "highway3.jpg"],
154
  format_func=slider_func,
155
+ value="highway2.jpg",
156
  )
157
+
158
+ # visualize input image
159
+ if image_file is not None:
160
+ image = Image.open(image_file)
161
+ else:
162
+ image = sahi.utils.cv.read_image_as_pil(IMAGE_TO_URL[slider])
163
+ st.image(image, width=300)
164
+
165
  with col3:
166
  st.markdown(f"##### Set SAHI parameters:")
167
 
185
  with col2:
186
  submit = st.button("Submit")
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  if submit:
189
  # perform prediction
190
  with st.spinner(
215
  postprocess_class_agnostic=postprocess_class_agnostic,
216
  )
217
 
218
+ st.session_state["output_1"] = output_1
219
+ st.session_state["output_2"] = output_2
220
+
221
+ st.markdown(f"##### YOLOX Standard vs SAHI Prediction:")
222
+ static_component = image_compare(
223
+ img1=st.session_state["output_1"],
224
+ img2=st.session_state["output_2"],
225
+ label1="YOLOX",
226
+ label2="SAHI+YOLOX",
227
+ width=700,
228
+ starting_position=50,
229
+ show_labels=True,
230
+ make_responsive=True,
231
+ in_memory=True,
232
+ )
utils.py CHANGED
@@ -5,6 +5,10 @@ import sahi.utils
5
  from PIL import Image
6
  import base64
7
  import io
 
 
 
 
8
 
9
 
10
  def sahi_mmdet_inference(
@@ -57,14 +61,32 @@ def sahi_mmdet_inference(
57
 
58
  def pillow_to_base64(image: Image.Image):
59
  in_mem_file = io.BytesIO()
60
- image.save(in_mem_file, format="PNG")
61
  img_bytes = in_mem_file.getvalue() # bytes
62
  image_str = base64.b64encode(img_bytes).decode("utf-8")
63
- base64_src = f"data:image/png;base64,{image_str}"
 
 
 
 
 
 
 
 
 
64
  return base64_src
65
 
66
 
67
- def imagecompare(
 
 
 
 
 
 
 
 
 
68
  img1: str,
69
  img2: str,
70
  label1: str = "1",
@@ -73,6 +95,7 @@ def imagecompare(
73
  show_labels: bool = True,
74
  starting_position: int = 50,
75
  make_responsive: bool = True,
 
76
  ):
77
  """Create a new juxtapose component.
78
  Parameters
@@ -93,6 +116,8 @@ def imagecompare(
93
  Starting position of the slider as percent (0-100)
94
  make_responsive: bool or None
95
  Enable responsive mode
 
 
96
  Returns
97
  -------
98
  static_component: Boolean
@@ -103,8 +128,22 @@ def imagecompare(
103
  h_to_w = img_height / img_width
104
  height = (width * h_to_w) * 0.95
105
 
106
- img1 = pillow_to_base64(sahi.utils.cv.read_image_as_pil(img1))
107
- img2 = pillow_to_base64(sahi.utils.cv.read_image_as_pil(img2))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  # load css + js
110
  cdn_path = "https://cdn.knightlab.com/libs/juxtapose/latest"
5
  from PIL import Image
6
  import base64
7
  import io
8
+ import os
9
+ import uuid
10
+
11
+ TEMP_DIR = "temp"
12
 
13
 
14
  def sahi_mmdet_inference(
61
 
62
  def pillow_to_base64(image: Image.Image):
63
  in_mem_file = io.BytesIO()
64
+ image.save(in_mem_file, format="JPEG", subsampling=0, quality=100)
65
  img_bytes = in_mem_file.getvalue() # bytes
66
  image_str = base64.b64encode(img_bytes).decode("utf-8")
67
+ base64_src = f"data:image/jpg;base64,{image_str}"
68
+ return base64_src
69
+
70
+
71
+ def local_file_to_base64(image_path: str):
72
+ file_ = open(image_path, "rb")
73
+ img_bytes = file_.read()
74
+ image_str = base64.b64encode(img_bytes).decode("utf-8")
75
+ file_.close()
76
+ base64_src = f"data:image/jpg;base64,{image_str}"
77
  return base64_src
78
 
79
 
80
+ def pillow_local_file_to_base64(image: Image.Image):
81
+ # pillow to local file
82
+ img_path = TEMP_DIR + "/" + str(uuid.uuid4()) + ".jpg"
83
+ image.save(img_path, subsampling=0, quality=100)
84
+ # local file base64 str
85
+ base64_src = local_file_to_base64(img_path)
86
+ return base64_src
87
+
88
+
89
+ def image_compare(
90
  img1: str,
91
  img2: str,
92
  label1: str = "1",
95
  show_labels: bool = True,
96
  starting_position: int = 50,
97
  make_responsive: bool = True,
98
+ in_memory=False,
99
  ):
100
  """Create a new juxtapose component.
101
  Parameters
116
  Starting position of the slider as percent (0-100)
117
  make_responsive: bool or None
118
  Enable responsive mode
119
+ in_memory: bool or None
120
+ Handle pillow to base64 conversion in memory without saving to local
121
  Returns
122
  -------
123
  static_component: Boolean
128
  h_to_w = img_height / img_width
129
  height = (width * h_to_w) * 0.95
130
 
131
+ img1_pillow = sahi.utils.cv.read_image_as_pil(img1)
132
+ img2_pillow = sahi.utils.cv.read_image_as_pil(img2)
133
+
134
+ if in_memory:
135
+ # create base64 str from pillow images
136
+ img1 = pillow_to_base64(img1_pillow)
137
+ img2 = pillow_to_base64(img2_pillow)
138
+ else:
139
+ # clean temp dir
140
+ os.makedirs(TEMP_DIR, exist_ok=True)
141
+ for file_ in os.listdir(TEMP_DIR):
142
+ if file_.endswith(".jpg"):
143
+ os.remove(TEMP_DIR + "/" + file_)
144
+ # create base64 str from pillow images
145
+ img1 = pillow_local_file_to_base64(img1_pillow)
146
+ img2 = pillow_local_file_to_base64(img2_pillow)
147
 
148
  # load css + js
149
  cdn_path = "https://cdn.knightlab.com/libs/juxtapose/latest"