LiuZichen commited on
Commit
79ecf3f
1 Parent(s): 1a1aace

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -9
app.py CHANGED
@@ -100,7 +100,6 @@ def prepare_images_and_masks(total_mask, original_image, add_color_image, add_ed
100
  return add_color_image_tensor, original_image_tensor, total_mask, add_edge_mask, remove_edge_mask
101
 
102
  def guess_prompt_handler(original_image, add_color_image, add_edge_image):
103
- # 将张量转换为 NumPy 数组
104
  original_image_tensor = load_and_preprocess_image(original_image)
105
 
106
  if add_color_image:
@@ -114,33 +113,27 @@ def guess_prompt_handler(original_image, add_color_image, add_edge_image):
114
  original_image_numpy = tensor_to_numpy(original_image_tensor.squeeze(0))
115
  add_color_image_numpy = tensor_to_numpy(add_color_image_tensor.squeeze(0))
116
  add_edge_mask_numpy = tensor_to_numpy(add_edge_mask.squeeze(0).unsqueeze(-1))
117
-
118
  original_image_numpy = cv2.cvtColor(original_image_numpy, cv2.COLOR_RGB2BGR)
119
  add_color_image_numpy = cv2.cvtColor(add_color_image_numpy, cv2.COLOR_RGB2BGR)
120
-
121
- # 创建临时文件,保存 NumPy 数组为图像文件
122
  original_image_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png", mode='w+b')
123
  add_color_image_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png", mode='w+b')
124
  add_edge_mask_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png", mode='w+b')
125
 
126
- # 保存 NumPy 数组为 PNG 图像文件
127
  cv2.imwrite(original_image_file.name, original_image_numpy)
128
  cv2.imwrite(add_color_image_file.name, add_color_image_numpy)
129
  cv2.imwrite(add_edge_mask_file.name, add_edge_mask_numpy)
130
-
131
- # 确保文件关闭以保证内容写入磁盘
132
  original_image_file.close()
133
  add_color_image_file.close()
134
  add_edge_mask_file.close()
135
 
136
- # 调用 API,传递临时文件的路径
137
  res = client.predict(
138
  handle_file(original_image_file.name),
139
  handle_file(add_color_image_file.name),
140
  handle_file(add_edge_mask_file.name)
141
  )
142
 
143
- # 删除临时文件,确保它们存在且已被创建
144
  if original_image_file and os.path.exists(original_image_file.name):
145
  os.remove(original_image_file.name)
146
  if add_color_image_file and os.path.exists(add_color_image_file.name):
 
100
  return add_color_image_tensor, original_image_tensor, total_mask, add_edge_mask, remove_edge_mask
101
 
102
  def guess_prompt_handler(original_image, add_color_image, add_edge_image):
 
103
  original_image_tensor = load_and_preprocess_image(original_image)
104
 
105
  if add_color_image:
 
113
  original_image_numpy = tensor_to_numpy(original_image_tensor.squeeze(0))
114
  add_color_image_numpy = tensor_to_numpy(add_color_image_tensor.squeeze(0))
115
  add_edge_mask_numpy = tensor_to_numpy(add_edge_mask.squeeze(0).unsqueeze(-1))
 
116
  original_image_numpy = cv2.cvtColor(original_image_numpy, cv2.COLOR_RGB2BGR)
117
  add_color_image_numpy = cv2.cvtColor(add_color_image_numpy, cv2.COLOR_RGB2BGR)
118
+
 
119
  original_image_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png", mode='w+b')
120
  add_color_image_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png", mode='w+b')
121
  add_edge_mask_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png", mode='w+b')
122
 
 
123
  cv2.imwrite(original_image_file.name, original_image_numpy)
124
  cv2.imwrite(add_color_image_file.name, add_color_image_numpy)
125
  cv2.imwrite(add_edge_mask_file.name, add_edge_mask_numpy)
126
+
 
127
  original_image_file.close()
128
  add_color_image_file.close()
129
  add_edge_mask_file.close()
130
 
 
131
  res = client.predict(
132
  handle_file(original_image_file.name),
133
  handle_file(add_color_image_file.name),
134
  handle_file(add_edge_mask_file.name)
135
  )
136
 
 
137
  if original_image_file and os.path.exists(original_image_file.name):
138
  os.remove(original_image_file.name)
139
  if add_color_image_file and os.path.exists(add_color_image_file.name):