ryanwang058
commited on
Commit
·
e09f8ee
1
Parent(s):
8d91a27
Allow two usages
Browse files
app.py
CHANGED
@@ -39,18 +39,40 @@ def classify_preloaded_image(subdirectory, image_name, model_name):
|
|
39 |
image = Image.open(image_path).convert("RGB")
|
40 |
return predict(image, model_name)
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
model_choices = list(model_paths.keys())
|
43 |
|
44 |
# Define Gradio app
|
45 |
with gr.Blocks() as demo:
|
46 |
gr.Markdown("# Plant Disease Classifier")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
with gr.Tab("Select a Preloaded Image"):
|
49 |
with gr.Row():
|
50 |
subdir_dropdown = gr.Dropdown(choices=get_subdirectories(TEST_IMAGE_DIR), label="Select a Subdirectory")
|
51 |
image_dropdown = gr.Dropdown(choices=[], label="Select an Image")
|
52 |
model_input_preloaded = gr.Dropdown(choices=model_choices, label="Select Model", value="resnet")
|
53 |
-
|
|
|
|
|
|
|
54 |
classify_button_preloaded = gr.Button("Classify")
|
55 |
output_text_preloaded = gr.Textbox(label="Predicted Class")
|
56 |
|
@@ -59,6 +81,10 @@ with gr.Blocks() as demo:
|
|
59 |
return gr.update(choices=get_images_in_subdirectory(subdirectory))
|
60 |
|
61 |
subdir_dropdown.change(update_images, inputs=subdir_dropdown, outputs=image_dropdown)
|
|
|
|
|
|
|
|
|
62 |
classify_button_preloaded.click(
|
63 |
classify_preloaded_image, inputs=[subdir_dropdown, image_dropdown, model_input_preloaded], outputs=output_text_preloaded
|
64 |
)
|
|
|
39 |
image = Image.open(image_path).convert("RGB")
|
40 |
return predict(image, model_name)
|
41 |
|
42 |
+
def display_selected_image(subdirectory, image_name):
|
43 |
+
"""Display the selected image."""
|
44 |
+
image_path = os.path.join(TEST_IMAGE_DIR, subdirectory, image_name)
|
45 |
+
if os.path.exists(image_path):
|
46 |
+
return Image.open(image_path).convert("RGB")
|
47 |
+
return None
|
48 |
+
|
49 |
+
def classify_uploaded_image(image, model_name):
|
50 |
+
return predict(image, model_name)
|
51 |
+
|
52 |
model_choices = list(model_paths.keys())
|
53 |
|
54 |
# Define Gradio app
|
55 |
with gr.Blocks() as demo:
|
56 |
gr.Markdown("# Plant Disease Classifier")
|
57 |
+
|
58 |
+
with gr.Tab("Upload an Image"):
|
59 |
+
with gr.Row():
|
60 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
61 |
+
model_input_upload = gr.Dropdown(choices=model_choices, label="Select Model", value="resnet")
|
62 |
+
classify_button_upload = gr.Button("Classify")
|
63 |
+
output_text_upload = gr.Textbox(label="Predicted Class")
|
64 |
+
classify_button_upload.click(classify_uploaded_image, inputs=[image_input, model_input_upload], outputs=output_text_upload)
|
65 |
+
|
66 |
|
67 |
with gr.Tab("Select a Preloaded Image"):
|
68 |
with gr.Row():
|
69 |
subdir_dropdown = gr.Dropdown(choices=get_subdirectories(TEST_IMAGE_DIR), label="Select a Subdirectory")
|
70 |
image_dropdown = gr.Dropdown(choices=[], label="Select an Image")
|
71 |
model_input_preloaded = gr.Dropdown(choices=model_choices, label="Select Model", value="resnet")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
image_display = gr.Image(label="Selected Image", interactive=False)
|
75 |
+
|
76 |
classify_button_preloaded = gr.Button("Classify")
|
77 |
output_text_preloaded = gr.Textbox(label="Predicted Class")
|
78 |
|
|
|
81 |
return gr.update(choices=get_images_in_subdirectory(subdirectory))
|
82 |
|
83 |
subdir_dropdown.change(update_images, inputs=subdir_dropdown, outputs=image_dropdown)
|
84 |
+
|
85 |
+
# Update displayed image based on selected image
|
86 |
+
image_dropdown.change(display_selected_image, inputs=[subdir_dropdown, image_dropdown], outputs=image_display)
|
87 |
+
|
88 |
classify_button_preloaded.click(
|
89 |
classify_preloaded_image, inputs=[subdir_dropdown, image_dropdown, model_input_preloaded], outputs=output_text_preloaded
|
90 |
)
|