Danush-R commited on
Commit
5737823
1 Parent(s): 1442fd7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
5
+ import numpy as np
6
+ import pickle
7
+
8
+ # Disable oneDNN custom operations
9
+ os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
10
+
11
+ # Ensure TensorFlow uses CPU
12
+ import tensorflow as tf
13
+ tf.config.set_visible_devices([], 'GPU')
14
+
15
+ # Load the saved model
16
+ @st.cache_resource
17
+ def load_keras_model():
18
+ return load_model("best_model.h5")
19
+
20
+ model = load_keras_model()
21
+
22
+ # Load the class labels from a pickle file
23
+ with open("mod_class_labels.pkl", "rb") as f:
24
+ class_indices = pickle.load(f)
25
+
26
+ # Ensure class_indices is a dictionary
27
+ if isinstance(class_indices, list):
28
+ class_indices = {i: label for i, label in enumerate(class_indices)}
29
+
30
+ # Function to preprocess the image
31
+ def preprocess_image(image):
32
+ image = load_img(image, target_size=(256, 256)) # Load the image with target size
33
+ image = img_to_array(image) # Convert the image to array
34
+ image = np.expand_dims(image, axis=0) # Expand dimensions to match the input shape
35
+ image = image / 255.0 # Rescale the image
36
+ return image
37
+
38
+ # Function to make a prediction and get the label
39
+ def predict_image(image):
40
+ image = preprocess_image(image)
41
+ prediction = model.predict(image)
42
+ predicted_class = np.argmax(prediction, axis=1)[0]
43
+ predicted_label = class_indices[predicted_class]
44
+ return predicted_label
45
+
46
+ # Streamlit App
47
+ st.title("Rice Leaf Disease Classification")
48
+
49
+ st.write("Upload an image of a rice leaf and the model will predict its disease category.")
50
+
51
+ # File uploader
52
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
53
+
54
+ if uploaded_file is not None:
55
+ # Display the uploaded image
56
+ image = load_img(uploaded_file, target_size=(256, 256))
57
+ st.image(image, caption='Uploaded Image', use_column_width=True)
58
+ st.write("")
59
+ st.write("Classifying...")
60
+
61
+ # Make a prediction
62
+ predicted_label = predict_image(uploaded_file)
63
+ st.write(f"Predicted label: {predicted_label}")