nnibras commited on
Commit
16787a7
·
verified ·
1 Parent(s): 52a6248

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -68
app.py CHANGED
@@ -1,5 +1,10 @@
 
 
 
 
 
 
1
  import cv2
2
- import glob
3
  import gradio as gr
4
  import numpy as np
5
  import matplotlib.pyplot as plt
@@ -11,13 +16,19 @@ import pandas as pd
11
  from sklearn.model_selection import GridSearchCV
12
  from sklearn.neighbors import KNeighborsClassifier
13
  from skimage.feature import local_binary_pattern
 
 
 
 
 
14
 
15
  # Visualize GLCM features for grass and wood
16
  def plot_features(features, title):
17
  plt.figure(figsize=(10, 6))
18
  for i, feature in enumerate(features.T): # Transpose to plot feature by feature
19
- plt.plot(feature)
20
  plt.title(title)
 
21
  plt.show()
22
 
23
  # Define directories for grass and wood images
@@ -27,67 +38,76 @@ wood_dir = "images/Wood/Train_Wood"
27
  # Constants for LBP
28
  RADIUS = 1
29
  N_POINTS = 12 * RADIUS
30
- TARGET_SIZE = (30, 30)
31
 
32
  # for GLCM
33
  distances = [1]
34
  angles = [0]
35
 
36
- # Use glob to load images from the directory
37
  def load_and_convert_images(directory):
38
- """Load images from a directory using glob and convert them to grayscale."""
39
  dataset = []
40
- image_paths = glob.glob(f"{directory}/*.jpg") + glob.glob(f"{directory}/*.png") + glob.glob(f"{directory}/*.jpeg")
41
-
42
- for img_path in image_paths:
43
- img = cv2.imread(img_path) # Read the image
44
- if img is not None:
45
- gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
46
- resized_image = cv2.resize(gray_image, TARGET_SIZE, interpolation=cv2.INTER_AREA)
47
- dataset.append(resized_image)
48
- else:
49
- print(f"Warning: Failed to load image {img_path}")
50
-
51
  return dataset
52
 
 
 
 
 
53
  def calc_glcm_features(images):
54
  features = []
55
  for img in images:
56
  glcm = graycomatrix(img, distances, angles, symmetric=True, normed=True)
57
 
58
- contrast = graycoprops(glcm, "contrast")[0, 0]
59
- dissimilarity = graycoprops(glcm, "dissimilarity")[0, 0]
60
- homogeneity = graycoprops(glcm, "homogeneity")[0, 0]
61
- energy = graycoprops(glcm, "energy")[0, 0]
62
- correlation = graycoprops(glcm, "correlation")[0, 0]
63
 
64
- features.append([contrast, dissimilarity, homogeneity, energy, correlation])
65
  return features
66
 
 
67
  def extract_lbp_features(images):
68
- """Extract LBP features from a list of images."""
69
- lbp_features = []
70
- for image in images:
 
71
  lbp = local_binary_pattern(image, N_POINTS, RADIUS, method="uniform")
72
  n_bins = int(lbp.max() + 1)
73
  lbp_hist, _ = np.histogram(lbp, bins=n_bins, range=(0, n_bins), density=True)
74
  lbp_features.append(lbp_hist)
75
- return lbp_features
 
76
 
77
  # Load datasets
78
  grass_dataset = load_and_convert_images(grass_dir)
79
- wood_dataset = load_and_convert_images(wood_dir)
 
 
 
 
80
 
81
- # Create labels (0 for grass, 1 for wood)
82
- grass_labels = [0] * len(grass_dataset)
83
- wood_labels = [1] * len(wood_dataset)
84
 
85
  # Calculate features
86
  grass_glcm_features = calc_glcm_features(grass_dataset)
87
- wood_glcm_features = calc_glcm_features(wood_dataset)
 
 
88
 
89
  grass_lbp_features = extract_lbp_features(grass_dataset)
90
- wood_lbp_features = extract_lbp_features(wood_dataset)
 
 
91
 
92
  # GLCM features for grass and wood
93
  plot_features(np.array(grass_glcm_features), "GLCM Features for Grass Images")
