Dhruv2838 commited on
Commit
791c69a
1 Parent(s): 637eb17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -1,43 +1,51 @@
1
  import numpy as np
2
- import pandas as pd
3
- import tensorflow as tf
4
  import streamlit as st
5
-
6
  from PIL import Image
7
 
8
- model = tf.keras.models.load_model(r"C:\Dhruv\potato diseases\my_model.h5")
 
 
 
 
 
 
 
9
 
 
 
10
 
 
11
  resize_and_rescale = tf.keras.Sequential([
12
- tf.keras.layers.experimental.preprocessing.Resizing(256,256),
13
- tf.keras.layers.experimental.preprocessing.Rescaling(1.0/255)
14
  ])
15
 
16
  def predict(model, img):
17
-
18
  class_names = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
19
-
 
20
  img_array = tf.keras.preprocessing.image.img_to_array(np.array(img))
21
- img_array = tf.expand_dims(img_array, 0)
22
 
 
23
  img_array = resize_and_rescale(img_array)
24
 
 
25
  predictions = model.predict(img_array)
26
-
27
  predicted_class = class_names[np.argmax(predictions[0])]
28
- confidence = round(100 * (np.max(predictions[0])), 2)
29
  return predicted_class, confidence
30
 
 
31
  st.title("Potato Disease Classification")
32
 
33
  uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
34
 
35
- if uploaded_image:
36
-
37
- st.image(uploaded_image)
38
  img = Image.open(uploaded_image)
39
 
40
  predicted_class, confidence = predict(model=model, img=img)
41
-
42
  st.markdown(f"The predicted class is **{predicted_class}** with **{confidence}%** confidence")
43
-
 
1
  import numpy as np
2
+ import tensorflow as tf
 
3
  import streamlit as st
 
4
  from PIL import Image
5
 
6
+ # Custom deserialization function for SparseCategoricalCrossentropy
7
+ def custom_sparse_categorical_crossentropy_from_config(config):
8
+ # Remove the 'fn' key from config if it exists
9
+ config.pop('fn', None)
10
+ return tf.keras.losses.SparseCategoricalCrossentropy(**config)
11
+
12
+ # Load the model with custom objects
13
+ custom_objects = {'SparseCategoricalCrossentropy': custom_sparse_categorical_crossentropy_from_config}
14
 
15
+ model_path = r"C:\Dhruv\potato diseases\my_model.h5"
16
+ model = tf.keras.models.load_model(model_path, custom_objects=custom_objects)
17
 
18
+ # Image preprocessing function
19
  resize_and_rescale = tf.keras.Sequential([
20
+ tf.keras.layers.Resizing(256, 256),
21
+ tf.keras.layers.Rescaling(1.0 / 255)
22
  ])
23
 
24
  def predict(model, img):
 
25
  class_names = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
26
+
27
+ # Convert the image to array
28
  img_array = tf.keras.preprocessing.image.img_to_array(np.array(img))
29
+ img_array = tf.expand_dims(img_array, 0) # Create batch axis
30
 
31
+ # Preprocess the image
32
  img_array = resize_and_rescale(img_array)
33
 
34
+ # Make prediction
35
  predictions = model.predict(img_array)
 
36
  predicted_class = class_names[np.argmax(predictions[0])]
37
+ confidence = round(100 * np.max(predictions[0]), 2)
38
  return predicted_class, confidence
39
 
40
+ # Streamlit app
41
  st.title("Potato Disease Classification")
42
 
43
  uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
44
 
45
+ if uploaded_image is not None:
46
+ st.image(uploaded_image, caption='Uploaded Image', use_column_width=True)
 
47
  img = Image.open(uploaded_image)
48
 
49
  predicted_class, confidence = predict(model=model, img=img)
50
+
51
  st.markdown(f"The predicted class is **{predicted_class}** with **{confidence}%** confidence")