Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,17 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
-
"""app
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colab.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1KSLxI81slcuso6QwCO1eZ0yJd0iszw5G
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
from pathlib import Path
|
|
|
|
|
|
|
| 11 |
import numpy as np
|
| 12 |
from PIL import Image
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
# ===== 你要蒐集的類別 =====
|
| 16 |
-
|
| 17 |
-
|
| 18 |
"sailboat", "bicycle", "cookie", "banana",
|
| 19 |
-
"ice_cream", "frying_pan", "sandwich", "hamburger"
|
|
|
|
| 20 |
|
| 21 |
# ===== 資料儲存位置(存在 Space 的檔案系統)=====
|
| 22 |
BASE_DIR = Path("my_drawings")
|
|
@@ -52,10 +47,9 @@ def preprocess_to_28x28_gray(im):
|
|
| 52 |
|
| 53 |
# 先放大,避免線條太細
|
| 54 |
big = im.resize((112, 112), Image.Resampling.BILINEAR)
|
| 55 |
-
|
| 56 |
arr = np.array(big).astype("float32") / 255.0
|
| 57 |
|
| 58 |
-
# 反相:
|
| 59 |
arr = 1.0 - arr
|
| 60 |
|
| 61 |
# 找有畫到的範圍(> threshold 視為有筆畫)
|
|
@@ -68,7 +62,7 @@ def preprocess_to_28x28_gray(im):
|
|
| 68 |
y_min = max(y_min - pad, 0)
|
| 69 |
x_max = min(x_max + pad, arr.shape[1] - 1)
|
| 70 |
y_max = min(y_max + pad, arr.shape[0] - 1)
|
| 71 |
-
arr = arr[y_min
|
| 72 |
|
| 73 |
# 縮成 28x28
|
| 74 |
small = Image.fromarray((arr * 255).astype("uint8"))
|
|
@@ -80,7 +74,7 @@ def preprocess_to_28x28_gray(im):
|
|
| 80 |
dilated = np.zeros_like(arr28)
|
| 81 |
for i in range(28):
|
| 82 |
for j in range(28):
|
| 83 |
-
patch = padded[i
|
| 84 |
dilated[i, j] = patch.max()
|
| 85 |
|
| 86 |
return dilated # shape (28, 28), 值在 0~1
|
|
@@ -92,7 +86,12 @@ def save_drawing(label, im):
|
|
| 92 |
im: Sketchpad 畫布內容
|
| 93 |
"""
|
| 94 |
if label not in CLASS_NAMES:
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
arr28 = preprocess_to_28x28_gray(im)
|
| 98 |
|
|
@@ -105,6 +104,7 @@ def save_drawing(label, im):
|
|
| 105 |
idx = len(existing)
|
| 106 |
fname = class_dir / f"{label}_{idx:04d}.png"
|
| 107 |
|
|
|
|
| 108 |
img = Image.fromarray((arr28 * 255).astype("uint8"))
|
| 109 |
img.save(fname)
|
| 110 |
|
|
@@ -115,40 +115,42 @@ def save_drawing(label, im):
|
|
| 115 |
return (
|
| 116 |
f"已儲存:{fname.name}({label} 類別目前共 {idx + 1} 張)",
|
| 117 |
preview,
|
| 118 |
-
str(fname)
|
| 119 |
)
|
| 120 |
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
)
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
label_input = gr.Dropdown(
|
| 144 |
choices=CLASS_NAMES,
|
| 145 |
value="key",
|
| 146 |
-
label="
|
| 147 |
)
|
| 148 |
|
| 149 |
sketch_input = gr.Sketchpad(
|
| 150 |
-
label="
|
| 151 |
-
#
|
| 152 |
# canvas_size=(512, 512),
|
| 153 |
)
|
| 154 |
|
|
@@ -166,7 +168,7 @@ file_output = gr.File(
|
|
| 166 |
label="下載這次儲存的影像檔"
|
| 167 |
)
|
| 168 |
|
| 169 |
-
|
| 170 |
fn=save_drawing,
|
| 171 |
inputs=[label_input, sketch_input],
|
| 172 |
outputs=[status_output, preview_output, file_output],
|
|
@@ -178,6 +180,30 @@ demo = gr.Interface(
|
|
| 178 |
),
|
| 179 |
)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
# 在 Hugging Face Spaces 上不需要 share=True,平台會自動提供網址
|
| 182 |
if __name__ == "__main__":
|
| 183 |
demo.launch()
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
+
import shutil
|
| 4 |
+
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
# ===== 你要蒐集的類別 =====
|
| 10 |
+
CLASS_NAMES = [
|
| 11 |
+
"key", "bread", "carrot", "onion", "steak",
|
| 12 |
"sailboat", "bicycle", "cookie", "banana",
|
| 13 |
+
"ice_cream", "frying_pan", "sandwich", "hamburger"
|
| 14 |
+
]
|
| 15 |
|
| 16 |
# ===== 資料儲存位置(存在 Space 的檔案系統)=====
|
| 17 |
BASE_DIR = Path("my_drawings")
|
|
|
|
| 47 |
|
| 48 |
# 先放大,避免線條太細
|
| 49 |
big = im.resize((112, 112), Image.Resampling.BILINEAR)
|
|
|
|
| 50 |
arr = np.array(big).astype("float32") / 255.0
|
| 51 |
|
| 52 |
+
# 反相:黑線白底 -> 黑底白線
|
| 53 |
arr = 1.0 - arr
|
| 54 |
|
| 55 |
# 找有畫到的範圍(> threshold 視為有筆畫)
|
|
|
|
| 62 |
y_min = max(y_min - pad, 0)
|
| 63 |
x_max = min(x_max + pad, arr.shape[1] - 1)
|
| 64 |
y_max = min(y_max + pad, arr.shape[0] - 1)
|
| 65 |
+
arr = arr[y_min: y_max + 1, x_min: x_max + 1]
|
| 66 |
|
| 67 |
# 縮成 28x28
|
| 68 |
small = Image.fromarray((arr * 255).astype("uint8"))
|
|
|
|
| 74 |
dilated = np.zeros_like(arr28)
|
| 75 |
for i in range(28):
|
| 76 |
for j in range(28):
|
| 77 |
+
patch = padded[i: i + 3, j: j + 3]
|
| 78 |
dilated[i, j] = patch.max()
|
| 79 |
|
| 80 |
return dilated # shape (28, 28), 值在 0~1
|
|
|
|
| 86 |
im: Sketchpad 畫布內容
|
| 87 |
"""
|
| 88 |
if label not in CLASS_NAMES:
|
| 89 |
+
# 雖然理論上不會發生(Dropdown 已限制),但還是回傳三個輸出
|
| 90 |
+
return (
|
| 91 |
+
"⚠️ 請先從上方選擇一個類別(例如 key)",
|
| 92 |
+
None,
|
| 93 |
+
None
|
| 94 |
+
)
|
| 95 |
|
| 96 |
arr28 = preprocess_to_28x28_gray(im)
|
| 97 |
|
|
|
|
| 104 |
idx = len(existing)
|
| 105 |
fname = class_dir / f"{label}_{idx:04d}.png"
|
| 106 |
|
| 107 |
+
# 儲存成 28x28 PNG
|
| 108 |
img = Image.fromarray((arr28 * 255).astype("uint8"))
|
| 109 |
img.save(fname)
|
| 110 |
|
|
|
|
| 115 |
return (
|
| 116 |
f"已儲存:{fname.name}({label} 類別目前共 {idx + 1} 張)",
|
| 117 |
preview,
|
| 118 |
+
str(fname)
|
| 119 |
)
|
| 120 |
|
| 121 |
|
| 122 |
+
def make_zip():
|
| 123 |
+
"""
|
| 124 |
+
打包 my_drawings 資料夾為 zip,讓使用者下載。
|
| 125 |
+
"""
|
| 126 |
+
if not BASE_DIR.exists():
|
| 127 |
+
return "目前還沒有任何資料可以打包", None
|
| 128 |
|
| 129 |
+
# 檢查底下有沒有任何 png 檔
|
| 130 |
+
has_png = any(BASE_DIR.rglob("*.png"))
|
| 131 |
+
if not has_png:
|
| 132 |
+
return "目前還沒有任何圖像可以打包", None
|
| 133 |
|
| 134 |
+
zip_path = Path("my_drawings_export.zip")
|
| 135 |
+
if zip_path.exists():
|
| 136 |
+
zip_path.unlink()
|
|
|
|
| 137 |
|
| 138 |
+
# 建立新的 zip 檔(my_drawings_export.zip)
|
| 139 |
+
shutil.make_archive("my_drawings_export", "zip", BASE_DIR)
|
|
|
|
| 140 |
|
| 141 |
+
return "已建立 ZIP 檔,可以下載囉", str(zip_path)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
# ===== Gradio 介面設定:畫圖蒐集 =====
|
| 145 |
label_input = gr.Dropdown(
|
| 146 |
choices=CLASS_NAMES,
|
| 147 |
value="key",
|
| 148 |
+
label="請選擇要畫的類別",
|
| 149 |
)
|
| 150 |
|
| 151 |
sketch_input = gr.Sketchpad(
|
| 152 |
+
label="請在這裡畫出該物品",
|
| 153 |
+
# 若覺得畫布太小可以改大一點:
|
| 154 |
# canvas_size=(512, 512),
|
| 155 |
)
|
| 156 |
|
|
|
|
| 168 |
label="下載這次儲存的影像檔"
|
| 169 |
)
|
| 170 |
|
| 171 |
+
draw_demo = gr.Interface(
|
| 172 |
fn=save_drawing,
|
| 173 |
inputs=[label_input, sketch_input],
|
| 174 |
outputs=[status_output, preview_output, file_output],
|
|
|
|
| 180 |
),
|
| 181 |
)
|
| 182 |
|
| 183 |
+
# ===== Gradio 介面設定:資料打包下載 =====
|
| 184 |
+
zip_status = gr.Textbox(
|
| 185 |
+
label="打包狀態",
|
| 186 |
+
interactive=False
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
zip_file = gr.File(
|
| 190 |
+
label="下載打包好的 ZIP(my_drawings_export.zip)"
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
zip_demo = gr.Interface(
|
| 194 |
+
fn=make_zip,
|
| 195 |
+
inputs=[],
|
| 196 |
+
outputs=[zip_status, zip_file],
|
| 197 |
+
title="資料集下載",
|
| 198 |
+
description="按下按鈕(Run),將目前 my_drawings 資料夾打包成 ZIP,下載到本機。"
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
# 兩個頁籤:一個畫圖、一個下載
|
| 202 |
+
demo = gr.TabbedInterface(
|
| 203 |
+
[draw_demo, zip_demo],
|
| 204 |
+
["畫圖蒐集", "下載資料"]
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
# 在 Hugging Face Spaces 上不需要 share=True,平台會自動提供網址
|
| 208 |
if __name__ == "__main__":
|
| 209 |
demo.launch()
|