@@ -95,24 +115,36 @@ plot_features(np.array(wood_glcm_features), "GLCM Features for Wood Images")
95
 
96
  # LBP features for grass and wood
97
  plot_features(np.array(grass_lbp_features), "LBP Features for Grass Images")
98
- plot_features(np.array(wood_lbp_features), "LBP Features for Wood Images")
 
 
 
99
 
100
  # Combine labels and features for GLCM classifier
101
  glcm_features = grass_glcm_features + wood_glcm_features
102
- glcm_labels = grass_labels + wood_labels
103
 
104
  # Convert to numpy array
105
  glcm_features = np.array(glcm_features)
106
 
 
 
 
107
  # Prepare labels and features for LBP classifier
108
  lbp_features = grass_lbp_features + wood_lbp_features
109
- lbp_labels = grass_labels + wood_labels
110
 
111
  # Convert to numpy array
112
  lbp_features = np.array(lbp_features)
113
 
 
 
 
 
 
 
114
  num_grass = len(grass_dataset) # Number of grass images
115
- num_wood = len(wood_dataset) # Number of wood images
116
 
117
  # Create the labels: 0 for grass, 1 for wood
118
  y = np.array([0] * num_grass + [1] * num_wood)
@@ -126,7 +158,9 @@ glcm_accuracy_list = []
126
  lbp_accuracy_list = []
127
 
128
  # Parameter tuning using GridSearchCV for KNN classifier
129
- param_grid = {"n_neighbors": [3, 5, 7], "p": [1, 2]}
 
 
130
 
131
  # GLCM Classifier Training and Evaluation
132
  glcm_knn = KNeighborsClassifier()
@@ -139,15 +173,18 @@ print("Best parameters for GLCM KNN:", glcm_grid_search.best_params_)
139
  for train_index, test_index in kf.split(glcm_features):
140
  x_train, x_test = glcm_features[train_index], glcm_features[test_index]
141
  y_train, y_test = y[train_index], y[test_index]
142
-
143
- glcm_classifier = KNeighborsClassifier(
144
- n_neighbors=glcm_grid_search.best_params_["n_neighbors"]
145
- )
146
  glcm_classifier.fit(x_train, y_train)
147
  y_pred = glcm_classifier.predict(x_test)
148
-
149
  accuracy = accuracy_score(y_test, y_pred)
150
  glcm_accuracy_list.append(accuracy)
 
 
 
 
 
151
 
152
  # Print overall GLCM accuracy
153
  print(f"GLCM Cross-validated accuracy: {np.mean(glcm_accuracy_list) * 100:.2f}%")
@@ -163,51 +200,113 @@ print("Best parameters for LBP KNN:", lbp_grid_search.best_params_)
163
  for train_index, test_index in kf.split(lbp_features):
164
  x_train, x_test = lbp_features[train_index], lbp_features[test_index]
165
  y_train, y_test = y[train_index], y[test_index]
166
-
167
- lbp_classifier = KNeighborsClassifier(
168
- n_neighbors=lbp_grid_search.best_params_["n_neighbors"],
169
- p=lbp_grid_search.best_params_["p"]
170
- )
171
  lbp_classifier.fit(x_train, y_train)
172
  y_pred = lbp_classifier.predict(x_test)
173
-
174
  accuracy = accuracy_score(y_test, y_pred)
175
  lbp_accuracy_list.append(accuracy)
 
 
 
 
 
176
 
177
  # Print overall LBP accuracy
178
  print(f"LBP Cross-validated accuracy: {np.mean(lbp_accuracy_list) * 100:.2f}%")
179
 
180
- # Preprocess uploaded image for classification
181
- def preprocess_image(image):
182
- gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert to grayscale
183
- resized_image = cv2.resize(gray_image, TARGET_SIZE, interpolation=cv2.INTER_AREA) # Resize to match training size
184
- return resized_image
185
 
186
- # Define the Gradio interface
187
- def classify_uploaded_image(image, algorithm):
188
- image = preprocess_image(np.array(image, dtype=np.uint8)) # Preprocess uploaded image
189
-
190
- if algorithm == "GLCM":
191
- features = calc_glcm_features([image]) # Use parentheses, pass as a list
192
- prediction = glcm_classifier.predict(features)
193
- elif algorithm == "LBP":
194
- features = extract_lbp_features([image])
195
- prediction = lbp_classifier.predict(features)
 
 
 
 
 
