jenhung commited on
Commit
16f5238
·
1 Parent(s): 4bcb6cb

Add KDE func

Browse files
Files changed (1) hide show
  1. web_app.py +23 -16
web_app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import cv2
 
3
  import pandas as pd
4
  import PIL.Image as Image
5
  import gradio as gr
@@ -107,7 +108,7 @@ def predict_image(name, model, img, conf_threshold, iou_threshold):
107
  file_name.append(file_label)
108
 
109
  ### ============================
110
- """
111
  kde = KernelDensity(metric='euclidean', kernel='gaussian', algorithm='ball_tree')
112
 
113
  # Finding Optimal Bandwidth
@@ -124,7 +125,8 @@ def predict_image(name, model, img, conf_threshold, iou_threshold):
124
  (tf - ti)))
125
  kde.bandwidth = bw
126
  _ = kde.fit(cno_coor)
127
-
 
128
  xgrid = np.arange(0, result.orig_img.shape[1], 1)
129
  ygrid = np.arange(0, result.orig_img.shape[0], 1)
130
  xv, yv = np.meshgrid(xgrid, ygrid)
@@ -152,7 +154,7 @@ def predict_image(name, model, img, conf_threshold, iou_threshold):
152
  if layer_area == 0:
153
  density = np.round(0.0, 4)
154
  else:
155
- density = np.round((ecno / layer_area) * 512 * 512 / 400, 4)
156
  print("Level {}: Area={}, CNO={}, density={}".format(j, layer_area, ecno, density))
157
  single_layer_area.append(layer_area)
158
  single_layer_cno.append(ecno)
@@ -171,20 +173,24 @@ def predict_image(name, model, img, conf_threshold, iou_threshold):
171
  plt.gca().invert_yaxis()
172
  plt.xlim(0, gdim[1] - 1)
173
  plt.ylim(gdim[0] - 1, 0)
174
- kde_image.append([plt.figure(), file_label])
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  #plt.savefig(os.path.join(kde_dir, '{}_{}_{}_KDE.png'.format(file_list[idx], model_type, conf)),
176
  # bbox_inches='tight', pad_inches=0)
177
- """
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
 
 
188
  ### ============================
189
 
190
  data = {
@@ -306,5 +312,6 @@ iface = gr.Interface(
306
 
307
  if __name__ == '__main__':
308
  # iface.launch()
309
- app.launch(share=False, auth=[('jenhw', 'admin'), ('user', 'admin')], auth_message="Enter your username and password")
310
- # app.launch(share=True)
 
 
1
  import os
2
  import cv2
3
+ import io
4
  import pandas as pd
5
  import PIL.Image as Image
6
  import gradio as gr
 
108
  file_name.append(file_label)
109
 
110
  ### ============================
111
+
112
  kde = KernelDensity(metric='euclidean', kernel='gaussian', algorithm='ball_tree')
113
 
114
  # Finding Optimal Bandwidth
 
125
  (tf - ti)))
126
  kde.bandwidth = bw
127
  _ = kde.fit(cno_coor)
128
+ print("deb", result.orig_img.shape[1])
129
+ print("deb", result.orig_img.shape[0])
130
  xgrid = np.arange(0, result.orig_img.shape[1], 1)
131
  ygrid = np.arange(0, result.orig_img.shape[0], 1)
132
  xv, yv = np.meshgrid(xgrid, ygrid)
 
154
  if layer_area == 0:
155
  density = np.round(0.0, 4)
156
  else:
157
+ density = np.round((ecno / layer_area) * result.orig_img.shape[0] * result.orig_img.shape[1] / 400, 4)
158
  print("Level {}: Area={}, CNO={}, density={}".format(j, layer_area, ecno, density))
159
  single_layer_area.append(layer_area)
160
  single_layer_cno.append(ecno)
 
173
  plt.gca().invert_yaxis()
174
  plt.xlim(0, gdim[1] - 1)
175
  plt.ylim(gdim[0] - 1, 0)
176
+ plt.plot()
177
+ # plt.show()
178
+
179
+ # plt.savefig("test.png", format='png', bbox_inches='tight', pad_inches=0)
180
+ # plt.figure()
181
+ # plt.plot([1, 2])
182
+ img_buf = io.BytesIO()
183
+ plt.savefig(img_buf, format='png', bbox_inches='tight', pad_inches=0)
184
+ kde_im = Image.open(img_buf)
185
+ # kde_im.show()
186
+
187
+ # kde_img = Image.frombytes('RGB', fig.canvas.get_width_height(), fig.canvas.tostring_rgb())
188
+ # kde_image.append([imgplot, file_label])
189
+ kde_image.append([kde_im, file_label])
190
  #plt.savefig(os.path.join(kde_dir, '{}_{}_{}_KDE.png'.format(file_list[idx], model_type, conf)),
191
  # bbox_inches='tight', pad_inches=0)
 
 
 
 
 
 
 
 
 
 
192
 
193
+ #img_buf.close()
194
  ### ============================
195
 
196
  data = {
 
312
 
313
  if __name__ == '__main__':
314
  # iface.launch()
315
+ # app.launch(share=False, auth=[('jenhw', 'admin'), ('user', 'admin')],
316
+ # auth_message="Enter your username and password")
317
+ app.launch(share=False)