Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,7 +102,7 @@ def save_drawing(label, im):
|
|
| 102 |
img = Image.fromarray((arr28 * 255).astype("uint8"))
|
| 103 |
img.save(fname)
|
| 104 |
|
| 105 |
-
#
|
| 106 |
preview = img.resize((224, 224), Image.Resampling.NEAREST)
|
| 107 |
|
| 108 |
return f"已儲存:{fname.name}({label} 類別目前共 {idx + 1} 張)", preview
|
|
@@ -111,7 +111,7 @@ def save_drawing(label, im):
|
|
| 111 |
def list_images_for_class(label):
|
| 112 |
"""
|
| 113 |
回傳某個類別底下所有 PNG 的路徑 list,
|
| 114 |
-
給 gr.Gallery 顯示。
|
| 115 |
"""
|
| 116 |
class_dir = BASE_DIR / label
|
| 117 |
if not class_dir.exists():
|
|
@@ -126,29 +126,34 @@ with gr.Blocks(title="QuickDraw 風格資料蒐集 & 瀏覽") as demo:
|
|
| 126 |
with gr.Tab("畫圖蒐集"):
|
| 127 |
gr.Markdown("### 選一個類別,畫圖並儲存成 28×28 資料")
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
submit_btn.click(
|
| 154 |
fn=save_drawing,
|
|
@@ -160,7 +165,7 @@ with gr.Blocks(title="QuickDraw 風格資料蒐集 & 瀏覽") as demo:
|
|
| 160 |
with gr.Tab("瀏覽資料庫(Gallery)"):
|
| 161 |
gr.Markdown(
|
| 162 |
"### 選一個類別,載入目前所有已蒐集的圖像\n"
|
| 163 |
-
"
|
| 164 |
)
|
| 165 |
|
| 166 |
gallery_label = gr.Dropdown(
|
|
@@ -174,7 +179,7 @@ with gr.Blocks(title="QuickDraw 風格資料蒐集 & 瀏覽") as demo:
|
|
| 174 |
gallery = gr.Gallery(
|
| 175 |
label="目前已蒐集的圖像(縮圖)",
|
| 176 |
show_label=True,
|
| 177 |
-
columns=
|
| 178 |
height=400,
|
| 179 |
)
|
| 180 |
|
|
|
|
| 102 |
img = Image.fromarray((arr28 * 255).astype("uint8"))
|
| 103 |
img.save(fname)
|
| 104 |
|
| 105 |
+
# 右側只做預覽:把 28x28 放大成 224x224 看得比較清楚
|
| 106 |
preview = img.resize((224, 224), Image.Resampling.NEAREST)
|
| 107 |
|
| 108 |
return f"已儲存:{fname.name}({label} 類別目前共 {idx + 1} 張)", preview
|
|
|
|
| 111 |
def list_images_for_class(label):
|
| 112 |
"""
|
| 113 |
回傳某個類別底下所有 PNG 的路徑 list,
|
| 114 |
+
給 gr.Gallery 顯示(檔案本身是 28×28,不再特別放大)。
|
| 115 |
"""
|
| 116 |
class_dir = BASE_DIR / label
|
| 117 |
if not class_dir.exists():
|
|
|
|
| 126 |
with gr.Tab("畫圖蒐集"):
|
| 127 |
gr.Markdown("### 選一個類別,畫圖並儲存成 28×28 資料")
|
| 128 |
|
| 129 |
+
with gr.Row():
|
| 130 |
+
# 左邊:類別下拉 + Sketchpad + 按鈕
|
| 131 |
+
with gr.Column(scale=2):
|
| 132 |
+
label_input = gr.Dropdown(
|
| 133 |
+
choices=CLASS_NAMES,
|
| 134 |
+
value="key",
|
| 135 |
+
label="請選擇要畫的類別(class)",
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
sketch_input = gr.Sketchpad(
|
| 139 |
+
label="請在這裡畫出該物品(建議筆刷細一點)",
|
| 140 |
+
# 覺得太小可以改大:
|
| 141 |
+
# canvas_size=(512, 512),
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
submit_btn = gr.Button("Submit / 儲存這張圖")
|
| 145 |
+
|
| 146 |
+
# 右邊:狀態 + 預覽
|
| 147 |
+
with gr.Column(scale=1):
|
| 148 |
+
status_output = gr.Textbox(
|
| 149 |
+
label="狀態訊息 / Status",
|
| 150 |
+
interactive=False,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
preview_output = gr.Image(
|
| 154 |
+
label="預覽:前處理後的 28×28(放大顯示)",
|
| 155 |
+
image_mode="L",
|
| 156 |
+
)
|
| 157 |
|
| 158 |
submit_btn.click(
|
| 159 |
fn=save_drawing,
|
|
|
|
| 165 |
with gr.Tab("瀏覽資料庫(Gallery)"):
|
| 166 |
gr.Markdown(
|
| 167 |
"### 選一個類別,載入目前所有已蒐集的圖像\n"
|
| 168 |
+
"下方 Gallery 顯示的是已儲存的 28×28 圖像(原始尺寸)。"
|
| 169 |
)
|
| 170 |
|
| 171 |
gallery_label = gr.Dropdown(
|
|
|
|
| 179 |
gallery = gr.Gallery(
|
| 180 |
label="目前已蒐集的圖像(縮圖)",
|
| 181 |
show_label=True,
|
| 182 |
+
columns=10, # 一排多一點,方便你掃過去看
|
| 183 |
height=400,
|
| 184 |
)
|
| 185 |
|