STLooo commited on
Commit
1a75a8f
·
verified ·
1 Parent(s): 49b4baa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -26
app.py CHANGED
@@ -2,7 +2,6 @@ import cv2
2
  import numpy as np
3
  import os
4
  import gradio as gr
5
- from tqdm import tqdm
6
  import gc
7
 
8
  # 常量配置
@@ -19,30 +18,26 @@ def overlay_image(bg, fg, x, y):
19
  bg[y:y+fg.shape[0], x:x+fg.shape[1]] = alpha * fg[:, :, :3] + (1 - alpha) * bg[y:y+fg.shape[0], x:x+fg.shape[1]]
20
  return bg
21
 
22
-
23
  def process_images(fg_files):
24
  try:
25
- progress = gr.Progress()
26
- progress(0, desc="正在准备...")
27
-
28
- """处理1~250张前景图(动态数量)"""
29
- # 检查上传数量
30
- if len(fg_files) > MAX_FILES:
31
- raise gr.Error(f"最多上传 {MAX_FILES} 张图片!")
32
 
33
- num_files = len(fg_files)
34
- if num_files > MAX_FILES:
35
- raise gr.Error(f"最多支持 {MAX_FILES} 张前景图!")
36
- if num_files == 0:
37
- return []
38
-
39
- # 读取背景图(仅一次)
40
- bg = cv2.imread(BG_PATH)
41
- if bg is None:
42
- raise gr.Error("请先上传背景图!")
43
-
44
- results = []
45
- for i, fg_file in enumerate(fg_files, 1):
 
 
46
  progress(i/len(fg_files), f"合成中 ({i}/{len(fg_files)})")
47
  # 读取前景图(自动跳过损坏文件)
48
  fg = cv2.imread(fg_file.name, cv2.IMREAD_UNCHANGED)
@@ -65,13 +60,12 @@ def process_images(fg_files):
65
  if i % 10 == 0:
66
  gc.collect()
67
 
68
- progress(1, "处理完成!")
69
- return results
70
 
71
  except Exception as e:
72
  raise gr.Error(f"出错啦: {str(e)}")
73
 
74
-
75
  # Gradio界面
76
  with gr.Blocks() as demo:
77
  gr.Markdown("## 🖼️ 灵活图片合成工具 (1~250张)")
@@ -90,7 +84,7 @@ with gr.Blocks() as demo:
90
 
91
  # 事件绑定
92
  bg_upload.upload(
93
- lambda f: cv2.imwrite(BG_PATH, cv2.imdecode(np.frombuffer(f.read(), np.uint8)) or "背景图已更新!"),
94
  inputs=bg_upload,
95
  outputs=log_output
96
  )
 
2
  import numpy as np
3
  import os
4
  import gradio as gr
 
5
  import gc
6
 
7
  # 常量配置
 
18
  bg[y:y+fg.shape[0], x:x+fg.shape[1]] = alpha * fg[:, :, :3] + (1 - alpha) * bg[y:y+fg.shape[0], x:x+fg.shape[1]]
19
  return bg
20
 
 
21
  def process_images(fg_files):
22
  try:
23
+ progress = gr.Progress()
24
+ progress(0, desc="正在准备...")
 
 
 
 
 
25
 
26
+ """处理1~250张前景图(动态数量)"""
27
+ # 检查上传数量
28
+ num_files = len(fg_files)
29
+ if num_files > MAX_FILES:
30
+ raise gr.Error(f"最多支持 {MAX_FILES} 张前景图!")
31
+ if num_files == 0:
32
+ return []
33
+
34
+ # 读取背景图(仅一次)
35
+ bg = cv2.imread(BG_PATH)
36
+ if bg is None:
37
+ raise gr.Error("请先上传背景图!")
38
+
39
+ results = []
40
+ for i, fg_file in enumerate(fg_files, 1):
41
  progress(i/len(fg_files), f"合成中 ({i}/{len(fg_files)})")
42
  # 读取前景图(自动跳过损坏文件)
43
  fg = cv2.imread(fg_file.name, cv2.IMREAD_UNCHANGED)
 
60
  if i % 10 == 0:
61
  gc.collect()
62
 
63
+ progress(1, "处理完成!")
64
+ return results
65
 
66
  except Exception as e:
67
  raise gr.Error(f"出错啦: {str(e)}")
68
 
 
69
  # Gradio界面
70
  with gr.Blocks() as demo:
71
  gr.Markdown("## 🖼️ 灵活图片合成工具 (1~250张)")
 
84
 
85
  # 事件绑定
86
  bg_upload.upload(
87
+ lambda f: cv2.imwrite(BG_PATH, cv2.imdecode(np.frombuffer(f.read(), np.uint8)) or "背景图已更新!",
88
  inputs=bg_upload,
89
  outputs=log_output
90
  )