Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,18 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
# ==========================
|
| 9 |
-
model = tf.keras.models.load_model("orange_disease_model.h5")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
class_names = [
|
| 15 |
"Healthy",
|
| 16 |
"Black spot",
|
|
@@ -19,53 +22,30 @@ class_names = [
|
|
| 19 |
"Melanose"
|
| 20 |
]
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# ==========================
|
| 25 |
-
IMG_SIZE = 224
|
| 26 |
|
| 27 |
-
# ==========================
|
| 28 |
-
# PREDICTION FUNCTION
|
| 29 |
-
# ==========================
|
| 30 |
def predict(image):
|
| 31 |
|
| 32 |
-
# Convert to RGB
|
| 33 |
image = image.convert("RGB")
|
| 34 |
-
|
| 35 |
-
# Resize
|
| 36 |
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 37 |
|
| 38 |
-
|
| 39 |
-
img_array = np.array(image)
|
| 40 |
-
|
| 41 |
-
# Normalize
|
| 42 |
-
img_array = img_array / 255.0
|
| 43 |
-
|
| 44 |
-
# Expand dims
|
| 45 |
img_array = np.expand_dims(img_array, axis=0)
|
| 46 |
|
| 47 |
-
# Predict
|
| 48 |
prediction = model.predict(img_array)
|
| 49 |
|
| 50 |
-
confidence = float(np.max(prediction))
|
| 51 |
class_index = int(np.argmax(prediction))
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
|
| 55 |
-
return f"Prediction: {result} | Confidence: {confidence:.4f}"
|
| 56 |
|
| 57 |
-
|
| 58 |
-
# GRADIO INTERFACE
|
| 59 |
-
# ==========================
|
| 60 |
-
interface = gr.Interface(
|
| 61 |
fn=predict,
|
| 62 |
inputs=gr.Image(type="pil"),
|
| 63 |
outputs="text",
|
| 64 |
-
title="Orange Disease Detection"
|
| 65 |
-
description="Upload image of orange leaf to detect disease"
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
# LAUNCH
|
| 70 |
-
# ==========================
|
| 71 |
-
interface.launch()
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Fix compatibility
|
| 8 |
+
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load model
|
| 11 |
+
model = tf.keras.models.load_model(
|
| 12 |
+
"orange_disease_model.h5",
|
| 13 |
+
compile=False
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Classes (MODIFIER)
|
| 17 |
class_names = [
|
| 18 |
"Healthy",
|
| 19 |
"Black spot",
|
|
|
|
| 22 |
"Melanose"
|
| 23 |
]
|
| 24 |
|
| 25 |
+
IMG_SIZE = 256
|
| 26 |
+
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
def predict(image):
|
| 29 |
|
|
|
|
| 30 |
image = image.convert("RGB")
|
|
|
|
|
|
|
| 31 |
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 32 |
|
| 33 |
+
img_array = np.array(image) / 255.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
img_array = np.expand_dims(img_array, axis=0)
|
| 35 |
|
|
|
|
| 36 |
prediction = model.predict(img_array)
|
| 37 |
|
|
|
|
| 38 |
class_index = int(np.argmax(prediction))
|
| 39 |
+
confidence = float(np.max(prediction))
|
| 40 |
|
| 41 |
+
return f"Prediction: {class_names[class_index]} | Confidence: {confidence:.4f}"
|
| 42 |
|
|
|
|
| 43 |
|
| 44 |
+
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
| 45 |
fn=predict,
|
| 46 |
inputs=gr.Image(type="pil"),
|
| 47 |
outputs="text",
|
| 48 |
+
title="Orange Disease Detection API"
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
+
demo.launch()
|
|
|
|
|
|
|
|
|