196
  else:
197
  raise ValueError(f"Algorithm '{algorithm}' is not recognized.")
198
 
199
  return "Grass" if prediction[0] == 0 else "Wood"
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  # Gradio Interface Setup
202
  iface = gr.Interface(
203
- fn=classify_uploaded_image,
204
  inputs=[
205
- gr.Image(type="numpy", label="Upload an Image"),
206
- gr.Dropdown(choices=["GLCM", "LBP"], label="Algorithm"),
207
- ],
208
  outputs="text",
209
- title="Texture Classification",
210
  )
211
 
212
  # Launch the interface
213
  iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[55]:
5
+
6
+
7
  import cv2
 
8
  import gradio as gr
9
  import numpy as np
10
  import matplotlib.pyplot as plt
 
16
  from sklearn.model_selection import GridSearchCV
17
  from sklearn.neighbors import KNeighborsClassifier
18
  from skimage.feature import local_binary_pattern
19
+ import os
20
+
21
+
22
+ # In[56]:
23
+
24
 
25
  # Visualize GLCM features for grass and wood
26
  def plot_features(features, title):
27
  plt.figure(figsize=(10, 6))
28
  for i, feature in enumerate(features.T): # Transpose to plot feature by feature
29
+ plt.plot(feature, label=f'Feature {i+1}')
30
  plt.title(title)
31
+ plt.legend()
32
  plt.show()
33
 
34
  # Define directories for grass and wood images
 
38
  # Constants for LBP
39
  RADIUS = 1
40
  N_POINTS = 12 * RADIUS
41
+ TARGET_SIZE = (30,30)
42
 
43
  # for GLCM
44
  distances = [1]
45
  angles = [0]
46
 
 
47
  def load_and_convert_images(directory):
48
+ """Load images from a directory and convert them to grayscale."""
49
  dataset = []
50
+ for filename in os.listdir(directory):
51
+ if filename.endswith(('.jpg', '.png', '.jpeg')):
52
+ img_path = os.path.join(directory, filename) # Construct full image path
53
+ img = cv2.imread(img_path) # Read the image
54
+ if img is not None:
55
+ # resized_image = cv2.resize(img, TARGET_SIZE, interpolation=cv2.INTER_AREA) #resize the images
56
+ gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
57
+ resized_image = cv2.resize(gray_image, TARGET_SIZE, interpolation=cv2.INTER_AREA)
58
+ dataset.append(resized_image)
 
 
59
  return dataset
60
 
61
+
62
+ # In[57]:
63
+
64
+
65
  def calc_glcm_features(images):
66
  features = []
67
  for img in images:
68
  glcm = graycomatrix(img, distances, angles, symmetric=True, normed=True)
69
 
70
+ contrast = graycoprops(glcm, 'contrast')[0,0]
71
+ dissimilarity = graycoprops(glcm, 'dissimilarity')[0,0]
72
+ homogeneity = graycoprops(glcm, 'homogeneity')[0,0]
73
+ energy = graycoprops(glcm, 'energy')[0,0]
74
+ correlation = graycoprops(glcm, 'correlation')[0,0]
75
 
76
+ features.append([contrast, dissimilarity, homogeneity, energy, correlation])
77
  return features
78
 
79
+ # Function to extract LBP features from an image
80
  def extract_lbp_features(images):
81
+ """Extract LBP features from a list of images."""
82
+
83
+ lbp_features = []
84
+ for image in images:
85
  lbp = local_binary_pattern(image, N_POINTS, RADIUS, method="uniform")
86
  n_bins = int(lbp.max() + 1)
87
  lbp_hist, _ = np.histogram(lbp, bins=n_bins, range=(0, n_bins), density=True)
88
  lbp_features.append(lbp_hist)
89
+ return lbp_features
90
+
91
 
92
  # Load datasets
93
  grass_dataset = load_and_convert_images(grass_dir)
