-
Notifications
You must be signed in to change notification settings - Fork 4
/
streamlitApp.py
207 lines (180 loc) · 10 KB
/
streamlitApp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import streamlit as st
from PIL import Image
import io
import cv2
from tensorflow.keras.models import load_model
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from fastai import *
from fastai.vision import *
from fastai.imports import *
from fastai.vision.all import *
import cv2
import pathlib
plt = platform.system()
if plt == "Windows":
pathlib.PosixPath = pathlib.WindowsPath
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)
# Set up the page layout
st.set_page_config(page_title="ChromaticScan", page_icon=":camera:")
st.title("ChromaticScan")
st.caption(
"A ResNet 34-based Algorithm for Robust Plant Disease Detection with 99.2% Accuracy Across 39 Different Classes of Plant Leaf Images."
)
st.write("Try clicking a leaf image and watch how an AI Model will detect its disease.")
with st.sidebar:
img = Image.open("./Images/leaf.png")
st.image(img)
st.subheader("About ChromaticScan")
st.write(
"ChromaticScan is a state-of-the-art convolutional neural network (CNN) algorithm that is specifically designed for detecting plant diseases. It utilizes transfer learning by fine-tuning the ResNet 34 model on a large dataset of leaf images to achieve an impressive 99.2% accuracy in detecting various plant diseases. The algorithm is trained to identify specific patterns and features in the leaf images that are indicative of different types of diseases, such as leaf spots, blights, and wilts."
)
st.write(
"ChromaticScan is designed to be highly robust and accurate, with the ability to detect plant diseases in a wide range of conditions and environments. It can be used to quickly and accurately diagnose plant diseases, allowing farmers and gardeners to take immediate action to prevent the spread of the disease and minimize crop losses. With its high level of accuracy and ease of use, ChromaticScan is poised to revolutionize the way plant diseases are detected and managed in the agricultural industry."
)
st.write(
"The application will infer the one label out of 39 labels, as follows: 'Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust', 'Apple___healthy', 'Background_without_leaves', 'Blueberry___healthy', 'Cherry___healthy', 'Corn___Cercospora_leaf_spot Gray_leaf_spot', 'Corn___Common_rust', 'Corn___Northern_Leaf_Blight', 'Corn___healthy', 'Grape___Black_rot', 'Grape___Esca_(Black_Measles)', 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', 'Grape___healthy', 'Orange___Haunglongbing_(Citrus_greening)', 'Peach___Bacterial_spot', 'Peach___healthy', 'Pepper,_bell___Bacterial_spot', 'Pepper,_bell___healthy', 'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy', 'Raspberry___healthy', 'Soybean___healthy', 'Squash___Powdery_mildew', 'Strawberry___Leaf_scorch', 'Strawberry___healthy', 'Tomato___Bacterial_spot', 'Tomato___Early_blight', 'Tomato___Late_blight', 'Tomato___Leaf_Mold', 'Tomato___Septoria_leaf_spot', 'Tomato___Spider_mites Two-spotted_spider_mite', 'Tomato___Target_Spot', 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 'Tomato___Tomato_mosaic_virus', 'Tomato___healthy'."
)
classes = [
"Apple___Apple_scab",
"Apple___Black_rot",
"Apple___Cedar_apple_rust",
"Apple___healthy",
"Blueberry___healthy",
"Cherry_(including_sour)___Powdery_mildew",
"Cherry_(including_sour)___healthy",
"Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot",
"Corn_(maize)___Common_rust_",
"Corn_(maize)___Northern_Leaf_Blight",
"Corn_(maize)___healthy",
"Grape___Black_rot",
"Grape___Esca_(Black_Measles)",
"Grape___Leaf_blight_(Isariopsis_Leaf_Spot)",
"Grape___healthy",
"Orange___Haunglongbing_(Citrus_greening)",
"Peach___Bacterial_spot",
"Peach___healthy",
"Pepper,_bell___Bacterial_spot",
"Pepper,_bell___healthy",
"Potato___Early_blight",
"Potato___Late_blight",
"Potato___healthy",
"Raspberry___healthy",
"Soybean___healthy",
"Squash___Powdery_mildew",
"Strawberry___Leaf_scorch",
"Strawberry___healthy",
"Tomato___Bacterial_spot",
"Tomato___Early_blight",
"Tomato___Late_blight",
"Tomato___Leaf_Mold",
"Tomato___Septoria_leaf_spot",
"Tomato___Spider_mites Two-spotted_spider_mite",
"Tomato___Target_Spot",
"Tomato___Tomato_Yellow_Leaf_Curl_Virus",
"Tomato___Tomato_mosaic_virus",
"Tomato___healthy",
"Background_without_leaves",
]
classes_and_descriptions = {
"Apple___Apple_scab": "Apple with Apple scab disease detected.",
"Apple___Black_rot": "Apple with Black rot disease detected",
"Apple___Cedar_apple_rust": "Apple with Cedar apple rust disease detected.",
"Apple___healthy": "Healthy apple leaf detected.",
"Blueberry___healthy": "Healthy blueberry leaf detected.",
"Cherry_(including_sour)___Powdery_mildew": "Cherry with Powdery mildew disease detected.",
"Cherry_(including_sour)___healthy": "Healthy cherry leaf detected.",
"Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot": "Corn with Cercospora leaf spot or Gray leaf spot disease detected.",
"Corn_(maize)___Common_rust_": "Corn with Common rust disease detected.",
"Corn_(maize)___Northern_Leaf_Blight": "Corn with Northern Leaf Blight disease detected.",
"Corn_(maize)___healthy": "Healthy corn leaf detected.",
"Grape___Black_rot": "Grape with Black rot disease detected.",
"Grape___Esca_(Black_Measles)": "Grape with Esca (Black Measles) disease detected.",
"Grape___Leaf_blight_(Isariopsis_Leaf_Spot)": "Grape with Leaf blight (Isariopsis Leaf Spot) disease detected.",
"Grape___healthy": "Healthy grape leaf detected.",
"Orange___Haunglongbing_(Citrus_greening)": "Orange with Haunglongbing (Citrus greening) disease detected.",
"Peach___Bacterial_spot": "Peach with Bacterial spot disease detected.",
"Peach___healthy": "Healthy peach leaf detected.",
"Pepper,_bell___Bacterial_spot": "Bell pepper with Bacterial spot disease detected.",
"Pepper,_bell___healthy": "Healthy bell pepper leaf detected.",
"Potato___Early_blight": "Potato with Early blight disease detected.",
"Potato___Late_blight": "Potato with Late blight disease detected.",
"Potato___healthy": "Healthy potato leaf detected",
"Raspberry___healthy": "Healthy raspberry leaf detected",
"Soybean___healthy": "Healthy soybean leaf detected.",
"Squash___Powdery_mildew": "Squash with Powdery mildew disease detected.",
"Strawberry___Leaf_scorch": "Strawberry with Leaf scorch disease detected.",
"Strawberry___healthy": "Healthy strawberry leaf detected.",
"Tomato___Bacterial_spot": "Tomato leaf with Bacterial spot disease detected.",
"Tomato___Early_blight": "Tomato leaf with Early blight disease detected.",
"Tomato___Late_blight": "Tomato leaf with Late blight disease detected.",
"Tomato___Leaf_Mold": "Tomato leaf with Leaf Mold disease detected.",
"Tomato___Septoria_leaf_spot": "Tomato leaf with Septoria leaf spot disease detected.",
"Tomato___Spider_mites Two-spotted_spider_mite": "Tomato leaf with Spider mites or Two-spotted spider mite disease detected.",
"Tomato___Target_Spot": "Tomato leaf with Target Spot disease detected.",
"Tomato___Tomato_Yellow_Leaf_Curl_Virus": "Tomato leaf with Tomato Yellow Leaf Curl Virus disease detected.",
"Tomato___Tomato_mosaic_virus": "Tomato leaf with Tomato mosaic virus disease detected.",
"Tomato___healthy": "Healthy tomato leaf.",
"Background_without_leaves": "No plant leaf detected in the image.",
}
# Define the functions to load images
def load_uploaded_image(file):
file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
opencv_image = cv2.imdecode(file_bytes, 1)
return opencv_image
# Set up the sidebar
st.subheader("Select Image Input Method")
input_method = st.radio(
"options", ["File Uploader", "Camera Input"], label_visibility="collapsed"
)
# Check which input method was selected
if input_method == "File Uploader":
uploaded_file = st.file_uploader(
"Choose an image file", type=["jpg", "jpeg", "png"]
)
if uploaded_file is not None:
uploaded_file_img = load_uploaded_image(uploaded_file)
st.image(uploaded_file_img, caption="Uploaded Image", width=300)
st.success("Image uploaded successfully!")
else:
st.warning("Please upload an image file.")
elif input_method == "Camera Input":
st.warning("Please allow access to your camera.")
camera_image_file = st.camera_input("Click an Image")
if camera_image_file is not None:
camera_file_img = load_uploaded_image(camera_image_file)
st.image(camera_file_img, caption="Camera Input Image", width=300)
st.success("Image clicked successfully!")
else:
st.warning("Please click an image.")
# model file path
export_file_path = "./models/export.pkl"
def Plant_Disease_Detection(_img_file_path):
model = load_learner(export_file_path, "export.pkl")
prediction = model.predict(img_file_path)[0]
if prediction not in classes:
prediction_sentence = f"The uploaded image is {prediction}, which is not compatible with the application. Please upload an image of a plant leaf for disease detection."
return prediction_sentence
prediction_sentence = classes_and_descriptions[prediction]
return prediction_sentence
submit = st.button(label="Submit Leaf Image")
if submit:
st.subheader("Output")
if input_method == "File Uploader":
img_file_path = uploaded_file_img
elif input_method == "Camera Input":
img_file_path = camera_file_img
prediction = Plant_Disease_Detection(img_file_path)
with st.spinner(text="This may take a moment..."):
st.write(prediction)
footer = """
<div style="text-align: center; font-size: medium; margin-top:50px;">
If you find ChromaticScan useful or interesting, please consider starring it on GitHub.
<hr>
<a href="https://github.com/SaiJeevanPuchakayala/ChromaticScan" target="_blank">
<img src="https://img.shields.io/github/stars/SaiJeevanPuchakayala/ChromaticScan.svg?style=social" alt="GitHub stars">
</a>
</div>
"""
st.markdown(footer, unsafe_allow_html=True)