Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
|
|
|
|
|
|
|
| 4 |
def predict(img):
|
| 5 |
path = img.split("\\")[-1].split(".")[0]
|
| 6 |
print("path", path)
|
| 7 |
|
| 8 |
-
model = YOLO("model.pt")
|
| 9 |
-
|
| 10 |
results = model.predict(source=img, save=False, show_labels=False, show_conf=False)
|
| 11 |
|
| 12 |
# Handle both single result and list of results
|
|
@@ -24,15 +24,175 @@ def predict(img):
|
|
| 24 |
|
| 25 |
return str(count), annot_img
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
with gr.Row():
|
| 29 |
-
with gr.Column():
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
|
| 4 |
+
model = YOLO("model.pt")
|
| 5 |
+
|
| 6 |
def predict(img):
|
| 7 |
path = img.split("\\")[-1].split(".")[0]
|
| 8 |
print("path", path)
|
| 9 |
|
|
|
|
|
|
|
| 10 |
results = model.predict(source=img, save=False, show_labels=False, show_conf=False)
|
| 11 |
|
| 12 |
# Handle both single result and list of results
|
|
|
|
| 24 |
|
| 25 |
return str(count), annot_img
|
| 26 |
|
| 27 |
+
# 自定義 CSS 樣式(背景改為白色、簡潔風格)
|
| 28 |
+
custom_css = """
|
| 29 |
+
.gradio-container {
|
| 30 |
+
background: #ffffff;
|
| 31 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/* 主標題 */
|
| 35 |
+
.main-header {
|
| 36 |
+
text-align: center;
|
| 37 |
+
color: #333333;
|
| 38 |
+
font-size: 2.2em;
|
| 39 |
+
font-weight: bold;
|
| 40 |
+
margin-bottom: 5px;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/* 副標題 */
|
| 44 |
+
.sub-header {
|
| 45 |
+
text-align: center;
|
| 46 |
+
color: #555555;
|
| 47 |
+
font-size: 1.1em;
|
| 48 |
+
margin-bottom: 20px;
|
| 49 |
+
font-weight: 300;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
/* 卡片容器 */
|
| 53 |
+
.card-container {
|
| 54 |
+
background: #f9f9f9;
|
| 55 |
+
border-radius: 12px;
|
| 56 |
+
padding: 20px;
|
| 57 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.05);
|
| 58 |
+
margin: 15px;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
/* 輸入區 */
|
| 62 |
+
.input-section {
|
| 63 |
+
background: #ffffff;
|
| 64 |
+
border-radius: 10px;
|
| 65 |
+
padding: 20px;
|
| 66 |
+
margin-bottom: 15px;
|
| 67 |
+
border: 1px solid #e0e0e0;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/* 輸出區 */
|
| 71 |
+
.output-section {
|
| 72 |
+
background: #ffffff;
|
| 73 |
+
border-radius: 10px;
|
| 74 |
+
padding: 20px;
|
| 75 |
+
border: 1px solid #e0e0e0;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/* 按鈕 */
|
| 79 |
+
.predict-btn {
|
| 80 |
+
background: #4285f4 !important;
|
| 81 |
+
border: none !important;
|
| 82 |
+
border-radius: 20px !important;
|
| 83 |
+
padding: 12px 32px !important;
|
| 84 |
+
color: white !important;
|
| 85 |
+
font-size: 16px !important;
|
| 86 |
+
font-weight: 600 !important;
|
| 87 |
+
transition: background 0.2s ease !important;
|
| 88 |
+
cursor: pointer !important;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
.predict-btn:hover {
|
| 92 |
+
background: #3367d6 !important;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
/* 圖片上傳 */
|
| 96 |
+
.image-upload {
|
| 97 |
+
border: 2px dashed #cccccc !important;
|
| 98 |
+
border-radius: 10px !important;
|
| 99 |
+
background: #fcfcfc !important;
|
| 100 |
+
padding: 15px !important;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
.image-upload:hover {
|
| 104 |
+
border-color: #bbbbbb !important;
|
| 105 |
+
background: #f5f5f5 !important;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
/* 數量文字 */
|
| 109 |
+
.count-output {
|
| 110 |
+
font-size: 22px !important;
|
| 111 |
+
font-weight: 600 !important;
|
| 112 |
+
color: #222222 !important;
|
| 113 |
+
text-align: center !important;
|
| 114 |
+
background: #ffffff !important;
|
| 115 |
+
border: 1px solid #e0e0e0 !important;
|
| 116 |
+
border-radius: 8px !important;
|
| 117 |
+
padding: 12px !important;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
/* 結果圖 */
|
| 121 |
+
.result-image {
|
| 122 |
+
border-radius: 8px !important;
|
| 123 |
+
box-shadow: 0 6px 18px rgba(0,0,0,0.04) !important;
|
| 124 |
+
border: 1px solid #e0e0e0 !important;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
/* 響應式 */
|
| 128 |
+
@media (max-width: 768px) {
|
| 129 |
+
.main-header { font-size: 1.8em; }
|
| 130 |
+
.predict-btn { width: 100% !important; margin-top: 15px !important; }
|
| 131 |
+
}
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
with gr.Blocks(
|
| 135 |
+
title="Pill Counter",
|
| 136 |
+
theme=gr.themes.Soft(
|
| 137 |
+
primary_hue="blue",
|
| 138 |
+
secondary_hue="green",
|
| 139 |
+
neutral_hue="gray",
|
| 140 |
+
font=gr.themes.GoogleFont("Noto Sans TC")
|
| 141 |
+
),
|
| 142 |
+
css=custom_css
|
| 143 |
+
) as demo:
|
| 144 |
+
|
| 145 |
+
gr.HTML("""
|
| 146 |
+
<div class="main-header">Pill Counter</div>
|
| 147 |
+
""")
|
| 148 |
+
|
| 149 |
with gr.Row():
|
| 150 |
+
with gr.Column(scale=1):
|
| 151 |
+
|
| 152 |
+
gr.HTML('<h3 style="color: #4285f4; text-align: center; margin-bottom: 15px;">📤 上傳藥物圖片</h3>')
|
| 153 |
+
img = gr.Image(
|
| 154 |
+
type="filepath",
|
| 155 |
+
format=["jpg", "png"],
|
| 156 |
+
height=450,
|
| 157 |
+
width=800,
|
| 158 |
+
label="請選擇或拖放圖片",
|
| 159 |
+
elem_classes=["image-upload"]
|
| 160 |
+
)
|
| 161 |
+
gr.HTML('<div style="text-align: center; margin-top: 15px;">')
|
| 162 |
+
button = gr.Button(
|
| 163 |
+
"開始計算",
|
| 164 |
+
variant="primary",
|
| 165 |
+
elem_classes=["predict-btn"]
|
| 166 |
+
)
|
| 167 |
+
gr.HTML('</div><div style="text-align:center; margin-top:8px; color:#888;">支援 JPG/PNG,建議 640×640 以上</div>')
|
| 168 |
+
gr.HTML('</div>')
|
| 169 |
+
|
| 170 |
+
with gr.Column(scale=1):
|
| 171 |
+
|
| 172 |
+
gr.HTML('<h3 style="color: #34a853; text-align: center; margin-bottom: 15px;">📊 檢測結果</h3>')
|
| 173 |
+
data_output = gr.Textbox(
|
| 174 |
+
interactive=False,
|
| 175 |
+
elem_classes=["count-output"]
|
| 176 |
+
)
|
| 177 |
+
gr.HTML('<div style="text-align:center; margin:15px 0;"><label style="font-size:14px;color:#555;">標註結果</label></div>')
|
| 178 |
+
img_output = gr.Image(
|
| 179 |
+
type="numpy",
|
| 180 |
+
elem_classes=["result-image"]
|
| 181 |
+
)
|
| 182 |
+
gr.HTML('</div>')
|
| 183 |
+
|
| 184 |
+
gr.HTML("""
|
| 185 |
+
<div style="background:#ffffff; border-left:4px solid #4285f4; padding:15px; margin:15px; color:#666;">
|
| 186 |
+
<strong>使用步驟:</strong>
|
| 187 |
+
<ol style="margin-top:8px;">
|
| 188 |
+
<li>上傳清晰的圖片,保持純色背景(白色或黑色),勿有反光</li>
|
| 189 |
+
<li>點擊「開始計算」按鈕</li>
|
| 190 |
+
<li>查看標註後圖片及藥錠總數</li>
|
| 191 |
+
</ol>
|
| 192 |
+
</div>
|
| 193 |
+
""")
|
| 194 |
+
|
| 195 |
+
button.click(fn=predict, inputs=img, outputs=[data_output, img_output])
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
+
demo.launch()
|