94
+ wood_dataset = load_and_convert_images(wood_dir)
95
+
96
+ #Create labels (0 for grass, 1 for wood)
97
+ grass_labels = [0] * len(grass_dataset) # Label all grass images as 0
98
+ wood_labels = [1] * len(wood_dataset) # Label all wood images as 1
99
 
 
 
 
100
 
101
  # Calculate features
102
  grass_glcm_features = calc_glcm_features(grass_dataset)
103
+ wood_glcm_features = calc_glcm_features(wood_dataset)
104
+
105
+
106
 
107
  grass_lbp_features = extract_lbp_features(grass_dataset)
108
+ wood_lbp_features = extract_lbp_features(wood_dataset)
109
+
110
+ ####TESTING...
111
 
112
  # GLCM features for grass and wood
113
  plot_features(np.array(grass_glcm_features), "GLCM Features for Grass Images")
 
115
 
116
  # LBP features for grass and wood
117
  plot_features(np.array(grass_lbp_features), "LBP Features for Grass Images")
118
+ plot_features(np.array(wood_lbp_features), "LBP Features for Wood Images")
119
+
120
+
121
+ ###MATPLOTLIB....
122
 
123
  # Combine labels and features for GLCM classifier
124
  glcm_features = grass_glcm_features + wood_glcm_features
125
+ glcm_labels = grass_labels + wood_labels
126
 
127
  # Convert to numpy array
128
  glcm_features = np.array(glcm_features)
129
 
130
+
131
+
132
+
133
  # Prepare labels and features for LBP classifier
134
  lbp_features = grass_lbp_features + wood_lbp_features
135
+ lbp_labels = grass_labels + wood_labels
136
 
137
  # Convert to numpy array
138
  lbp_features = np.array(lbp_features)
139
 
140
+ print("block 2 run")
141
+
142
+
143
+ # In[58]:
144
+
145
+
146
  num_grass = len(grass_dataset) # Number of grass images
147
+ num_wood = len(wood_dataset) # Number of wood images
148
 
149
  # Create the labels: 0 for grass, 1 for wood
150
  y = np.array([0] * num_grass + [1] * num_wood)
 
158
  lbp_accuracy_list = []
159
 
160
  # Parameter tuning using GridSearchCV for KNN classifier
161
+ param_grid = {'n_neighbors': [3, 5, 7],
162
+ 'p' : [1,2]
163
+ }
164
 
165
  # GLCM Classifier Training and Evaluation
166
  glcm_knn = KNeighborsClassifier()
 
173
  for train_index, test_index in kf.split(glcm_features):
174
  x_train, x_test = glcm_features[train_index], glcm_features[test_index]
175
  y_train, y_test = y[train_index], y[test_index]
176
+
177
+ glcm_classifier = KNeighborsClassifier(n_neighbors=glcm_grid_search.best_params_['n_neighbors'])
 
 
178
  glcm_classifier.fit(x_train, y_train)
179
  y_pred = glcm_classifier.predict(x_test)
180
+
181
  accuracy = accuracy_score(y_test, y_pred)
182
  glcm_accuracy_list.append(accuracy)
183
+
184
+ # Optionally: Print confusion matrix and precision
185
+
186
+ # print(f"Confusion Matrix for GLCM Fold:\n{confusion_matrix(y_test, y_pred)}")
187
+ # print(f"Classification Report for GLCM Fold:\n{classification_report(y_test, y_pred, zero_division=0)}")
188
 
189
  # Print overall GLCM accuracy
190
  print(f"GLCM Cross-validated accuracy: {np.mean(glcm_accuracy_list) * 100:.2f}%")
 
200
  for train_index, test_index in kf.split(lbp_features):
201
  x_train, x_test = lbp_features[train_index], lbp_features[test_index]
202
  y_train, y_test = y[train_index], y[test_index]
203
+
204
+ lbp_classifier = KNeighborsClassifier(n_neighbors=lbp_grid_search.best_params_['n_neighbors'])
 
 
 
205
  lbp_classifier.fit(x_train, y_train)
206
  y_pred = lbp_classifier.predict(x_test)
207
+
208
  accuracy = accuracy_score(y_test, y_pred)
