Dokkone commited on
Commit
c5f86e6
Β·
verified Β·
1 Parent(s): af20e68

Update pages/model_1.py

Browse files
Files changed (1) hide show
  1. pages/model_1.py +27 -70
pages/model_1.py CHANGED
@@ -1,30 +1,23 @@
1
- # Trained on 80 epochs at Batch size 256 with Learning Rate of 0.001 under 50 seconds!!
2
- from PIL import Image, ImageOps
3
- import numpy as np
4
- import pandas as pd
5
  import streamlit as st
6
  import tensorflow as tf
 
 
 
7
 
8
  # Suppress warnings
9
  import warnings
10
  warnings.filterwarnings('ignore', category=UserWarning)
11
  warnings.filterwarnings('ignore', category=FutureWarning)
12
 
13
- # Disable scientific notation for clarity
14
- np.set_printoptions(suppress=True)
15
-
16
- def load_model_teachable():
17
- # Load the model
18
- model = tf.keras.models.load_model("pages/cnn_model.h5")
19
  return model
20
 
 
21
  with st.spinner('Model is being loaded..'):
22
- model = load_model_teachable()
23
 
24
- st.write("""
25
- # Teachable Machine Model
26
- """
27
- )
28
 
29
  st.sidebar.info("You should be happy if it classifies as - Healthy plant πŸ™‚")
30
 
@@ -32,30 +25,17 @@ file = st.file_uploader("Upload the image to be classified", type=["jpg", "png"]
32
  st.set_option('deprecation.showfileUploaderEncoding', False)
33
 
34
 
35
- def upload_predict_teachable(image, model, class_names):
36
-
37
- # Create the array of the right shape to feed into the keras model
38
- # The 'length' or number of images you can put into the array is
39
- # determined by the first position in the shape tuple, in this case 1
40
- data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
41
-
42
- # Replace this with the path to your image
43
- image_RGB = image.convert("RGB")
44
-
45
- # resizing the image to be at least 224x224 and then cropping from the center
46
- image_resized = image_RGB.resize((224, 224), resample=Image.LANCZOS)
47
-
48
- # turn the image into a numpy array
49
- image_array = np.asarray(image_resized)
50
 
51
- # Normalize the image
52
- normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
53
 
54
- # Load the image into the array
55
- data[0] = normalized_image_array
56
-
57
- # Predicts the model
58
- prediction = model.predict(data)
59
  return prediction
60
 
61
 
@@ -65,39 +45,16 @@ else:
65
  image = Image.open(file)
66
  st.image(image, use_column_width=True)
67
 
68
- # Load the labels
69
- class_names = open("pages/labels.txt", "r").readlines()
70
- prediction = upload_predict_teachable(image, model,class_names)
71
- index = np.argmax(prediction)
72
- class_name = class_names[index]
73
- confidence_score = prediction[0][index]
 
74
 
75
- # Print prediction and confidence score
76
- result = f"Your plant is suffering from: {class_name[2:]}"
77
  result_score = "βœ… Accurate prediction score is: {} / 100".format("%.2f" % confidence_score)
78
  st.success(result)
79
- st.info(result_score)
80
-
81
-
82
- st.markdown("""
83
- #### Know more about this disease and suggestions to prevent them from spreading.
84
-
85
- - *Just copy the disease name (result) from above & paste it in the below input box as it is!*
86
- """)
87
-
88
- # Loading diseases info & management strategies
89
- df = pd.read_csv('pages/Disease_solutions.csv')
90
- disease = st.text_input('Enter the disease name below πŸ‘‡', '')
91
-
92
- # Selecting rows based on condition
93
- about = df.loc[df['Diseases'] == disease, ['Type', 'Description']]
94
- strategies = df.loc[df['Diseases'] == disease, 'Management Strategies']
95
-
96
- # Print diseases' other info
97
- tab1, tab2 = st.tabs(["πŸ‘€ Description", "⭐ Solution"])
98
-
99
- with tab1:
100
- st.dataframe(about, use_container_width=True)
101
-
102
- with tab2:
103
- st.dataframe(strategies, use_container_width=True)
 
 
 
 
 
1
  import streamlit as st
2
  import tensorflow as tf
3
+ import cv2
4
+ from PIL import Image, ImageOps
5
+ import numpy as np
6
 
7
  # Suppress warnings
8
  import warnings
9
  warnings.filterwarnings('ignore', category=UserWarning)
10
  warnings.filterwarnings('ignore', category=FutureWarning)
11
 
12
+ def load_model():
13
+ model = tf.keras.models.load_model('pages/cnn_model.h5')
 
 
 
 
14
  return model
15
 
16
+
17
  with st.spinner('Model is being loaded..'):
18
+ model = load_model()
19
 
20
+ st.write("# ResNet50 Model")
 
 
 
21
 
22
  st.sidebar.info("You should be happy if it classifies as - Healthy plant πŸ™‚")
23
 
 
25
  st.set_option('deprecation.showfileUploaderEncoding', False)
26
 
27
 
28
+ def upload_predict(upload_image, model):
29
+ size = (180, 180)
30
+ image = ImageOps.fit(upload_image, size, Image.LANCZOS)
31
+ image = np.asarray(image)
32
+ img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
33
+ img_resize = cv2.resize(img, dsize=(224, 224), interpolation=cv2.INTER_CUBIC)
 
 
 
 
 
 
 
 
 
34
 
35
+ img_reshape = img_resize[np.newaxis, ...]
 
36
 
37
+ prediction = model.predict(img_reshape)
38
+ print(prediction)
 
 
 
39
  return prediction
40
 
41
 
 
45
  image = Image.open(file)
46
  st.image(image, use_column_width=True)
47
 
48
+ prediction = upload_predict(image, model)
49
+ predicted_result = np.argmax(prediction)
50
+
51
+ classfiers = ['bacterial_leaf_blight','bacterial_leaf_streak','bakanae','brown_spot','grassy_stunt_virus','healthy_rice_plant',
52
+ 'narrow_brown_spot','ragged_stunt_virus','rice_blast','rice_false_smut','sheath_blight','sheath_rot','stem_rot','tungro_virus']
53
+ image_class = classfiers[predicted_result]
54
+ confidence_score = prediction[0][predicted_result]
55
 
56
+ # Results
57
+ result = f"Your plant is suffering from: *{image_class}*"
58
  result_score = "βœ… Accurate prediction score is: {} / 100".format("%.2f" % confidence_score)
59
  st.success(result)
60
+ st.info(result_score)