Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +67 -26
  3. requirements.txt +1 -1
.gitignore CHANGED
@@ -1 +1,3 @@
1
  *.jpg
 
 
 
1
  *.jpg
2
+ LoackerVenv/
3
+ __pycache__/
app.py CHANGED
@@ -37,6 +37,24 @@ Please note that this application is intended to provide an estimate only and ma
37
  image = st.file_uploader(label="Upload your image here",
38
  type=['png', 'jpg', 'jpeg'])
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  def fat_choco_rateo(image, input_image, threshold):
42
 
@@ -81,10 +99,16 @@ def fat_choco_rateo(image, input_image, threshold):
81
  st.image(input_image, caption="Original", use_column_width=True)
82
 
83
  with col2:
 
84
  st.image(img, caption="Detected Tortina", use_column_width=True)
85
 
86
  with col3:
87
- st.image(predicted, caption="Predicted bloom zone",
 
 
 
 
 
88
  use_column_width=True)
89
 
90
  with col4:
@@ -101,25 +125,28 @@ def fat_choco_rateo(image, input_image, threshold):
101
  hist_image = cv2.resize(hist_image, (600, 600))
102
  st.image(hist_image, caption="Histogram", use_column_width=True)
103
 
104
- st.write("Percentuale fat")
105
- st.write(int(len(fat) / len(hist_analisy) * 100))
106
- st.write("Percentuale choco")
107
- st.write(int(len(choco) / len(hist_analisy) * 100))
108
 
109
 
110
- fat = (len(fat) / len(hist_analisy))
111
 
112
- #Define class appartenence
113
- if( fat == 0):
114
- st.write("Class 0 ")
115
- if( fat > 0 and fat < 0.25):
116
- st.write("Class 1 ")
117
- if( fat >= 0.25 and fat < 0.50):
118
- st.write("Class 2 ")
119
- if( fat >= 50 and fat < 0.75):
120
- st.write("Class 3 ")
121
- if( fat >= 75 ):
122
- st.write("Class 4 ")
 
 
 
123
 
124
 
125
 
@@ -146,7 +173,7 @@ def auto_encoder_prediction(image, thresh):
146
 
147
  thresholded_anomaly_maps = (
148
  anomaly_maps_np > thresh).astype(np.uint8).max(-1)
149
- postprocessed_anomaly_maps = np.zeros_like(thresholded_anomaly_maps)
150
 
151
  postprocessed_anomaly_maps = postprocessing(
152
  gray, thresholded_anomaly_maps[0])
@@ -160,7 +187,7 @@ def auto_encoder_prediction(image, thresh):
160
 
161
 
162
  # Display results in Streamlit
163
- col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
164
 
165
  with col1:
166
  st.image(img, caption="Original", use_column_width=True)
@@ -169,15 +196,24 @@ def auto_encoder_prediction(image, thresh):
169
  detected[postprocessed_anomaly_maps == 0] = 0
170
  st.image(detected, caption="Detected Tortina", use_column_width=True)
171
  with col3:
172
- predicted = np.zeros(postprocessed_anomaly_maps.shape, dtype=np.uint8)
173
- predicted[postprocessed_anomaly_maps == 2] = 255
174
- st.image(predicted, caption="Predicted bloom zone",use_column_width=True)
 
175
  with col4:
176
- threshold = 0.03
 
 
 
 
 
 
 
 
177
  hist_data = np.ravel(anomaly_maps_np[0][postprocessed_anomaly_maps != 0])
178
  fig, ax = plt.subplots(figsize=(6, 4))
