Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,130 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
"cardboard": "
|
| 8 |
-
"glass": "玻璃
|
| 9 |
-
"metal": "金屬
|
| 10 |
-
"paper": "紙類",
|
| 11 |
-
"plastic": "塑膠類",
|
| 12 |
-
"trash": "一般垃圾"
|
| 13 |
}
|
| 14 |
|
| 15 |
def predict_garbage(image):
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
class_name = result.names[class_id]
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
信心
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
-
return output_text
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
|
| 5 |
+
classifier = pipeline(
|
| 6 |
+
task="zero-shot-image-classification",
|
| 7 |
+
model="openai/clip-vit-base-patch32"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
LABELS = [
|
| 11 |
+
"cardboard waste",
|
| 12 |
+
"glass waste",
|
| 13 |
+
"metal waste",
|
| 14 |
+
"paper waste",
|
| 15 |
+
"plastic waste",
|
| 16 |
+
"general trash"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
CLASS_ZH = {
|
| 20 |
+
"cardboard waste": "紙板/紙箱類",
|
| 21 |
+
"glass waste": "玻璃類",
|
| 22 |
+
"metal waste": "金屬類",
|
| 23 |
+
"paper waste": "紙類",
|
| 24 |
+
"plastic waste": "塑膠類",
|
| 25 |
+
"general trash": "一般垃圾"
|
| 26 |
+
}
|
| 27 |
|
| 28 |
+
RECYCLE_TIPS = {
|
| 29 |
+
"cardboard waste": "建議壓扁後回收,若沾有大量油污或食物殘渣,應依當地規定處理。",
|
| 30 |
+
"glass waste": "建議清空內容物後回收,破玻璃需妥善包裝,避免割傷清潔人員。",
|
| 31 |
+
"metal waste": "建議清空內容物後回收,鋁罐、鐵罐通常可歸入金屬回收。",
|
| 32 |
+
"paper waste": "乾淨紙類可回收,若嚴重沾油、沾水或污染,可能需作一般垃圾處理。",
|
| 33 |
+
"plastic waste": "建議清空並簡單沖洗後回收,依塑膠材質與當地規則分類。",
|
| 34 |
+
"general trash": "此類較可能為一般垃圾,建議確認是否仍有可回收部分。"
|
| 35 |
}
|
| 36 |
|
| 37 |
def predict_garbage(image):
|
| 38 |
+
if image is None:
|
| 39 |
+
return None, "請先上傳一張圖片。"
|
| 40 |
+
|
| 41 |
+
results = classifier(image, candidate_labels=LABELS)
|
| 42 |
+
|
| 43 |
+
best = results[0]
|
| 44 |
+
label = best["label"]
|
| 45 |
+
score = best["score"]
|
| 46 |
|
| 47 |
+
chinese_name = CLASS_ZH.get(label, label)
|
| 48 |
+
tip = RECYCLE_TIPS.get(label, "請依照當地垃圾分類規則處理。")
|
|
|
|
| 49 |
|
| 50 |
+
top_text = ""
|
| 51 |
|
| 52 |
+
for item in results:
|
| 53 |
+
item_label = item["label"]
|
| 54 |
+
item_score = item["score"]
|
| 55 |
+
zh = CLASS_ZH.get(item_label, item_label)
|
| 56 |
+
top_text += f"{item_label}({zh}):{item_score * 100:.2f}%\n"
|
| 57 |
|
| 58 |
+
output_text = f"""預測結果:{label}
|
| 59 |
|
| 60 |
+
中文類別:{chinese_name}
|
| 61 |
|
| 62 |
+
信心分數:{score:.4f}
|
| 63 |
+
|
| 64 |
+
信心百分比:{score * 100:.2f}%
|
| 65 |
+
|
| 66 |
+
分類建議:
|
| 67 |
+
{tip}
|
| 68 |
+
|
| 69 |
+
各類別預測結果:
|
| 70 |
+
{top_text}
|
| 71 |
"""
|
| 72 |
|
| 73 |
+
return image, output_text
|
| 74 |
|
| 75 |
+
custom_css = """
|
| 76 |
+
.gradio-container {
|
| 77 |
+
max-width: 1100px !important;
|
| 78 |
+
margin: auto !important;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
#title-block {
|
| 82 |
+
text-align: center;
|
| 83 |
+
padding: 22px 12px 10px 12px;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
#title-block h1 {
|
| 87 |
+
font-size: 34px;
|
| 88 |
+
margin-bottom: 8px;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
#title-block p {
|
| 92 |
+
font-size: 16px;
|
| 93 |
+
opacity: 0.85;
|
| 94 |
+
}
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
with gr.Blocks(css=custom_css, title="AI 垃圾分類影像辨識系統") as demo:
|
| 98 |
+
gr.HTML("""
|
| 99 |
+
<div id="title-block">
|
| 100 |
+
<h1>AI 垃圾分類影像辨識系統</h1>
|
| 101 |
+
<p>上傳垃圾圖片,系統會辨識 cardboard、glass、metal、paper、plastic、trash 類別</p>
|
| 102 |
+
</div>
|
| 103 |
+
""")
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column(scale=1):
|
| 107 |
+
input_image = gr.Image(type="pil", label="上傳垃圾圖片")
|
| 108 |
+
submit_btn = gr.Button("開始辨識", variant="primary")
|
| 109 |
+
clear_btn = gr.ClearButton([input_image], value="清除圖片")
|
| 110 |
+
|
| 111 |
+
with gr.Column(scale=1):
|
| 112 |
+
output_image = gr.Image(type="pil", label="輸入圖片預覽")
|
| 113 |
+
output_text = gr.Textbox(label="模型預測結果", lines=14)
|
| 114 |
+
|
| 115 |
+
submit_btn.click(
|
| 116 |
+
fn=predict_garbage,
|
| 117 |
+
inputs=input_image,
|
| 118 |
+
outputs=[output_image, output_text]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
gr.Markdown("""
|
| 122 |
+
### 使用說明
|
| 123 |
+
|
| 124 |
+
1. 上傳一張垃圾圖片。
|
| 125 |
+
2. 點選「開始辨識」。
|
| 126 |
+
3. 系統會輸出垃圾類別、中文說明、信心分數與分類建議。
|
| 127 |
+
""")
|
| 128 |
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|