Spaces:
Runtime error
Runtime error
HashamUllah
commited on
Commit
•
9650572
1
Parent(s):
4b28a17
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,45 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# Load
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
image_array =
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
if __name__ == '__main__':
|
48 |
-
import uvicorn
|
49 |
-
uvicorn.run(app, host='0.0.0.0', port=8080)
|
50 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import json
|
7 |
+
|
8 |
+
# Load the TensorFlow model
|
9 |
+
model = tf.keras.models.load_model('./plant_disease_detection.h5')
|
10 |
+
|
11 |
+
# Load categories
|
12 |
+
with open('./categories.json') as f:
|
13 |
+
categories = json.load(f)
|
14 |
+
|
15 |
+
def preprocess_image(image):
|
16 |
+
# Convert the image to a NumPy array
|
17 |
+
image = image.resize((224, 224)) # Adjust size as needed
|
18 |
+
image_array = np.array(image) / 255.0 # Normalize to [0, 1]
|
19 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
20 |
+
return image_array
|
21 |
+
|
22 |
+
def predict(image):
|
23 |
+
image_array = preprocess_image(image)
|
24 |
+
|
25 |
+
# Make prediction
|
26 |
+
predictions = model.predict(image_array)
|
27 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
28 |
+
|
29 |
+
# Map to category names
|
30 |
+
predicted_label = categories.get(str(predicted_class), 'Unknown')
|
31 |
+
|
32 |
+
return predicted_label, float(predictions[0][predicted_class])
|
33 |
+
|
34 |
+
# Create a Gradio interface
|
35 |
+
iface = gr.Interface(
|
36 |
+
fn=predict,
|
37 |
+
inputs=gr.Image(type="pil"),
|
38 |
+
outputs=[gr.Label(), gr.Number()],
|
39 |
+
title="Plant Disease Detection",
|
40 |
+
description="Upload an image of a plant leaf to detect if it has any diseases."
|
41 |
+
)
|
42 |
+
|
43 |
+
# Launch the interface
|
44 |
+
if __name__ == "__main__":
|
45 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|