agayabag commited on
Commit
446f42c
·
1 Parent(s): a8279b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -30
app.py CHANGED
@@ -1,52 +1,44 @@
1
  import streamlit as st
2
- import pandas as pd
3
  import numpy as np
4
- from tensorflow import keras
5
- import tensorflow as tf
6
- from keras.models import load_model
7
- import matplotlib.pyplot as plt
8
  from PIL import Image
9
 
10
  st.title('Plant Disease Recognition')
11
 
12
- # import model
13
 
14
  model = load_model('plant_model.h5')
15
 
16
- # prediction function
17
 
18
- def prediction(file):
19
- img = tf.keras.utils.load_img(file, target_size=(240, 240))
20
- x = tf.keras.utils.img_to_array(img)
21
- x = np.expand_dims(x, axis=0)
 
 
 
22
 
23
- # predict the class probabilities
24
-
25
- classes = model.predict(x)
26
 
27
- # get the predicted class label
28
-
29
- classes = np.ravel(classes) # convert to 1D array
30
- idx = np.argmax(classes)
31
- class_name = ['Healthy', 'Powdery', 'Rust'][idx]
32
-
33
- return class_name
34
 
35
  # file uploader
36
 
37
  uploaded_file = st.file_uploader("Upload your leaf picture.")
38
- if uploaded_file is not None:
39
- image = Image.open(uploaded_file)
40
- image = image.resize((240, 240))
41
- image = tf.keras.preprocessing.image.img_to_array(image)
42
- image = image / 255.0
43
- image = tf.expand_dims(image, axis=0)
44
-
45
  # result
46
 
47
  if st.button('Predict'):
48
  if uploaded_file is None:
49
  st.write('Please upload a leaf picture first.')
50
  else:
51
- result = prediction(uploaded_file)
52
- st.write('This leaf belongs to {} class.'.format(result))
 
 
1
  import streamlit as st
 
2
  import numpy as np
3
+ from tensorflow.keras.models import load_model
 
 
 
4
  from PIL import Image
5
 
6
  st.title('Plant Disease Recognition')
7
 
8
+ # import the model
9
 
10
  model = load_model('plant_model.h5')
11
 
12
+ # define the preprocessing function
13
 
14
+ def preprocess_image(image):
15
+ image = image.resize((240, 240)) # resize the image to the desired dimensions
16
+ image = image.convert("RGB") # convert the image to RGB mode if needed
17
+ image = np.array(image) # convert the image to a NumPy array
18
+ image = image / 255.0 # normalize the pixel values to the range of 0 to 1
19
+ image = np.expand_dims(image, axis=0) # add an extra dimension for batch size
20
+ return image
21
 
22
+ # define the prediction function
 
 
23
 
24
+ def prediction(image):
25
+ preprocessed_image = preprocess_image(image)
26
+ classes = model.predict(preprocessed_image)
27
+ predicted_class_index = np.argmax(classes)
28
+ class_labels = ['Healthy', 'Powdery', 'Rust']
29
+ predicted_class = class_labels[predicted_class_index]
30
+ return predicted_class
31
 
32
  # file uploader
33
 
34
  uploaded_file = st.file_uploader("Upload your leaf picture.")
35
+
 
 
 
 
 
 
36
  # result
37
 
38
  if st.button('Predict'):
39
  if uploaded_file is None:
40
  st.write('Please upload a leaf picture first.')
41
  else:
42
+ image = Image.open(uploaded_file)
43
+ result = prediction(image)
44
+ st.write('This leaf belongs to the {} class.'.format(result))