Spaces:
Running
on
Zero
Running
on
Zero
Update breed_detection.py
Browse files- breed_detection.py +51 -11
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, and the model will predict its breed and provide detailed information!
|
26 |
</p>
|
27 |
<p style='
|
28 |
font-size: 0.9em;
|
@@ -35,27 +35,67 @@ def create_detection_tab(predict_fn, example_images):
|
|
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 |
-
|
|
|
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 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
)
|
55 |
|
56 |
return {
|
57 |
'input_image': input_image,
|
|
|
58 |
'output_image': output_image,
|
59 |
'output': output,
|
60 |
-
'initial_state': initial_state
|
61 |
-
|
|
|
|
22 |
-webkit-text-fill-color: transparent;
|
23 |
font-weight: 600;
|
24 |
'>
|
25 |
+
Upload a picture of a dog or take a photo, 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.Tabs():
|
39 |
+
# tab1: upload images
|
40 |
+
with gr.TabItem("Upload Image"):
|
41 |
+
input_image = gr.Image(label="Upload a dog image", type="pil")
|
42 |
+
gr.Examples(
|
43 |
+
examples=example_images,
|
44 |
+
inputs=input_image
|
45 |
+
)
|
46 |
+
|
47 |
+
# tab2: camera feature
|
48 |
+
with gr.TabItem("Take Photo"):
|
49 |
+
camera_input = gr.Image(
|
50 |
+
source="webcam",
|
51 |
+
label="Take a photo of a dog",
|
52 |
+
type="pil",
|
53 |
+
tool="editor" # 允許用戶編輯拍攝的照片
|
54 |
+
)
|
55 |
+
|
56 |
+
# output area
|
57 |
with gr.Row():
|
|
|
58 |
output_image = gr.Image(label="Annotated Image")
|
59 |
+
output = gr.HTML(label="Prediction Results")
|
60 |
+
|
61 |
+
# utilize state to store results
|
62 |
initial_state = gr.State()
|
63 |
+
|
64 |
+
def detect_from_inputs(upload_image, camera_image):
|
65 |
+
image_to_use = camera_image if camera_image is not None else upload_image
|
66 |
+
|
67 |
+
if image_to_use is None:
|
68 |
+
return "Please upload an image or take a photo first.", None, None
|
69 |
+
|
70 |
+
return predict_fn(image_to_use)
|
71 |
+
|
72 |
input_image.change(
|
73 |
predict_fn,
|
74 |
inputs=input_image,
|
75 |
outputs=[output, output_image, initial_state]
|
76 |
)
|
77 |
+
|
78 |
+
# add camera input
|
79 |
+
camera_input.change(
|
80 |
+
predict_fn,
|
81 |
+
inputs=camera_input,
|
82 |
+
outputs=[output, output_image, initial_state]
|
83 |
+
)
|
84 |
+
|
85 |
+
with gr.Row():
|
86 |
+
detect_btn = gr.Button("Detect Breed", variant="primary")
|
87 |
+
|
88 |
+
detect_btn.click(
|
89 |
+
detect_from_inputs,
|
90 |
+
inputs=[input_image, camera_input],
|
91 |
+
outputs=[output, output_image, initial_state]
|
92 |
)
|
93 |
|
94 |
return {
|
95 |
'input_image': input_image,
|
96 |
+
'camera_input': camera_input,
|
97 |
'output_image': output_image,
|
98 |
'output': output,
|
99 |
+
'initial_state': initial_state,
|
100 |
+
'detect_btn': detect_btn
|
101 |
+
}
|