Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,7 @@
|
|
1 |
-
import streamlit as st
|
2 |
import tensorflow as tf
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
-
import io
|
6 |
-
import json
|
7 |
|
8 |
-
# Load categories
|
9 |
-
with open('categories.json') as f:
|
10 |
-
categories = json.load(f)
|
11 |
-
|
12 |
-
# Ensure the correct path
|
13 |
-
model_path = 'plant_disease_detection_model.h5'
|
14 |
-
|
15 |
-
# Load the TensorFlow model
|
16 |
try:
|
17 |
-
model = tf.keras.models.load_model(
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
def preprocess_image(image):
|
23 |
-
# Convert the image to a NumPy array
|
24 |
-
image = Image.open(io.BytesIO(image))
|
25 |
-
image = image.resize((224, 224)) # Adjust size as needed
|
26 |
-
image_array = np.array(image) / 255.0 # Normalize to [0, 1]
|
27 |
-
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
28 |
-
return image_array
|
29 |
-
|
30 |
-
def main():
|
31 |
-
st.title("Plant Disease Detection")
|
32 |
-
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
|
33 |
-
if uploaded_file is not None:
|
34 |
-
# Preprocess image
|
35 |
-
image_array = preprocess_image(uploaded_file.read())
|
36 |
-
|
37 |
-
# Make prediction
|
38 |
-
predictions = model.predict(image_array)
|
39 |
-
predicted_class = np.argmax(predictions, axis=1)[0]
|
40 |
-
confidence = float(predictions[0][predicted_class])
|
41 |
-
|
42 |
-
# Map to category names
|
43 |
-
predicted_label = categories.get(str(predicted_class), 'Unknown')
|
44 |
-
|
45 |
-
st.write(f"Prediction: {predicted_label}")
|
46 |
-
st.write(f"Confidence: {confidence:.2f}")
|
47 |
-
|
48 |
-
if __name__ == "__main__":
|
49 |
-
main()
|
|
|
|
|
1 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
try:
|
4 |
+
model = tf.keras.models.load_model('plant_disease_detection_model.h5')
|
5 |
+
print("Model loaded successfully")
|
6 |
+
except Exception as e:
|
7 |
+
print(f"Error loading model: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|