fcakyon commited on
Commit
7d22bdf
1 Parent(s): 4d223e5

render images from base64 str instead of local

Browse files
Files changed (2) hide show
  1. app.py +1 -6
  2. utils.py +17 -26
app.py CHANGED
@@ -216,7 +216,7 @@ if submit:
216
  )
217
 
218
  st.markdown(f"##### YOLOX Standard vs SAHI Prediction:")
219
- static_component, image_1_path, image_2_path, htmlcode = imagecompare(
220
  output_1,
221
  output_2,
222
  label1="YOLOX",
@@ -226,8 +226,3 @@ if submit:
226
  show_labels=True,
227
  make_responsive=True,
228
  )
229
-
230
- st.write(htmlcode)
231
- st.write(pathlib.Path(image_1_path).exists())
232
- st.write(image_1_path)
233
- st.image(Image.open(image_1_path))
 
216
  )
217
 
218
  st.markdown(f"##### YOLOX Standard vs SAHI Prediction:")
219
+ static_component = imagecompare(
220
  output_1,
221
  output_2,
222
  label1="YOLOX",
 
226
  show_labels=True,
227
  make_responsive=True,
228
  )
 
 
 
 
 
utils.py CHANGED
@@ -1,14 +1,10 @@
1
  import streamlit.components.v1 as components
2
- import streamlit as st
3
  import numpy
4
  import sahi.predict
5
  import sahi.utils
6
  from PIL import Image
7
- import pathlib
8
- import os
9
- import uuid
10
-
11
- STREAMLIT_STATIC_PATH = pathlib.Path(st.__path__[0]) / "static"
12
 
13
 
14
  def sahi_mmdet_inference(
@@ -59,6 +55,15 @@ def sahi_mmdet_inference(
59
  return output_1, output_2
60
 
61
 
 
 
 
 
 
 
 
 
 
62
  def imagecompare(
63
  img1: str,
64
  img2: str,
@@ -94,27 +99,13 @@ def imagecompare(
94
  Returns a static component with a timeline
95
  """
96
  # prepare images
97
- for file_ in os.listdir(STREAMLIT_STATIC_PATH):
98
- if file_.endswith(".png") and "favicon" not in file_:
99
- os.remove(str(STREAMLIT_STATIC_PATH / file_))
100
-
101
- image_1_name = str(uuid.uuid4()) + ".png"
102
- image_1_path = STREAMLIT_STATIC_PATH / image_1_name
103
- image_1_path = str(image_1_path.resolve())
104
- sahi.utils.cv.read_image_as_pil(img1).save(image_1_path)
105
-
106
- image_2_name = str(uuid.uuid4()) + ".png"
107
- image_2_path = STREAMLIT_STATIC_PATH / image_2_name
108
- image_2_path = str(image_2_path.resolve())
109
- sahi.utils.cv.read_image_as_pil(img2).save(image_2_path)
110
-
111
- # image_1_name = "https://juxtapose.knightlab.com/static/img/Sochi_11April2005.jpg"
112
- # image_2_name = "https://juxtapose.knightlab.com/static/img/Sochi_22Nov2013.jpg"
113
-
114
  img_width, img_height = img1.size
115
  h_to_w = img_height / img_width
116
  height = (width * h_to_w) * 0.95
117
 
 
 
 
118
  # load css + js
119
  cdn_path = "https://cdn.knightlab.com/libs/juxtapose/latest"
120
  css_block = f'<link rel="stylesheet" href="{cdn_path}/css/juxtapose.css">'
@@ -129,11 +120,11 @@ def imagecompare(
129
  slider = new juxtapose.JXSlider('#foo',
130
  [
131
  {{
132
- src: '{image_1_name}',
133
  label: '{label1}',
134
  }},
135
  {{
136
- src: '{image_2_name}',
137
  label: '{label2}',
138
  }}
139
  ],
@@ -148,4 +139,4 @@ def imagecompare(
148
  """
149
  static_component = components.html(htmlcode, height=height, width=width)
150
 
151
- return static_component, image_1_path, image_2_path, htmlcode
 
1
  import streamlit.components.v1 as components
 
2
  import numpy
3
  import sahi.predict
4
  import sahi.utils
5
  from PIL import Image
6
+ import base64
7
+ import io
 
 
 
8
 
9
 
10
  def sahi_mmdet_inference(
 
55
  return output_1, output_2
56
 
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,
 
99
  Returns a static component with a timeline
100
  """
101
  # prepare images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  img_width, img_height = img1.size
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"
111
  css_block = f'<link rel="stylesheet" href="{cdn_path}/css/juxtapose.css">'
 
120
  slider = new juxtapose.JXSlider('#foo',
121
  [
122
  {{
123
+ src: '{img1}',
124
  label: '{label1}',
125
  }},
126
  {{
127
+ src: '{img2}',
128
  label: '{label2}',
129
  }}
130
  ],
 
139
  """
140
  static_component = components.html(htmlcode, height=height, width=width)
141
 
142
+ return static_component