179
  ax.hist(hist_data, 256//10, (0, hist_data.max()))
180
- ax.axvline(x=threshold, color='red')
181
  ax.set_title("Histogram")
182
  # Render plot as image
183
  canvas = FigureCanvasAgg(fig)
@@ -189,8 +225,13 @@ def auto_encoder_prediction(image, thresh):
189
 
190
 
191
  #st.image(rgb_output, caption="rgb out", use_column_width=True)
192
- st.write("Final class prediction: ", final_predictions)
 
 
193
 
 
 
 
194
 
195
  if image is not None:
196
 
@@ -206,7 +247,7 @@ if image is not None:
206
 
207
  st.title('Deep Learning Approach')
208
  threshold2 = st.slider(
209
- 'Threshold (0.33 is the optimal value evalueted by the model)', 0.001, 1.000, 0.033)
210
  auto_encoder_prediction("img.jpg", threshold2)
211
 
212
 
 
37
  image = st.file_uploader(label="Upload your image here",
38
  type=['png', 'jpg', 'jpeg'])
39
 
40
+ def write_area_prediction(fat_ratio):
41
+ fat_percentage = round(fat_ratio * 100, ndigits=2)
42
+ choco_percentage = 100 - fat_percentage
43
+ st.write("Area Covered by Fat (%):", fat_percentage)
44
+ st.write("Area Covered by Chocolate (%):", choco_percentage)
45
+
46
+ #Define class appartenence
47
+ if( fat_ratio == 0):
48
+ st.write("Predicted Area Class:", 0)
49
+ if( fat_ratio > 0 and fat_ratio < 0.25):
50
+ st.write("Predicted Area Class:", 1)
51
+ if( fat_ratio >= 0.25 and fat_ratio < 0.50):
52
+ st.write("Predicted Area Class:", 2)
53
+ if( fat_ratio >= 0.5 and fat_ratio < 0.75):
54
+ st.write("Predicted Area Class:", 3)
55
+ if( fat_ratio >= 0.75 ):
56
+ st.write("Predicted Area Class:", 4)
57
+
58
 
59
  def fat_choco_rateo(image, input_image, threshold):
60
 
 
99
  st.image(input_image, caption="Original", use_column_width=True)
100
 
101
  with col2:
102
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
103
  st.image(img, caption="Detected Tortina", use_column_width=True)
104
 
105
  with col3:
106
+ predicted = predicted.min(-1) #take minimum of thresholded channels
107
+ overlay = img
108
+ #overlay = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
109
+ #overlay[:,:,-1] = 200 #make half transparent
110
+ overlay[predicted == 255] = np.array([0, 0, 255])
111
+ st.image(overlay, caption="Prediction",
112
  use_column_width=True)
113
 
114
  with col4:
 
125
  hist_image = cv2.resize(hist_image, (600, 600))
126
  st.image(hist_image, caption="Histogram", use_column_width=True)
127
 
128
+ # st.write("Percentuale fat")
129
+ # st.write(int(len(fat) / len(hist_analisy) * 100))
130
+ # st.write("Percentuale choco")
131
+ # st.write(int(len(choco) / len(hist_analisy) * 100))
132
 
133
 
134
+ # fat = (len(fat) / len(hist_analisy))
135
 
136
+ # #Define class appartenence
137
+ # if( fat == 0):
138
+ # st.write("Class 0 ")
139
+ # if( fat > 0 and fat < 0.25):
140
+ # st.write("Class 1 ")
141
+ # if( fat >= 0.25 and fat < 0.50):
142
+ # st.write("Class 2 ")
143
+ # if( fat >= 50 and fat < 0.75):
144
+ # st.write("Class 3 ")
145
+ # if( fat >= 75 ):
146
+ # st.write("Class 4 ")
147
+
148
+ fat_ratio = (len(fat) / len(hist_analisy))
149
+ write_area_prediction(fat_ratio)
150
 
151
 
152
 
 
173
 
174
  thresholded_anomaly_maps = (
175
  anomaly_maps_np > thresh).astype(np.uint8).max(-1)
176
+ #postprocessed_anomaly_maps = np.zeros_like(thresholded_anomaly_maps)
177
 
178
  postprocessed_anomaly_maps = postprocessing(
179
  gray, thresholded_anomaly_maps[0])
 
187
 
188
 
189
  # Display results in Streamlit
190
+ col1, col2, col3, col4, col5 = st.columns([1, 1, 1, 1, 1])
191
 
192
  with col1:
193
  st.image(img, caption="Original", use_column_width=True)
 
196
  detected[postprocessed_anomaly_maps == 0] = 0
197
  st.image(detected, caption="Detected Tortina", use_column_width=True)
198
  with col3:
199
+ predicted = detected#np.zeros(img[0].shape, dtype=np.uint8)
200
+ predicted[(thresholded_anomaly_maps[0] == 1) & (detected.sum(-1) != 0)] = np.array([0, 0, 255])
201
+ st.image(predicted, caption="Prediction",use_column_width=True)
202
+
203
  with col4:
204
+ # predicted = np.zeros(postprocessed_anomaly_maps.shape, dtype=np.uint8)
205
+ # predicted[postprocessed_anomaly_maps == 2] = 255
206
+ # st.image(predicted, caption="Predicted bloom zone",use_column_width=True)
207
+ predicted = np.zeros(img[0].shape, dtype=np.uint8)
208
+ predicted[postprocessed_anomaly_maps == 1] = img[0][postprocessed_anomaly_maps == 1]
209
+ predicted[postprocessed_anomaly_maps == 2] = np.array([0, 0, 255])
210
+ st.image(predicted, caption="Post-processed Prediction",use_column_width=True)
211
+ with col5:
212
+ #threshold = 0.03
213
  hist_data = np.ravel(anomaly_maps_np[0][postprocessed_anomaly_maps != 0])
214
  fig, ax = plt.subplots(figsize=(6, 4))
215
  ax.hist(hist_data, 256//10, (0, hist_data.max()))
216
+ ax.axvline(x=thresh, color='red')
217
  ax.set_title("Histogram")
218
  # Render plot as image
219
  canvas = FigureCanvasAgg(fig)
 
225
 
226
 
227
  #st.image(rgb_output, caption="rgb out", use_column_width=True)
228
+ #st.write("Final class prediction: ", final_predictions)
229
+ fat_pixels = (postprocessed_anomaly_maps == 2).sum()
230
+ choco_pixels = (postprocessed_anomaly_maps == 1).sum()
231
 
232
+ fat_ratio = (fat_pixels) / (fat_pixels + choco_pixels)
233
+
234
+ write_area_prediction(fat_ratio)
235
 
236
  if image is not None:
237
 
 
247
 
248
  st.title('Deep Learning Approach')
249
  threshold2 = st.slider(
250
+ 'Threshold (0.033 is the optimal value evalueted by the model)', 0.001, 1.000, 0.033)
251
  auto_encoder_prediction("img.jpg", threshold2)
252
 
253
 
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
  altair==4.2.2
2
  attrs==22.2.0
3
- backports.zoneinfo==0.2.1
4
  blinker==1.5
5
  brotlipy==0.7.0
6
  cachetools==5.3.0
 
1
  altair==4.2.2
2
  attrs==22.2.0
3
+ backports.zoneinfo==0.2.1;python_version<"3.9"
4
  blinker==1.5
5
  brotlipy==0.7.0
6
  cachetools==5.3.0