Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,49 +3,69 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def classify(image):
|
| 15 |
if image is None:
|
| 16 |
-
return
|
|
|
|
| 17 |
if not isinstance(image, Image.Image):
|
| 18 |
image = Image.fromarray(image)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
img_array = np.array(
|
| 22 |
img_array = np.expand_dims(img_array, axis=0)
|
| 23 |
|
| 24 |
predictions = model.predict(img_array)[0]
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 30 |
gr.Markdown(
|
| 31 |
"""
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
"""
|
| 35 |
)
|
| 36 |
|
| 37 |
with gr.Row():
|
| 38 |
with gr.Column():
|
| 39 |
-
image_input = gr.Image(type="pil", label="
|
| 40 |
-
submit_button = gr.Button("
|
| 41 |
-
|
| 42 |
with gr.Column():
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
submit_button.click(
|
| 46 |
fn=classify,
|
| 47 |
inputs=image_input,
|
| 48 |
-
outputs=
|
| 49 |
api_name="predict"
|
| 50 |
)
|
| 51 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Constants
|
| 7 |
+
IMAGE_SIZE = (300, 300)
|
| 8 |
|
| 9 |
+
# Class index to label mapping
|
| 10 |
+
CLASS_NAMES = {
|
| 11 |
+
0: "A healthy tomato leaf",
|
| 12 |
+
1: "A tomato leaf with Leaf Mold",
|
| 13 |
+
2: "A tomato leaf with Target Spot",
|
| 14 |
+
3: "A tomato leaf with Late Blight",
|
| 15 |
+
4: "A tomato leaf with Early Blight",
|
| 16 |
+
5: "A tomato leaf with Bacterial Spot",
|
| 17 |
+
6: "A tomato leaf with Septoria Leaf Spot",
|
| 18 |
+
7: "A tomato leaf with Tomato Mosaic Virus",
|
| 19 |
+
8: "A tomato leaf with Tomato Yellow Leaf Curl Virus",
|
| 20 |
+
9: "A tomato leaf with Spider Mites Two-spotted Spider Mite"
|
| 21 |
+
}
|
| 22 |
|
| 23 |
+
# Load the model from Hugging Face Hub
|
| 24 |
+
model_url = "https://huggingface.co/chimithecat/penyakit_tomat/resolve/main/Tomato_Models.h5"
|
| 25 |
+
model_path = tf.keras.utils.get_file("Tomato_Models.h5", model_url)
|
| 26 |
+
model = tf.keras.models.load_model(model_path)
|
| 27 |
+
|
| 28 |
+
# Prediction function
|
| 29 |
def classify(image):
|
| 30 |
if image is None:
|
| 31 |
+
return "Tidak ada gambar.", ""
|
| 32 |
+
|
| 33 |
if not isinstance(image, Image.Image):
|
| 34 |
image = Image.fromarray(image)
|
| 35 |
|
| 36 |
+
img = image.resize(IMAGE_SIZE)
|
| 37 |
+
img_array = np.array(img) / 255.0
|
| 38 |
img_array = np.expand_dims(img_array, axis=0)
|
| 39 |
|
| 40 |
predictions = model.predict(img_array)[0]
|
| 41 |
+
predicted_index = int(np.argmax(predictions))
|
| 42 |
+
confidence = float(predictions[predicted_index])
|
| 43 |
+
label = CLASS_NAMES[predicted_index]
|
| 44 |
|
| 45 |
+
return f"{label}", f"Confidence: {confidence*100:.2f}%"
|
| 46 |
+
|
| 47 |
+
# UI with Gradio Blocks
|
| 48 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 49 |
gr.Markdown(
|
| 50 |
"""
|
| 51 |
+
# π
Tomato Leaf Disease Classifier
|
| 52 |
+
Upload a photo of a tomato leaf to detect its potential disease.
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column():
|
| 58 |
+
image_input = gr.Image(type="pil", label="Upload Leaf Image")
|
| 59 |
+
submit_button = gr.Button("Analyze", variant="primary")
|
| 60 |
+
|
| 61 |
with gr.Column():
|
| 62 |
+
result_output = gr.Text(label="Prediction Result")
|
| 63 |
+
confidence_output = gr.Text(label="Confidence")
|
| 64 |
|
| 65 |
submit_button.click(
|
| 66 |
fn=classify,
|
| 67 |
inputs=image_input,
|
| 68 |
+
outputs=[result_output, confidence_output],
|
| 69 |
api_name="predict"
|
| 70 |
)
|
| 71 |
|