Spaces:
Running
on
Zero
Running
on
Zero
Update breed_detection.py
Browse files- breed_detection.py +143 -48
breed_detection.py
CHANGED
@@ -2,56 +2,151 @@ import re
|
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def create_detection_tab(predict_fn, example_images):
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
</p>
|
27 |
-
<p style='
|
28 |
-
font-size: 0.9em;
|
29 |
-
color: #666;
|
30 |
-
margin-top: 8px;
|
31 |
-
padding: 0 20px;
|
32 |
-
'>
|
33 |
-
Note: The model's predictions may not always be 100% accurate, and it is recommended to use the results as a reference.
|
34 |
-
</p>
|
35 |
-
</div>
|
36 |
-
""")
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
return {
|
57 |
'input_image': input_image,
|
|
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
|
5 |
+
# def create_detection_tab(predict_fn, example_images):
|
6 |
+
# with gr.TabItem("Breed Detection"):
|
7 |
+
# gr.HTML("""
|
8 |
+
# <div style='
|
9 |
+
# text-align: center;
|
10 |
+
# padding: 20px 0;
|
11 |
+
# margin: 15px 0;
|
12 |
+
# background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
13 |
+
# border-radius: 10px;
|
14 |
+
# '>
|
15 |
+
# <p style='
|
16 |
+
# font-size: 1.2em;
|
17 |
+
# margin: 0;
|
18 |
+
# padding: 0 20px;
|
19 |
+
# line-height: 1.5;
|
20 |
+
# background: linear-gradient(90deg, #4299e1, #48bb78);
|
21 |
+
# -webkit-background-clip: text;
|
22 |
+
# -webkit-text-fill-color: transparent;
|
23 |
+
# font-weight: 600;
|
24 |
+
# '>
|
25 |
+
# Upload a picture of a dog, and the model will predict its breed and provide detailed information!
|
26 |
+
# </p>
|
27 |
+
# <p style='
|
28 |
+
# font-size: 0.9em;
|
29 |
+
# color: #666;
|
30 |
+
# margin-top: 8px;
|
31 |
+
# padding: 0 20px;
|
32 |
+
# '>
|
33 |
+
# Note: The model's predictions may not always be 100% accurate, and it is recommended to use the results as a reference.
|
34 |
+
# </p>
|
35 |
+
# </div>
|
36 |
+
# """)
|
37 |
+
|
38 |
+
# with gr.Row():
|
39 |
+
# input_image = gr.Image(label="Upload a dog image", type="pil")
|
40 |
+
# output_image = gr.Image(label="Annotated Image")
|
41 |
+
|
42 |
+
# output = gr.HTML(label="Prediction Results")
|
43 |
+
# initial_state = gr.State()
|
44 |
+
|
45 |
+
# input_image.change(
|
46 |
+
# predict_fn,
|
47 |
+
# inputs=input_image,
|
48 |
+
# outputs=[output, output_image, initial_state]
|
49 |
+
# )
|
50 |
+
|
51 |
+
# gr.Examples(
|
52 |
+
# examples=example_images,
|
53 |
+
# inputs=input_image
|
54 |
+
# )
|
55 |
+
|
56 |
+
# return {
|
57 |
+
# 'input_image': input_image,
|
58 |
+
# 'output_image': output_image,
|
59 |
+
# 'output': output,
|
60 |
+
# 'initial_state': initial_state
|
61 |
+
# }
|
62 |
+
|
63 |
+
|
64 |
def create_detection_tab(predict_fn, example_images):
|
65 |
+
# 首先定義CSS樣式
|
66 |
+
custom_css = """
|
67 |
+
/* 標籤樣式 */
|
68 |
+
.tab-nav {
|
69 |
+
padding: 0 !important;
|
70 |
+
margin-bottom: 20px !important;
|
71 |
+
border-bottom: 1px solid #e2e8f0 !important;
|
72 |
+
}
|
73 |
+
|
74 |
+
/* 所有標籤的基本樣式 */
|
75 |
+
.tab-nav button {
|
76 |
+
padding: 12px 16px !important;
|
77 |
+
margin: 0 8px !important;
|
78 |
+
font-size: 1.1em !important;
|
79 |
+
font-weight: 500 !important;
|
80 |
+
transition: all 0.3s ease !important;
|
81 |
+
border-bottom: 2px solid transparent !important;
|
82 |
+
background: none !important;
|
83 |
+
position: relative !important;
|
84 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
/* 被選中的標籤樣式 */
|
87 |
+
.tab-nav button.selected {
|
88 |
+
color: #4299e1 !important;
|
89 |
+
border-bottom: 2px solid #4299e1 !important;
|
90 |
+
background: linear-gradient(to bottom, rgba(66, 153, 225, 0.1), transparent) !important;
|
91 |
+
}
|
92 |
+
|
93 |
+
/* hover 效果 */
|
94 |
+
.tab-nav button:hover {
|
95 |
+
color: #4299e1 !important;
|
96 |
+
background: rgba(66, 153, 225, 0.05) !important;
|
97 |
+
}
|
98 |
+
"""
|
99 |
+
|
100 |
+
with gr.Blocks(css=custom_css) as detection_tab:
|
101 |
+
with gr.TabItem("Breed Detection"):
|
102 |
+
gr.HTML("""
|
103 |
+
<div style='
|
104 |
+
text-align: center;
|
105 |
+
padding: 20px 0;
|
106 |
+
margin: 15px 0;
|
107 |
+
background: linear-gradient(to right, rgba(66, 153, 225, 0.1), rgba(72, 187, 120, 0.1));
|
108 |
+
border-radius: 10px;
|
109 |
+
'>
|
110 |
+
<p style='
|
111 |
+
font-size: 1.2em;
|
112 |
+
margin: 0;
|
113 |
+
padding: 0 20px;
|
114 |
+
line-height: 1.5;
|
115 |
+
background: linear-gradient(90deg, #4299e1, #48bb78);
|
116 |
+
-webkit-background-clip: text;
|
117 |
+
-webkit-text-fill-color: transparent;
|
118 |
+
font-weight: 600;
|
119 |
+
'>
|
120 |
+
Upload a picture of a dog, and the model will predict its breed and provide detailed information!
|
121 |
+
</p>
|
122 |
+
<p style='
|
123 |
+
font-size: 0.9em;
|
124 |
+
color: #666;
|
125 |
+
margin-top: 8px;
|
126 |
+
padding: 0 20px;
|
127 |
+
'>
|
128 |
+
Note: The model's predictions may not always be 100% accurate, and it is recommended to use the results as a reference.
|
129 |
+
</p>
|
130 |
+
</div>
|
131 |
+
""")
|
132 |
+
|
133 |
+
with gr.Row():
|
134 |
+
input_image = gr.Image(label="Upload a dog image", type="pil")
|
135 |
+
output_image = gr.Image(label="Annotated Image")
|
136 |
+
|
137 |
+
output = gr.HTML(label="Prediction Results")
|
138 |
+
initial_state = gr.State()
|
139 |
+
|
140 |
+
input_image.change(
|
141 |
+
predict_fn,
|
142 |
+
inputs=input_image,
|
143 |
+
outputs=[output, output_image, initial_state]
|
144 |
+
)
|
145 |
+
|
146 |
+
gr.Examples(
|
147 |
+
examples=example_images,
|
148 |
+
inputs=input_image
|
149 |
+
)
|
150 |
|
151 |
return {
|
152 |
'input_image': input_image,
|