jinfengxie commited on
Commit
e221ef4
·
verified ·
1 Parent(s): 9e3d231

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -185,10 +185,11 @@ def predict_and_visualize(image):
185
  height,width,_=image_np.shape
186
  maxhl=max(height,width)
187
  image = tf.convert_to_tensor(image_np, dtype=tf.float32)
188
- if maxhl==height:
189
- image=tf.image.resize(image,(1500,int(1500*width/height)))
190
- if maxhl==width:
191
- image=tf.image.resize(image,(int(1500*height/width),1500))
 
192
  #image = tf.image.resize_with_pad(image, 1500, 1500)
193
  image = tf.cast(image, tf.float32) / 255.0
194
  image = tf.transpose(image, perm=[2, 0, 1])
@@ -219,12 +220,13 @@ def predict_and_visualize(image):
219
  color_mask = color_mask.astype(np.uint8)
220
  result_image = Image.fromarray(color_mask)
221
  draw = ImageDraw.Draw(result_image)
222
- font = ImageFont.truetype("arial.ttf", int(height/40)) # 尝试加载Arial字体,大小为12
223
 
224
  for key, position in label_positions.items():
225
  if key in id2label:
226
  # 绘制文本,您可能需要调整文本位置和字体大小
227
- draw.text((position[0], position[1]), str(key), font=font, fill='white')
 
228
 
229
  return pred_mask,result_image,counts_dict
230
 
@@ -334,7 +336,7 @@ def plot_material_color_palette_grid(material_dict, materials_per_row=4):
334
  # 将matplotlib图转换为图像
335
  def plt_to_image():
336
  buf = io.BytesIO()
337
- plt.savefig(buf, format='png')
338
  plt.close()
339
  buf.seek(0)
340
  img = Image.open(buf)
 
185
  height,width,_=image_np.shape
186
  maxhl=max(height,width)
187
  image = tf.convert_to_tensor(image_np, dtype=tf.float32)
188
+ if maxhl>1500:
189
+ if maxhl==height:
190
+ image=tf.image.resize(image,(1500,int(1500*width/height)))
191
+ if maxhl==width:
192
+ image=tf.image.resize(image,(int(1500*height/width),1500))
193
  #image = tf.image.resize_with_pad(image, 1500, 1500)
194
  image = tf.cast(image, tf.float32) / 255.0
195
  image = tf.transpose(image, perm=[2, 0, 1])
 
220
  color_mask = color_mask.astype(np.uint8)
221
  result_image = Image.fromarray(color_mask)
222
  draw = ImageDraw.Draw(result_image)
223
+ font = ImageFont.truetype("arial.ttf", int(height/30)) # 尝试加载Arial字体,大小为12
224
 
225
  for key, position in label_positions.items():
226
  if key in id2label:
227
  # 绘制文本,您可能需要调整文本位置和字体大小
228
+ material=id2material[key]
229
+ draw.text((position[0], position[1]), str(material), font=font, fill='white')
230
 
231
  return pred_mask,result_image,counts_dict
232
 
 
336
  # 将matplotlib图转换为图像
337
  def plt_to_image():
338
  buf = io.BytesIO()
339
+ plt.savefig(buf, format='png',dpi=300)
340
  plt.close()
341
  buf.seek(0)
342
  img = Image.open(buf)