Update app.py
Browse files
app.py
CHANGED
|
@@ -16,42 +16,52 @@ def colorize_ddcolor(input_pil):
|
|
| 16 |
return "錯誤:找不到 colorizer.onnx 檔案,請確認已上傳。"
|
| 17 |
|
| 18 |
try:
|
|
|
|
| 19 |
session = ort.InferenceSession(MODEL_PATH, providers=['CPUExecutionProvider'])
|
| 20 |
|
| 21 |
-
#
|
| 22 |
img_np = np.array(input_pil.convert('RGB'))
|
| 23 |
orig_h, orig_w = img_np.shape[:2]
|
| 24 |
|
| 25 |
-
#
|
|
|
|
| 26 |
input_256 = cv2.resize(img_np, (256, 256))
|
|
|
|
|
|
|
|
|
|
| 27 |
input_256 = input_256.astype(np.float32) / 255.0
|
| 28 |
|
| 29 |
-
#
|
| 30 |
input_tensor = np.transpose(input_256, (2, 0, 1)) # HWC -> CHW
|
| 31 |
-
input_tensor = np.expand_dims(input_tensor, axis=0) # 增加 Batch
|
| 32 |
|
| 33 |
-
# 運行 AI
|
| 34 |
input_name = session.get_inputs()[0].name
|
| 35 |
output = session.run(None, {input_name: input_tensor})
|
| 36 |
|
| 37 |
-
#
|
| 38 |
output_np = np.squeeze(output)
|
| 39 |
output_np = np.transpose(output_np, (1, 2, 0)) # CHW -> HWC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
output_np = np.clip(output_np, 0, 1)
|
| 41 |
output_np = (output_np * 255).astype(np.uint8)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
result_img = cv2.resize(output_np, (orig_w, orig_h))
|
| 45 |
return Image.fromarray(result_img)
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
# 如果依然報錯,將錯誤打印到日誌中
|
| 49 |
print(f"運行錯誤: {str(e)}")
|
| 50 |
-
return "
|
| 51 |
|
| 52 |
-
#
|
| 53 |
with gr.Blocks(title="AI DDColor 上色工具") as demo:
|
| 54 |
-
gr.Markdown("# 🎨 DDColor 自動上色工具 (
|
|
|
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column():
|
|
@@ -63,4 +73,5 @@ with gr.Blocks(title="AI DDColor 上色工具") as demo:
|
|
| 63 |
btn.click(colorize_ddcolor, inputs=input_i, outputs=output_i)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
|
|
|
| 66 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 16 |
return "錯誤:找不到 colorizer.onnx 檔案,請確認已上傳。"
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
# 啟動 AI 引擎
|
| 20 |
session = ort.InferenceSession(MODEL_PATH, providers=['CPUExecutionProvider'])
|
| 21 |
|
| 22 |
+
# 1. 預處理:將圖片轉為 RGB 並記錄原始大小
|
| 23 |
img_np = np.array(input_pil.convert('RGB'))
|
| 24 |
orig_h, orig_w = img_np.shape[:2]
|
| 25 |
|
| 26 |
+
# 2. 縮放與格式轉換
|
| 27 |
+
# 修正:很多 ONNX 模型內部其實是吃 BGR 順序,我們這裡做一次轉換
|
| 28 |
input_256 = cv2.resize(img_np, (256, 256))
|
| 29 |
+
input_256 = cv2.cvtColor(input_256, cv2.COLOR_RGB2BGR)
|
| 30 |
+
|
| 31 |
+
# 3. 歸一化:使用標準 AI 數值 (讓顏色填得進去的關鍵)
|
| 32 |
input_256 = input_256.astype(np.float32) / 255.0
|
| 33 |
|
| 34 |
+
# 4. 調整維度符合 AI 要求 (Batch, Channel, Height, Width)
|
| 35 |
input_tensor = np.transpose(input_256, (2, 0, 1)) # HWC -> CHW
|
| 36 |
+
input_tensor = np.expand_dims(input_tensor, axis=0) # 增加 Batch
|
| 37 |
|
| 38 |
+
# 5. 運行 AI 上色
|
| 39 |
input_name = session.get_inputs()[0].name
|
| 40 |
output = session.run(None, {input_name: input_tensor})
|
| 41 |
|
| 42 |
+
# 6. 後處理:將輸出的數據轉回圖片
|
| 43 |
output_np = np.squeeze(output)
|
| 44 |
output_np = np.transpose(output_np, (1, 2, 0)) # CHW -> HWC
|
| 45 |
+
|
| 46 |
+
# 修正顏色順序:將 BGR 轉回 RGB 供螢幕顯示
|
| 47 |
+
output_np = cv2.cvtColor(output_np, cv2.COLOR_BGR2RGB)
|
| 48 |
+
|
| 49 |
+
# 確保數值在 0-255 之間
|
| 50 |
output_np = np.clip(output_np, 0, 1)
|
| 51 |
output_np = (output_np * 255).astype(np.uint8)
|
| 52 |
|
| 53 |
+
# 7. 縮放回原始大小並輸出
|
| 54 |
result_img = cv2.resize(output_np, (orig_w, orig_h))
|
| 55 |
return Image.fromarray(result_img)
|
| 56 |
|
| 57 |
except Exception as e:
|
|
|
|
| 58 |
print(f"運行錯誤: {str(e)}")
|
| 59 |
+
return f"發生錯誤: {str(e)}"
|
| 60 |
|
| 61 |
+
# --- Gradio 介面部分 (這部分保持不變,確保你能正常操作) ---
|
| 62 |
with gr.Blocks(title="AI DDColor 上色工具") as demo:
|
| 63 |
+
gr.Markdown("# 🎨 DDColor 自動上色工具 (修正顏色版)")
|
| 64 |
+
gr.Markdown("這是一個專門修復『變藍色』與『填不到色』問題的版本。")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column():
|
|
|
|
| 73 |
btn.click(colorize_ddcolor, inputs=input_i, outputs=output_i)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
# 使用 7860 端口運行
|
| 77 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|