plutosss commited on
Commit
a2a31a0
·
verified ·
1 Parent(s): 652fc2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -363,19 +363,19 @@ 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
- def process_line(input_files, output_folder):
367
- try:
368
- # 创建输出文件夹(如果不存在)
369
- os.makedirs(output_folder, exist_ok=True)
370
 
371
- # 存储处理后的图片路径
372
- processed_images = []
373
 
374
- # 遍历所有输入文件
375
  for img_path in input_files:
376
  img_path = img_path.name # 获取文件路径
377
 
378
- # 处理图片的函数调用
379
  depth_anything(img_path, os.path.join(output_folder, "depth_anything"))
380
  teed_imgs(img_path, os.path.join(output_folder, "teed_imgs"), [1, 7, 2])
381
  teed_imgs(os.path.join(output_folder, "depth_anything"), os.path.join(output_folder, "dp_teed_imgs"), [0, 7, 2])
@@ -390,32 +390,32 @@ def process_line(input_files, output_folder):
390
  for f in os.listdir(os.path.join(output_folder, "merged_imgs"))
391
  if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
392
 
393
- return processed_images, "" # 返回处理后的图片路径和空错误信息
394
- except Exception as e:
395
- return [], f"发生错误: {str(e)}" # 返回空图片和错误信息
 
 
396
 
 
 
 
397
 
398
  def launch_interface():
399
  with gr.Blocks() as demo:
400
  # 允许用户选择多张图片
401
  input_files = gr.File(label="选择输入图片", file_count="multiple", type="filepath")
402
 
403
- # 手动输入输出文件夹路径
404
- output_folder = gr.Textbox(label="输出文件夹路径", placeholder="请输入输出文件夹路径", lines=1)
405
-
406
  submit_button = gr.Button("开始处理")
407
 
408
- # 显示处理后的图片
409
- output_gallery = gr.Gallery(label="处理后的图片", show_label=True, height='auto')
410
-
411
  # 显示错误信息
412
  error_text = gr.Textbox(label="错误信息", interactive=False, visible=False)
413
 
414
  # 点击按钮时调用 process_line 函数
415
- submit_button.click(process_line, inputs=[input_files, output_folder], outputs=[output_gallery, error_text])
416
 
417
  demo.launch(share=True)
418
 
419
-
420
  if __name__ == "__main__":
421
  launch_interface()
 
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
+ output_folder = "/app/output" # 默认输出路径
369
+ os.makedirs(output_folder, exist_ok=True)
370
 
371
+ processed_images = []
372
+ errors = []
373
 
374
+ try:
375
  for img_path in input_files:
376
  img_path = img_path.name # 获取文件路径
377
 
378
+ # 调用处理函数
379
  depth_anything(img_path, os.path.join(output_folder, "depth_anything"))
380
  teed_imgs(img_path, os.path.join(output_folder, "teed_imgs"), [1, 7, 2])
381
  teed_imgs(os.path.join(output_folder, "depth_anything"), os.path.join(output_folder, "dp_teed_imgs"), [0, 7, 2])
 
390
  for f in os.listdir(os.path.join(output_folder, "merged_imgs"))
391
  if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
392
 
393
+ # 将处理后的图片压缩成 ZIP
394
+ zip_file_path = os.path.join(output_folder, "processed_images.zip")
395
+ with zipfile.ZipFile(zip_file_path, 'w') as zipf:
396
+ for image in processed_images:
397
+ zipf.write(image, os.path.basename(image))
398
 
399
+ return zip_file_path, "" # 返回 ZIP 文件路径和空错误信息
400
+ except Exception as e:
401
+ return "", f"发生错误: {str(e)}" # 返回空和错误信息
402
 
403
  def launch_interface():
404
  with gr.Blocks() as demo:
405
  # 允许用户选择多张图片
406
  input_files = gr.File(label="选择输入图片", file_count="multiple", type="filepath")
407
 
 
 
 
408
  submit_button = gr.Button("开始处理")
409
 
410
+ # 显示处理后的文件下载链接
411
+ output_file = gr.File(label="下载处理后的文件")
 
412
  # 显示错误信息
413
  error_text = gr.Textbox(label="错误信息", interactive=False, visible=False)
414
 
415
  # 点击按钮时调用 process_line 函数
416
+ submit_button.click(process_line, inputs=[input_files], outputs=[output_file, error_text])
417
 
418
  demo.launch(share=True)
419
 
 
420
  if __name__ == "__main__":
421
  launch_interface()