shawntan123 commited on
Commit
bfce114
·
verified ·
1 Parent(s): bbaf0b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -22
app.py CHANGED
@@ -53,49 +53,68 @@ def process_upscale(input_img):
53
 
54
  # 4. 动漫化 (使用你上传的 face_paint_512_v2_0.onnx)
55
  def process_anime(input_img):
56
- if input_img is None: return None
57
 
58
- # 建议换成 shinkai_512.onnx
59
  model_path = "AnimeGANv3_Shinkai_37.onnx"
60
 
61
  try:
62
  if models["anime_session"] is None:
63
- models["anime_session"] = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
 
 
 
 
64
 
65
- # 1. 保持比例缩放,而不是硬挤压
66
- img = input_img.convert("RGB")
67
- img.thumbnail((512, 512), Image.LANCZOS)
 
 
 
 
 
68
 
69
- # 2. 补齐到 32 的倍数 (或者直接 resize 512)
70
- img = img.resize((512, 512), Image.LANCZOS)
71
 
72
- # 3. 严格执行 RGB 归一化
73
- img_np = np.array(img).astype(np.float32)
74
- # 注意:这里必须是 (x / 127.5) - 1.0
 
 
 
 
 
 
75
  img_np = (img_np / 127.5) - 1.0
76
- img_np = np.transpose(img_np, (2, 0, 1))
77
- img_np = np.expand_dims(img_np, axis=0)
78
 
79
  # 4. 推理
80
- inputs = {models["anime_session"].get_inputs()[0].name: img_np}
81
- output = models["anime_session"].run(None, inputs)[0]
82
 
83
- # 5. 后处理逻辑修复
84
- output = np.squeeze(output).transpose(1, 2, 0)
85
- # 关键:先加 1 再乘 127.5,确保回到 0-255 范围
 
 
86
  output = (output + 1.0) * 127.5
87
  output = np.clip(output, 0, 255).astype(np.uint8)
88
 
89
- return Image.fromarray(output)
 
 
90
 
91
  except Exception as e:
92
- print(f"Error: {e}")
93
- return input_img
94
 
95
  # --- Gradio 界面设计 ---
96
  with gr.Blocks(theme=gr.themes.Default()) as demo:
97
  gr.Markdown("# 🎨 全能 AI 图像处理 API 站")
98
- gr.Markdown("当前动漫化模型:`face_paint_512_v2_0.onnx` (AnimeGANv2)")
99
 
100
  with gr.Tabs():
101
  with gr.TabItem("🖼️ 抠图 (RMBG)"):
 
53
 
54
  # 4. 动漫化 (使用你上传的 face_paint_512_v2_0.onnx)
55
  def process_anime(input_img):
56
+ if input_img is None: return None
57
 
58
+ # 确保文件名匹配
59
  model_path = "AnimeGANv3_Shinkai_37.onnx"
60
 
61
  try:
62
  if models["anime_session"] is None:
63
+ # 这里的加载逻辑保持不变
64
+ models["anime_session"] = ort.InferenceSession(
65
+ model_path,
66
+ providers=['CPUExecutionProvider']
67
+ )
68
 
69
+ # 1. 获取原图尺寸
70
+ img_pil = input_img.convert("RGB")
71
+ w, h = img_pil.size
72
+
73
+ # 2. 动态计算尺寸:保持比例,同时确保是 32 的倍数
74
+ # 限制最大边长为 1024 避免 CPU 内存崩掉
75
+ max_side = 1024
76
+ scale = max_side / max(w, h) if max(w, h) > max_side else 1.0
77
 
78
+ target_w = int((w * scale) // 32) * 32
79
+ target_h = int((h * scale) // 32) * 32
80
 
81
+ # 如果缩放后尺寸太小,强制设为 32
82
+ target_w = max(32, target_w)
83
+ target_h = max(32, target_h)
84
+
85
+ # 3. 预处理
86
+ img_resized = img_pil.resize((target_w, target_h), Image.LANCZOS)
87
+ img_np = np.array(img_resized).astype(np.float32)
88
+
89
+ # V3 标准归一化
90
  img_np = (img_np / 127.5) - 1.0
91
+ img_np = np.transpose(img_np, (2, 0, 1)) # HWC -> CHW
92
+ img_np = np.expand_dims(img_np, axis=0) # CHW -> NCHW
93
 
94
  # 4. 推理
95
+ input_name = models["anime_session"].get_inputs()[0].name
96
+ output = models["anime_session"].run(None, {input_name: img_np})[0]
97
 
98
+ # 5. 后处理
99
+ output = np.squeeze(output)
100
+ output = np.transpose(output, (1, 2, 0)) # CHW -> HWC
101
+
102
+ # 恢复到 0-255 范围
103
  output = (output + 1.0) * 127.5
104
  output = np.clip(output, 0, 255).astype(np.uint8)
105
 
106
+ # 6. 关键:将结果 Resize 回用户上传的原始比例,解决“拉伸感”
107
+ result_pil = Image.fromarray(output)
108
+ return result_pil.resize((w, h), Image.LANCZOS)
109
 
110
  except Exception as e:
111
+ print(f"V3 推理出错: {e}")
112
+ return input_img # 出错则返回原图,不让 UI 裂开
113
 
114
  # --- Gradio 界面设计 ---
115
  with gr.Blocks(theme=gr.themes.Default()) as demo:
116
  gr.Markdown("# 🎨 全能 AI 图像处理 API 站")
117
+ gr.Markdown("当前动漫化模型:`AnimeGANv3_Shinkai_37.onnx` (AnimeGANv3)")
118
 
119
  with gr.Tabs():
120
  with gr.TabItem("🖼️ 抠图 (RMBG)"):