Howarddddd commited on
Commit
9bc40a3
·
verified ·
1 Parent(s): dafbf27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -28
app.py CHANGED
@@ -1,11 +1,16 @@
1
- # 下載 AnimeGANv3 宮崎駿風格 ONNX 模型
2
- #!wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=1ud-QxEtdp4bMODFUMHCZluy2Ic0Yfns1' -O AnimeGANv3_Hayao_STYLE_36.onnx
 
 
 
 
 
3
 
4
  # 啟動 ONNX 模型 Session
5
  providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if ort.get_device() == 'GPU' else ['CPUExecutionProvider']
6
  session = ort.InferenceSession('AnimeGANv3_Hayao_STYLE_36.onnx', providers=providers)
7
 
8
- # 圖片處理與轉換函式
9
  def process_image(img):
10
  h, w = img.shape[:2]
11
  def to_8s(x): return 256 if x < 256 else x - x % 8
@@ -13,14 +18,13 @@ def process_image(img):
13
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 127.5 - 1.0
14
  return img
15
 
 
16
  def cartoonize_image(input_image):
17
- # PIL 轉 numpy array
18
  img = np.array(input_image)
19
  h, w = img.shape[:2]
20
  processed = process_image(img)
21
  processed = np.expand_dims(processed, axis=0)
22
 
23
- # 模型推論
24
  input_name = session.get_inputs()[0].name
25
  output = session.run(None, {input_name: processed})[0]
26
  result = (np.squeeze(output) + 1.) / 2 * 255
@@ -28,45 +32,26 @@ def cartoonize_image(input_image):
28
  result = cv2.resize(result, (w, h))
29
  result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
30
 
31
- return Image.fromarray(result)
32
-
33
- def cartoonize_image(input_image):
34
- # PIL 轉 numpy array
35
- img = np.array(input_image)
36
- h, w = img.shape[:2]
37
- processed = process_image(img)
38
- processed = np.expand_dims(processed, axis=0)
39
-
40
- # 模型推論
41
- input_name = session.get_inputs()[0].name
42
- output = session.run(None, {input_name: processed})[0]
43
- result = (np.squeeze(output) + 1.) / 2 * 255
44
- result = np.clip(result, 0, 255).astype(np.uint8)
45
- result = cv2.resize(result, (w, h))
46
- result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
47
-
48
- # 轉換結果為 PIL
49
  result_image = Image.fromarray(result)
50
 
51
- # 保存結果至臨時檔案夾
52
  os.makedirs("outputs", exist_ok=True)
53
  filename = f"outputs/{uuid.uuid4().hex}.png"
54
  result_image.save(filename)
55
 
56
  return result_image, filename
57
 
58
- # 建立 Gradio UI
59
  interface = gr.Interface(
60
  fn=cartoonize_image,
61
  inputs=gr.Image(type="pil", label="上傳圖片"),
62
  outputs=[
63
  gr.Image(type="pil", label="宮崎駿風格轉換結果"),
64
- gr.File(label="⬇️ 點此下載圖片")
65
  ],
66
  title="🎨 AnimeGANv3 - 宮崎駿風格轉換器",
67
  description="上傳圖片,將自動轉換為宮崎駿動畫風格!",
68
  allow_flagging="never"
69
  )
70
 
71
- # 啟動 Gradio 介面
72
- interface.launch()
 
1
+ import gradio as gr
2
+ import onnxruntime as ort
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+ import os
7
+ import uuid
8
 
9
  # 啟動 ONNX 模型 Session
10
  providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if ort.get_device() == 'GPU' else ['CPUExecutionProvider']
11
  session = ort.InferenceSession('AnimeGANv3_Hayao_STYLE_36.onnx', providers=providers)
12
 
13
+ # 圖片處理
14
  def process_image(img):
15
  h, w = img.shape[:2]
16
  def to_8s(x): return 256 if x < 256 else x - x % 8
 
18
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 127.5 - 1.0
19
  return img
20
 
21
+ # 圖片轉換 + 下載功能
22
  def cartoonize_image(input_image):
 
23
  img = np.array(input_image)
24
  h, w = img.shape[:2]
25
  processed = process_image(img)
26
  processed = np.expand_dims(processed, axis=0)
27
 
 
28
  input_name = session.get_inputs()[0].name
29
  output = session.run(None, {input_name: processed})[0]
30
  result = (np.squeeze(output) + 1.) / 2 * 255
 
32
  result = cv2.resize(result, (w, h))
33
  result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  result_image = Image.fromarray(result)
36
 
37
+ # 保存下載檔案
38
  os.makedirs("outputs", exist_ok=True)
39
  filename = f"outputs/{uuid.uuid4().hex}.png"
40
  result_image.save(filename)
41
 
42
  return result_image, filename
43
 
44
+ # Gradio UI
45
  interface = gr.Interface(
46
  fn=cartoonize_image,
47
  inputs=gr.Image(type="pil", label="上傳圖片"),
48
  outputs=[
49
  gr.Image(type="pil", label="宮崎駿風格轉換結果"),
50
+ gr.File(label="⬇️ 下載圖片")
51
  ],
52
  title="🎨 AnimeGANv3 - 宮崎駿風格轉換器",
53
  description="上傳圖片,將自動轉換為宮崎駿動畫風格!",
54
  allow_flagging="never"
55
  )
56
 
57
+ interface.launch()