sancho10 commited on
Commit
e5971fc
·
1 Parent(s): 680d564

Description of changes

Browse files
Files changed (7) hide show
  1. .DS_Store +0 -0
  2. Application/app.py +169 -0
  3. app.py +169 -0
  4. pepper/.DS_Store +0 -0
  5. potato/.DS_Store +0 -0
  6. requirements.txt +81 -0
  7. tomato/.DS_Store +0 -0
.DS_Store ADDED
Binary file (8.2 kB). View file
 
Application/app.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import joblib
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from tensorflow import keras
7
+ from keras.layers import Dense
8
+ from keras.models import Sequential, load_model
9
+ from tensorflow.keras.layers import BatchNormalization
10
+ from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
11
+ from tensorflow.keras.preprocessing import image as keras_image
12
+ from skimage import io, color, transform
13
+ from skimage.feature import hog
14
+ # from keras.layers import Dense,Flatten,BatchNormalization,Dropout
15
+ from scipy.special import softmax
16
+
17
+ # ... (Previous imports and model loading code remain the same)
18
+
19
+
20
+ def preprocess_image(img, model_name):
21
+ # if model_name == 'Resnet':
22
+ # # Resize image to match ResNet input size (256x256)
23
+ # img = img.resize((256, 256))
24
+ # img_array = keras_image.img_to_array(img)
25
+ # img_array = np.expand_dims(img_array, axis=0)
26
+ # img_array = preprocess_input(img_array)
27
+ # return img_array
28
+ if model_name == 'SVM':
29
+ resized_image = transform.resize(np.array(img), (100, 100), mode='reflect')
30
+ if resized_image.shape[2] == 4:
31
+ resized_image = color.rgba2rgb(resized_image)
32
+ grayscale_image = color.rgb2gray(resized_image)
33
+ hog_features = hog(grayscale_image, orientations=9, pixels_per_cell=(8, 8),
34
+ cells_per_block=(2, 2), block_norm='L2-Hys')
35
+ return hog_features
36
+ elif model_name == "ANN" or model_name == "CNN":
37
+ resized_image = img.resize((256, 256))
38
+ img_array = keras_image.img_to_array(resized_image)
39
+ img_array = np.expand_dims(img_array, axis=0)
40
+ img_array = img_array / 255.0 # Normalize pixel values to be between 0 and 1
41
+ return img_array
42
+
43
+
44
+
45
+ def predict(model, img, model_name):
46
+ img_array = preprocess_image(img, model_name)
47
+
48
+ # if model_name == 'Resnet':
49
+ # predictions = model.predict(img_array)
50
+ # decoded_predictions = custom_decode_predictions(predictions)
51
+ # return decoded_predictions
52
+
53
+ if model_name == 'SVM':
54
+ # Flatten the array before passing it to the SVM model
55
+ img_array_flattened = img_array.flatten().reshape(1, -1)
56
+ prediction = model.predict(img_array_flattened)
57
+ return prediction[0]
58
+ elif model_name == 'ANN' or model_name == 'CNN':
59
+ prediction = model.predict(img_array)
60
+ confidence_scores = softmax(prediction, axis=1) # Apply softmax to get confidence scores
61
+ predicted_class = np.argmax(confidence_scores, axis=1)[0] # Get the class with the highest confidence
62
+ confidence = confidence_scores[0, predicted_class]
63
+ return predicted_class, confidence
64
+
65
+ def custom_decode_predictions(predictions):
66
+ # Assuming predictions is a 2D array with shape (1, num_classes)
67
+ num_classes = predictions.shape[1]
68
+
69
+ # Assuming predictions contain class probabilities
70
+ confidence_scores = predictions[0]
71
+
72
+ # Get the index of the class with the highest probability
73
+ predicted_class_index = np.argmax(confidence_scores)
74
+
75
+ # Return the class index, confidence score, and probability distribution
76
+ return [
77
+ predicted_class_index,
78
+ confidence_scores[predicted_class_index],
79
+ ]
80
+
81
+ # Load the models
82
+ pper_model_CNN = load_model("../pepper/models/pepper_CNN.h5")
83
+ pper_model_SVM = joblib.load('../pepper/models/pepper_hog_svm_model.pkl')
84
+ # pper_model_resnet = load_model('../pepper/models/pepper_resnet50.h5')
85
+ potato_model_CNN = load_model('../potato/models/potato_CNN.h5')
86
+ potato_model_SVM = joblib.load('../potato/models/potato_hog_svm_model.pkl')
87
+ # potato_model_resnet = load_model('../potato/models/potato_resnet50.h5')
88
+ tomato_model_CNN = load_model('../tomato/models/tomato_CNN.h5')
89
+ tomato_model_SVM = joblib.load('../tomato/models/tomato_hog_svm_model.pkl')
90
+ tomato_model_ANN = load_model('../tomato/models/ANN_tomato.h5')
91
+ # tomato_model_resnet = load_model('../tomato/models/tomato_resnet50.h5')
92
+ Potato_class = 3
93
+ Pepper_class = 2
94
+ Tomato_class = 10
95
+
96
+ # Set the title of the app
97
+ st.title('Plant Disease Detection App')
98
+
99
+ # Create a dropdown menu for the user to choose a plant
100
+ plant = st.selectbox('Choose a plant', ('Pepper', 'Potato', 'Tomato'))
101
+
102
+ # Create a radio button for the user to choose a model
103
+ if plant == 'Pepper':
104
+ no_of_classes=2
105
+ class_name = ['pepper_bacterial_spot', 'pepper_bell_healthy']
106
+ model = st.radio('Choose a model', ('CNN', 'SVM', ))
107
+ if model == 'SVM':
108
+ model_func = pper_model_SVM
109
+ elif model == 'CNN':
110
+ model_func = pper_model_CNN
111
+ # elif model == 'Resnet':
112
+ # model_func = pper_model_resnet
113
+ elif plant == 'Potato':
114
+ class_name=['EarlyBlight','Healthy', 'LateBlight']
115
+ no_of_classes=2
116
+ model = st.radio('Choose a model', ('SVM', 'CNN', ))
117
+ if model == 'SVM':
118
+ model_func =potato_model_SVM
119
+ elif model == 'CNN':
120
+ model_func =potato_model_CNN
121
+ # elif model == 'Resnet':
122
+ # model_func = potato_model_resnet
123
+ elif plant == 'Tomato':
124
+ class_name=['Tomato___Bacterial_spot',
125
+ 'Tomato___Early_blight',
126
+ 'Tomato___Late_blight',
127
+ 'Tomato___Leaf_Mold',
128
+ 'Tomato___Septoria_leaf_spot',
129
+ 'Tomato___Spider_mites Two-spotted_spider_mite',
130
+ 'Tomato___Target_Spot',
131
+ 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
132
+ 'Tomato___Tomato_mosaic_virus',
133
+ 'Tomato___healthy']
134
+
135
+ no_of_classes=10
136
+ model = st.radio('Choose a model', ('SVM','CNN', 'ANN', ))
137
+ if model == 'SVM':
138
+ model_func = tomato_model_SVM
139
+ elif model == 'CNN':
140
+ model_func = tomato_model_CNN
141
+ elif model == 'ANN':
142
+ model_func = tomato_model_ANN
143
+ # elif model== 'Resnet':
144
+ # model_func = tomato_model_resnet
145
+
146
+ # Create a file uploader for the user to upload an image
147
+ uploaded_file = st.file_uploader('Upload an image', type='jpg')
148
+
149
+ # Display the selected model and plant
150
+ st.write('Selected plant:', plant)
151
+ st.write('Selected model:', model)
152
+
153
+ # If an image is uploaded, display it and make a prediction
154
+ if uploaded_file is not None:
155
+ image = Image.open(uploaded_file)
156
+ # st.image(image, caption='Uploaded Image', use_column_width=True)
157
+
158
+ # Make a prediction using the selected model
159
+ prediction_result = predict(model_func, image, model)
160
+ print(no_of_classes)
161
+ # st.write(prediction_result)
162
+ # Display the result based on the model type
163
+ if model == 'SVM':
164
+ st.write(f'Prediction: {prediction_result}')
165
+ else:
166
+ predicted_class, confidence = prediction_result
167
+ st.write(f'Prediction: {class_name[predicted_class]}')
168
+ st.write(f'Confidence: {confidence}')
169
+
app.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import joblib
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from tensorflow import keras
7
+ from keras.layers import Dense
8
+ from keras.models import Sequential, load_model
9
+ from tensorflow.keras.layers import BatchNormalization
10
+ from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
11
+ from tensorflow.keras.preprocessing import image as keras_image
12
+ from skimage import io, color, transform
13
+ from skimage.feature import hog
14
+ # from keras.layers import Dense,Flatten,BatchNormalization,Dropout
15
+ from scipy.special import softmax
16
+
17
+ # ... (Previous imports and model loading code remain the same)
18
+
19
+
20
+ def preprocess_image(img, model_name):
21
+ # if model_name == 'Resnet':
22
+ # # Resize image to match ResNet input size (256x256)
23
+ # img = img.resize((256, 256))
24
+ # img_array = keras_image.img_to_array(img)
25
+ # img_array = np.expand_dims(img_array, axis=0)
26
+ # img_array = preprocess_input(img_array)
27
+ # return img_array
28
+ if model_name == 'SVM':
29
+ resized_image = transform.resize(np.array(img), (100, 100), mode='reflect')
30
+ if resized_image.shape[2] == 4:
31
+ resized_image = color.rgba2rgb(resized_image)
32
+ grayscale_image = color.rgb2gray(resized_image)
33
+ hog_features = hog(grayscale_image, orientations=9, pixels_per_cell=(8, 8),
34
+ cells_per_block=(2, 2), block_norm='L2-Hys')
35
+ return hog_features
36
+ elif model_name == "ANN" or model_name == "CNN":
37
+ resized_image = img.resize((256, 256))
38
+ img_array = keras_image.img_to_array(resized_image)
39
+ img_array = np.expand_dims(img_array, axis=0)
40
+ img_array = img_array / 255.0 # Normalize pixel values to be between 0 and 1
41
+ return img_array
42
+
43
+
44
+
45
+ def predict(model, img, model_name):
46
+ img_array = preprocess_image(img, model_name)
47
+
48
+ # if model_name == 'Resnet':
49
+ # predictions = model.predict(img_array)
50
+ # decoded_predictions = custom_decode_predictions(predictions)
51
+ # return decoded_predictions
52
+
53
+ if model_name == 'SVM':
54
+ # Flatten the array before passing it to the SVM model
55
+ img_array_flattened = img_array.flatten().reshape(1, -1)
56
+ prediction = model.predict(img_array_flattened)
57
+ return prediction[0]
58
+ elif model_name == 'ANN' or model_name == 'CNN':
59
+ prediction = model.predict(img_array)
60
+ confidence_scores = softmax(prediction, axis=1) # Apply softmax to get confidence scores
61
+ predicted_class = np.argmax(confidence_scores, axis=1)[0] # Get the class with the highest confidence
62
+ confidence = confidence_scores[0, predicted_class]
63
+ return predicted_class, confidence
64
+
65
+ def custom_decode_predictions(predictions):
66
+ # Assuming predictions is a 2D array with shape (1, num_classes)
67
+ num_classes = predictions.shape[1]
68
+
69
+ # Assuming predictions contain class probabilities
70
+ confidence_scores = predictions[0]
71
+
72
+ # Get the index of the class with the highest probability
73
+ predicted_class_index = np.argmax(confidence_scores)
74
+
75
+ # Return the class index, confidence score, and probability distribution
76
+ return [
77
+ predicted_class_index,
78
+ confidence_scores[predicted_class_index],
79
+ ]
80
+
81
+ # Load the models
82
+ pper_model_CNN = load_model("../pepper/models/pepper_CNN.h5")
83
+ pper_model_SVM = joblib.load('../pepper/models/pepper_hog_svm_model.pkl')
84
+ # pper_model_resnet = load_model('../pepper/models/pepper_resnet50.h5')
85
+ potato_model_CNN = load_model('../potato/models/potato_CNN.h5')
86
+ potato_model_SVM = joblib.load('../potato/models/potato_hog_svm_model.pkl')
87
+ # potato_model_resnet = load_model('../potato/models/potato_resnet50.h5')
88
+ tomato_model_CNN = load_model('../tomato/models/tomato_CNN.h5')
89
+ tomato_model_SVM = joblib.load('../tomato/models/tomato_hog_svm_model.pkl')
90
+ tomato_model_ANN = load_model('../tomato/models/ANN_tomato.h5')
91
+ # tomato_model_resnet = load_model('../tomato/models/tomato_resnet50.h5')
92
+ Potato_class = 3
93
+ Pepper_class = 2
94
+ Tomato_class = 10
95
+
96
+ # Set the title of the app
97
+ st.title('Plant Disease Detection App')
98
+
99
+ # Create a dropdown menu for the user to choose a plant
100
+ plant = st.selectbox('Choose a plant', ('Pepper', 'Potato', 'Tomato'))
101
+
102
+ # Create a radio button for the user to choose a model
103
+ if plant == 'Pepper':
104
+ no_of_classes=2
105
+ class_name = ['pepper_bacterial_spot', 'pepper_bell_healthy']
106
+ model = st.radio('Choose a model', ('CNN', 'SVM', ))
107
+ if model == 'SVM':
108
+ model_func = pper_model_SVM
109
+ elif model == 'CNN':
110
+ model_func = pper_model_CNN
111
+ # elif model == 'Resnet':
112
+ # model_func = pper_model_resnet
113
+ elif plant == 'Potato':
114
+ class_name=['EarlyBlight','Healthy', 'LateBlight']
115
+ no_of_classes=2
116
+ model = st.radio('Choose a model', ('SVM', 'CNN', ))
117
+ if model == 'SVM':
118
+ model_func =potato_model_SVM
119
+ elif model == 'CNN':
120
+ model_func =potato_model_CNN
121
+ # elif model == 'Resnet':
122
+ # model_func = potato_model_resnet
123
+ elif plant == 'Tomato':
124
+ class_name=['Tomato___Bacterial_spot',
125
+ 'Tomato___Early_blight',
126
+ 'Tomato___Late_blight',
127
+ 'Tomato___Leaf_Mold',
128
+ 'Tomato___Septoria_leaf_spot',
129
+ 'Tomato___Spider_mites Two-spotted_spider_mite',
130
+ 'Tomato___Target_Spot',
131
+ 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
132
+ 'Tomato___Tomato_mosaic_virus',
133
+ 'Tomato___healthy']
134
+
135
+ no_of_classes=10
136
+ model = st.radio('Choose a model', ('SVM','CNN', 'ANN', ))
137
+ if model == 'SVM':
138
+ model_func = tomato_model_SVM
139
+ elif model == 'CNN':
140
+ model_func = tomato_model_CNN
141
+ elif model == 'ANN':
142
+ model_func = tomato_model_ANN
143
+ # elif model== 'Resnet':
144
+ # model_func = tomato_model_resnet
145
+
146
+ # Create a file uploader for the user to upload an image
147
+ uploaded_file = st.file_uploader('Upload an image', type='jpg')
148
+
149
+ # Display the selected model and plant
150
+ st.write('Selected plant:', plant)
151
+ st.write('Selected model:', model)
152
+
153
+ # If an image is uploaded, display it and make a prediction
154
+ if uploaded_file is not None:
155
+ image = Image.open(uploaded_file)
156
+ # st.image(image, caption='Uploaded Image', use_column_width=True)
157
+
158
+ # Make a prediction using the selected model
159
+ prediction_result = predict(model_func, image, model)
160
+ print(no_of_classes)
161
+ # st.write(prediction_result)
162
+ # Display the result based on the model type
163
+ if model == 'SVM':
164
+ st.write(f'Prediction: {prediction_result}')
165
+ else:
166
+ predicted_class, confidence = prediction_result
167
+ st.write(f'Prediction: {class_name[predicted_class]}')
168
+ st.write(f'Confidence: {confidence}')
169
+
pepper/.DS_Store ADDED
Binary file (8.2 kB). View file
 