209
  lbp_accuracy_list.append(accuracy)
210
+
211
+ # Optionally: Print confusion matrix and precision
212
+
213
+ # print(f"Confusion Matrix for LBP Fold:\n{confusion_matrix(y_test, y_pred)}")
214
+ # print(f"Classification Report for LBP Fold:\n{classification_report(y_test, y_pred, zero_division=0)}")
215
 
216
  # Print overall LBP accuracy
217
  print(f"LBP Cross-validated accuracy: {np.mean(lbp_accuracy_list) * 100:.2f}%")
218
 
 
 
 
 
 
219
 
220
+ # In[59]:
221
+
222
+
223
+ def classify_texture(image, algorithm):
224
+ # Resize and convert the image to grayscale
225
+ resized_image = cv2.resize(image, TARGET_SIZE, interpolation=cv2.INTER_AREA)
226
+ gray_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY) # Convert to grayscale
227
+
228
+ # Select the feature extraction method based on the algorithm
229
+ if algorithm == 'GLCM':
230
+ features = calc_glcm_features([gray_image]) # Use parentheses, pass as a list
231
+ prediction = glcm_grid_search.predict(features)
232
+ elif algorithm == 'LBP':
233
+ features = extract_lbp_features([gray_image])
234
+ prediction = lbp_grid_search.predict(features)
235
  else:
236
  raise ValueError(f"Algorithm '{algorithm}' is not recognized.")
237
 
238
  return "Grass" if prediction[0] == 0 else "Wood"
239
 
240
+ # Function to highlight regions
241
+ def highlight_texture(image, result):
242
+ overlay = image.copy()
243
+ if result == "Grass":
244
+ overlay[:, :] = [0, 255, 0] # Highlight grass regions in green
245
+ else:
246
+ overlay[:, :] = [165, 42, 42] # Highlight wood regions in brown
247
+ return overlay
248
+
249
+
250
+ # Define the Gradio interface
251
+ def classify_uploaded_image(image, algorithm):
252
+ # Convert the uploaded image to a format that OpenCV can process
253
+ image = np.array(image, dtype=np.uint8) # Ensure correct type
254
+
255
+ # Call the classify_texture function
256
+ result = classify_texture(image, algorithm)
257
+
258
+ return result
259
+
260
  # Gradio Interface Setup
261
  iface = gr.Interface(
262
+ fn=classify_uploaded_image,
263
  inputs=[
264
+ gr.Image(type="numpy", label="Upload an Image"), # Image input, passing as NumPy array
265
+ gr.Dropdown(choices=["GLCM", "LBP"], label="Algorithm")
266
+ ],
267
  outputs="text",
268
+ title="Texture Classification"
269
  )
270
 
271
  # Launch the interface
272
  iface.launch()
273
+
274
+
275
+ # # Create Gradio interface
276
+ # iface = gr.Interface(
277
+ # fn=lambda image, algorithm: (result := classify_texture(image, algorithm), highlight_texture(image, result)),
278
+ # inputs=[
279
+ # gr.Image(type='numpy', label="Upload Image"),
280
+ # gr.Radio(choices=['GLCM', 'LBP'], label="Select Algorithm")
281
+ # ],
282
+ # outputs=[gr.Textbox(label="Classification Result"), gr.Image(label="Highlighted Result")],
283
+ # title="Texture Classification",
284
+ # description="Upload an image and select the classification algorithm (GLCM or LBP). The result will show the classification and highlight the detected texture."
285
+ # )
286
+
287
+ # # Launch the interface
288
+ # iface.launch()
289
+
290
+
291
+ # In[54]:
292
+
293
+
294
+ # Example of testing with a specific image
295
+ test_grass_image = cv2.imread('grass.jpg')
296
+ test_wood_image = cv2.imread('wood.jpg')
297
+
298
+ print("Testing with Grass Image Prediction:", classify_texture(test_grass_image, 'LBP'))
299
+ print("Testing with Wood Image Prediction:", classify_texture(test_wood_image, 'LBP'))
300
+
301
+
302
+ # In[ ]:
303
+
304
+
305
+
306
+
307
+
308
+ # In[ ]:
309
+
310
+
311
+
312
+