Spaces:
Running
on
Zero
Running
on
Zero
Update breed_detection.py
Browse files- breed_detection.py +10 -52
breed_detection.py
CHANGED
@@ -22,7 +22,7 @@ def create_detection_tab(predict_fn, example_images):
|
|
22 |
-webkit-text-fill-color: transparent;
|
23 |
font-weight: 600;
|
24 |
'>
|
25 |
-
Upload a picture of a dog
|
26 |
</p>
|
27 |
<p style='
|
28 |
font-size: 0.9em;
|
@@ -35,69 +35,27 @@ def create_detection_tab(predict_fn, example_images):
|
|
35 |
</div>
|
36 |
""")
|
37 |
|
38 |
-
# 將輸入方法放在標籤頁中
|
39 |
-
with gr.Tabs():
|
40 |
-
# 標籤頁 1: 上傳圖片 (保留原有功能)
|
41 |
-
with gr.TabItem("Upload Image"):
|
42 |
-
input_image = gr.Image(label="Upload a dog image", type="pil")
|
43 |
-
gr.Examples(
|
44 |
-
examples=example_images,
|
45 |
-
inputs=input_image
|
46 |
-
)
|
47 |
-
|
48 |
-
# 標籤頁 2: 拍照功能 (使用 gr.Webcam 而非 gr.Image(source="webcam"))
|
49 |
-
with gr.TabItem("Take Photo"):
|
50 |
-
camera_input = gr.Webcam(label="Take a photo of a dog")
|
51 |
-
|
52 |
-
# 輸出區域
|
53 |
with gr.Row():
|
|
|
54 |
output_image = gr.Image(label="Annotated Image")
|
55 |
-
|
56 |
-
|
57 |
-
# 使用 State 保存預測結果
|
58 |
initial_state = gr.State()
|
59 |
-
|
60 |
-
# 輔助函數,在函數內部定義避免循環導入
|
61 |
-
def detect_from_inputs(upload_image, camera_image):
|
62 |
-
# 使用最後修改的圖片(優先相機拍攝的圖片)
|
63 |
-
image_to_use = camera_image if camera_image is not None else upload_image
|
64 |
-
|
65 |
-
if image_to_use is None:
|
66 |
-
return "Please upload an image or take a photo first.", None, None
|
67 |
-
|
68 |
-
# 使用作為參數傳入的 predict_fn
|
69 |
-
return predict_fn(image_to_use)
|
70 |
-
|
71 |
-
# 修改輸入圖片事件處理
|
72 |
input_image.change(
|
73 |
predict_fn,
|
74 |
inputs=input_image,
|
75 |
outputs=[output, output_image, initial_state]
|
76 |
)
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
inputs=camera_input,
|
82 |
-
outputs=[output, output_image, initial_state]
|
83 |
-
)
|
84 |
-
|
85 |
-
# 添加按鈕以便使用者可以主動觸發分析
|
86 |
-
with gr.Row():
|
87 |
-
detect_btn = gr.Button("Detect Breed", variant="primary")
|
88 |
-
|
89 |
-
# 為按鈕設置事件處理
|
90 |
-
detect_btn.click(
|
91 |
-
detect_from_inputs,
|
92 |
-
inputs=[input_image, camera_input],
|
93 |
-
outputs=[output, output_image, initial_state]
|
94 |
)
|
95 |
|
96 |
return {
|
97 |
'input_image': input_image,
|
98 |
-
'camera_input': camera_input,
|
99 |
'output_image': output_image,
|
100 |
'output': output,
|
101 |
-
'initial_state': initial_state
|
102 |
-
'detect_btn': detect_btn
|
103 |
}
|
|
|
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;
|
|
|
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 |
}
|