potato/.DS_Store ADDED
Binary file (8.2 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.1.0
2
+ altair==5.3.0
3
+ astunparse==1.6.3
4
+ attrs==23.2.0
5
+ blinker==1.8.2
6
+ cachetools==5.3.3
7
+ certifi==2024.2.2
8
+ charset-normalizer==3.3.2
9
+ click==8.1.7
10
+ flatbuffers==24.3.25
11
+ gast==0.5.4
12
+ gitdb==4.0.11
13
+ GitPython==3.1.43
14
+ google-auth==2.29.0
15
+ google-auth-oauthlib==1.2.0
16
+ google-pasta==0.2.0
17
+ grpcio==1.64.0
18
+ h5py==3.11.0
19
+ idna==3.7
20
+ imageio==2.34.1
21
+ Jinja2==3.1.4
22
+ joblib==1.4.2
23
+ jsonschema==4.22.0
24
+ jsonschema-specifications==2023.12.1
25
+ keras==3.3.3
26
+ lazy_loader==0.4
27
+ libclang==18.1.1
28
+ Markdown==3.6
29
+ markdown-it-py==3.0.0
30
+ MarkupSafe==2.1.5
31
+ mdurl==0.1.2
32
+ ml-dtypes==0.3.2
33
+ namex==0.0.8
34
+ networkx==3.3
35
+ numpy==1.26.4
36
+ oauthlib==3.2.2
37
+ opt-einsum==3.3.0
38
+ optree==0.11.0
39
+ packaging==24.0
40
+ pandas==2.2.2
41
+ pillow==10.3.0
42
+ protobuf==4.25.3
43
+ pyarrow==16.1.0
44
+ pyasn1==0.6.0
45
+ pyasn1_modules==0.4.0
46
+ pydeck==0.9.1
47
+ Pygments==2.18.0
48
+ python-dateutil==2.9.0.post0
49
+ pytz==2024.1
50
+ referencing==0.35.1
51
+ requests==2.32.2
52
+ requests-oauthlib==2.0.0
53
+ rich==13.7.1
54
+ rpds-py==0.18.1
55
+ rsa==4.9
56
+ scikeras==0.12.0
57
+ scikit-image==0.23.2
58
+ scikit-learn==1.5.0
59
+ scipy==1.13.0
60
+ six==1.16.0
61
+ smmap==5.0.1
62
+ streamlit==1.34.0
63
+ tenacity==8.3.0
64
+ tensorboard==2.16.2
65
+ tensorboard-data-server==0.7.2
66
+ tensorflow==2.16.1
67
+ tensorflow-estimator==2.15.0
68
+ tensorflow-io-gcs-filesystem==0.37.0
69
+ tensorflow-macos==2.15.0
70
+ tensorflow-metal==1.1.0
71
+ termcolor==2.4.0
72
+ threadpoolctl==3.5.0
73
+ tifffile==2024.5.10
74
+ toml==0.10.2
75
+ toolz==0.12.1
76
+ tornado==6.4
77
+ typing_extensions==4.11.0
78
+ tzdata==2024.1
79
+ urllib3==2.2.1
80
+ Werkzeug==3.0.3
81
+ wrapt==1.14.1
tomato/.DS_Store ADDED
Binary file (8.2 kB). View file