plutosss commited on
Commit
b15c316
·
verified ·
1 Parent(s): 75fd403

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -18
app.py CHANGED
@@ -4,9 +4,12 @@
4
  # 输出图片
5
  # 使用 depth-anything + teed 生成外轮廓
6
  # 使用 teed + 腐蚀算法 生成内边缘
 
 
7
  from PIL import Image
8
 
9
  import cv2
 
10
  import numpy as np
11
  import gradio as gr
12
  import os
@@ -363,33 +366,51 @@ def process_images(input_folder='./output/merged_imgs'):
363
  raise ValueError(f"Failed to read image: {image_file}")
364
  except Exception as e:
365
  print(f"Error processing file {image_file}: {e}")
366
-
367
  def process_line(input_files):
368
- processed_images = []
369
- errors = []
370
-
371
  try:
 
 
 
 
 
 
 
 
372
  for img_path in input_files:
373
  img_path = img_path.name # 获取文件路径
374
- # 调用处理函数
375
- depth_anything(img_path, "depth_anything_output.jpg") # 直接指定输出文件名
376
- teed_imgs(img_path, "teed_imgs_output.jpg", [1, 7, 2])
377
- teed_imgs("depth_anything_output.jpg", "dp_teed_imgs_output.jpg", [0, 7, 2])
378
- merge_images_in_2_folder("teed_imgs_output.jpg", "dp_teed_imgs_output.jpg", "merged_imgs_output.jpg", '_depth', 1, 'multiply', [[2, 0], [2, 1]], [1, 0])
379
- process_images("merged_imgs_output.jpg")
380
 
381
- # 假设 merged_imgs_output.jpg 是处理后的输出文件
382
- processed_images.append("merged_imgs_output.jpg")
 
 
 
 
 
 
 
 
 
383
 
384
- # 将处理后的图片压缩成 ZIP
385
- zip_file_path = "processed_images.zip"
 
 
 
 
 
 
 
386
  with zipfile.ZipFile(zip_file_path, 'w') as zipf:
387
- for image in processed_images:
388
- zipf.write(image, os.path.basename(image))
 
 
 
389
 
390
- return zip_file_path, "" # 返回 ZIP 文件路径和空错误信息
391
  except Exception as e:
392
- return "", f"发生错误: {str(e)}" # 返回空和错误信息
 
393
 
394
  def launch_interface():
395
  with gr.Blocks() as demo:
 
4
  # 输出图片
5
  # 使用 depth-anything + teed 生成外轮廓
6
  # 使用 teed + 腐蚀算法 生成内边缘
7
+ import zipfile
8
+
9
  from PIL import Image
10
 
11
  import cv2
12
+ import cv2_ext
13
  import numpy as np
14
  import gradio as gr
15
  import os
 
366
  raise ValueError(f"Failed to read image: {image_file}")
367
  except Exception as e:
368
  print(f"Error processing file {image_file}: {e}")
 
369
  def process_line(input_files):
 
 
 
370
  try:
371
+ # 创建临时输出文件夹
372
+ output_folder = "temp_output"
373
+ os.makedirs(output_folder, exist_ok=True)
374
+
375
+ # 存储处理后的图片路径
376
+ processed_images = []
377
+
378
+ # 遍历所有输入文件
379
  for img_path in input_files:
380
  img_path = img_path.name # 获取文件路径
 
 
 
 
 
 
381
 
382
+ # 处理图片的文件夹
383
+ depth_folder = os.path.join(output_folder, "depth_anything")
384
+ teed_folder = os.path.join(output_folder, "teed_imgs")
385
+ dp_teed_folder = os.path.join(output_folder, "dp_teed_imgs")
386
+ merged_folder = os.path.join(output_folder, "merged_imgs")
387
+
388
+ # 创建每个处理步骤的文件夹
389
+ os.makedirs(depth_folder, exist_ok=True)
390
+ os.makedirs(teed_folder, exist_ok=True)
391
+ os.makedirs(dp_teed_folder, exist_ok=True)
392
+ os.makedirs(merged_folder, exist_ok=True)
393
 
394
+ # 调用处理函数
395
+ depth_anything(img_path, depth_folder)
396
+ teed_imgs(img_path, teed_folder, [1, 7, 2])
397
+ teed_imgs(depth_folder, dp_teed_folder, [0, 7, 2])
398
+ merge_images_in_2_folder(teed_folder, dp_teed_folder, merged_folder, '_depth', 1, 'multiply', [[2, 0], [2, 1]], [1, 0])
399
+ process_images(merged_folder)
400
+
401
+ # 创建压缩包
402
+ zip_file_path = os.path.join(output_folder, "processed_images.zip")
403
  with zipfile.ZipFile(zip_file_path, 'w') as zipf:
404
+ # 将每个步骤的文件夹添加到压缩包中
405
+ for folder in [depth_folder, teed_folder, dp_teed_folder, merged_folder]:
406
+ for root, _, files in os.walk(folder):
407
+ for file in files:
408
+ zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), output_folder))
409
 
410
+ return [zip_file_path], "" # 返回压缩包路径和空错误信息
411
  except Exception as e:
412
+ return [], f"发生错误: {str(e)}" # 返回空图片和错误信息
413
+
414
 
415
  def launch_interface():
416
  with gr.Blocks() as demo: