ajaydamsani commited on
Commit
54752e2
1 Parent(s): 19c2c17

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +74 -0
  2. crop_disease_detection_model.h5 +3 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from PIL import Image
4
+
5
+ import numpy as np
6
+ import tensorflow as tf
7
+ import streamlit as st
8
+ import matplotlib.pyplot as plt
9
+
10
+ # Load the pre-trained model and class indices
11
+ working_dir = os.path.dirname(os.path.abspath(__file__))
12
+ model_path = f"{working_dir}/trained model/crop_disease_detection_model.h5"
13
+ model = tf.keras.models.load_model(model_path)
14
+ class_indices = json.load(open(f"{working_dir}/class_indices.json"))
15
+
16
+ # Function to Load and Preprocess the Image using Pillow
17
+ def load_and_preprocess_image(image_path, target_size=(224, 224)):
18
+ img = Image.open(image_path)
19
+ img = img.resize(target_size)
20
+ img_array = np.array(img)
21
+ img_array = np.expand_dims(img_array, axis=0)
22
+ img_array = img_array.astype('float32') / 255.
23
+ return img_array
24
+
25
+ # Function to Predict the Class of an Image
26
+ def predict_image_class(model, img_array, class_indices):
27
+ predictions = model.predict(img_array)
28
+ predicted_class_index = np.argmax(predictions, axis=1)[0]
29
+ predicted_class_name = class_indices[str(predicted_class_index)]
30
+ confidence_score = predictions[0][predicted_class_index]
31
+ return predicted_class_name, confidence_score
32
+
33
+ # Streamlit App
34
+ st.title('Plant Disease Classifier')
35
+
36
+ # Upload and preprocess the image only once
37
+ uploaded_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
38
+ if uploaded_image is not None:
39
+ img_array = load_and_preprocess_image(uploaded_image)
40
+ st.session_state.img_array = img_array
41
+ st.session_state.image_uploaded = True
42
+
43
+ # Display tabs for Identification and Visualization side by side
44
+ col1, col2 = st.columns(2)
45
+ with col1:
46
+ if st.button('Identification'):
47
+ st.session_state.tab_selected = 'Identification'
48
+ with col2:
49
+ if st.button('Visualization'):
50
+ st.session_state.tab_selected = 'Visualization'
51
+
52
+ selected_tab = st.session_state.get('tab_selected', 'Identification')
53
+
54
+ if st.session_state.get('image_uploaded', False):
55
+ if selected_tab == 'Identification':
56
+ st.header('Plant Disease Identification')
57
+ st.image(uploaded_image, caption='Uploaded Image', use_column_width=False)
58
+ predicted_class, confidence_score = predict_image_class(model, img_array, class_indices)
59
+ st.write(f'Prediction: {predicted_class} ({confidence_score:.2f} confidence)')
60
+
61
+ elif selected_tab == 'Visualization':
62
+ st.header('Confidence Scores Visualization')
63
+ plt.figure(figsize=(12, 6)) # Smaller graph size
64
+ plt.bar(class_indices.values(), model.predict(img_array)[0])
65
+ plt.xlabel('Class')
66
+ plt.ylabel('Confidence Score')
67
+ plt.xticks(rotation=90, ha='right')
68
+ plt.title('Confidence Scores for Predicted Classes')
69
+ st.pyplot(plt)
70
+
71
+
72
+
73
+
74
+
crop_disease_detection_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:503e768444890fce8ecedd5d8fe0c8425016ed8109bb17134a0da09662802ab8
3
+ size 573695016