Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,5 @@
|
|
| 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
|
|
@@ -14,23 +9,18 @@ from sklearn.neighbors import KNeighborsClassifier
|
|
| 14 |
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
| 15 |
import pandas as pd
|
| 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
|
| 30 |
plt.title(title)
|
| 31 |
-
plt.legend()
|
| 32 |
plt.show()
|
| 33 |
|
|
|
|
| 34 |
# Define directories for grass and wood images
|
| 35 |
grass_dir = "images/Grass/Train_Grass"
|
| 36 |
wood_dir = "images/Wood/Train_Wood"
|
|
@@ -38,76 +28,71 @@ wood_dir = "images/Wood/Train_Wood"
|
|
| 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 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 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,
|
| 71 |
-
dissimilarity = graycoprops(glcm,
|
| 72 |
-
homogeneity = graycoprops(glcm,
|
| 73 |
-
energy = graycoprops(glcm,
|
| 74 |
-
correlation = graycoprops(glcm,
|
| 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 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 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,36 +100,24 @@ plot_features(np.array(wood_glcm_features), "GLCM Features for Wood 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)
|
| 148 |
|
| 149 |
# Create the labels: 0 for grass, 1 for wood
|
| 150 |
y = np.array([0] * num_grass + [1] * num_wood)
|
|
@@ -158,9 +131,7 @@ glcm_accuracy_list = []
|
|
| 158 |
lbp_accuracy_list = []
|
| 159 |
|
| 160 |
# Parameter tuning using GridSearchCV for KNN classifier
|
| 161 |
-
param_grid = {
|
| 162 |
-
'p' : [1,2]
|
| 163 |
-
}
|
| 164 |
|
| 165 |
# GLCM Classifier Training and Evaluation
|
| 166 |
glcm_knn = KNeighborsClassifier()
|
|
@@ -173,18 +144,15 @@ print("Best parameters for GLCM KNN:", glcm_grid_search.best_params_)
|
|
| 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(
|
|
|
|
|
|
|
| 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,113 +168,51 @@ print("Best parameters for LBP KNN:", lbp_grid_search.best_params_)
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 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"),
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import glob
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
| 9 |
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
| 10 |
import pandas as pd
|
| 11 |
from sklearn.model_selection import GridSearchCV
|
|
|
|
| 12 |
from skimage.feature import local_binary_pattern
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 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 |
+
|
| 24 |
# Define directories for grass and wood images
|
| 25 |
grass_dir = "images/Grass/Train_Grass"
|
| 26 |
wood_dir = "images/Wood/Train_Wood"
|
|
|
|
| 28 |
# Constants for LBP
|
| 29 |
RADIUS = 1
|
| 30 |
N_POINTS = 12 * RADIUS
|
| 31 |
+
TARGET_SIZE = (30, 30)
|
| 32 |
|
| 33 |
# for GLCM
|
| 34 |
distances = [1]
|
| 35 |
angles = [0]
|
| 36 |
|
| 37 |
+
|
| 38 |
+
# Use glob to load images from the directory
|
| 39 |
def load_and_convert_images(directory):
|
| 40 |
+
"""Load images from a directory using glob and convert them to grayscale."""
|
| 41 |
dataset = []
|
| 42 |
+
image_paths = glob.glob(f"{directory}/*.jpg") + glob.glob(f"{directory}/*.png") + glob.glob(f"{directory}/*.jpeg")
|
| 43 |
+
|
| 44 |
+
for img_path in image_paths:
|
| 45 |
+
img = cv2.imread(img_path) # Read the image
|
| 46 |
+
if img is not None:
|
| 47 |
+
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
|
| 48 |
+
resized_image = cv2.resize(gray_image, TARGET_SIZE, interpolation=cv2.INTER_AREA)
|
| 49 |
+
dataset.append(resized_image)
|
| 50 |
+
else:
|
| 51 |
+
print(f"Warning: Failed to load image {img_path}")
|
| 52 |
+
|
| 53 |
return dataset
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
def calc_glcm_features(images):
|
| 57 |
features = []
|
| 58 |
for img in images:
|
| 59 |
glcm = graycomatrix(img, distances, angles, symmetric=True, normed=True)
|
| 60 |
|
| 61 |
+
contrast = graycoprops(glcm, "contrast")[0, 0]
|
| 62 |
+
dissimilarity = graycoprops(glcm, "dissimilarity")[0, 0]
|
| 63 |
+
homogeneity = graycoprops(glcm, "homogeneity")[0, 0]
|
| 64 |
+
energy = graycoprops(glcm, "energy")[0, 0]
|
| 65 |
+
correlation = graycoprops(glcm, "correlation")[0, 0]
|
| 66 |
|
| 67 |
+
features.append([contrast, dissimilarity, homogeneity, energy, correlation])
|
| 68 |
return features
|
| 69 |
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
def extract_lbp_features(images):
|
| 72 |
+
"""Extract LBP features from a list of images."""
|
| 73 |
+
lbp_features = []
|
| 74 |
+
for image in images:
|
| 75 |
lbp = local_binary_pattern(image, N_POINTS, RADIUS, method="uniform")
|
| 76 |
n_bins = int(lbp.max() + 1)
|
| 77 |
lbp_hist, _ = np.histogram(lbp, bins=n_bins, range=(0, n_bins), density=True)
|
| 78 |
lbp_features.append(lbp_hist)
|
| 79 |
+
return lbp_features
|
| 80 |
+
|
| 81 |
|
| 82 |
# Load datasets
|
| 83 |
grass_dataset = load_and_convert_images(grass_dir)
|
| 84 |
+
wood_dataset = load_and_convert_images(wood_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# Create labels (0 for grass, 1 for wood)
|
| 87 |
+
grass_labels = [0] * len(grass_dataset)
|
| 88 |
+
wood_labels = [1] * len(wood_dataset)
|
| 89 |
|
| 90 |
# Calculate features
|
| 91 |
grass_glcm_features = calc_glcm_features(grass_dataset)
|
| 92 |
+
wood_glcm_features = calc_glcm_features(wood_dataset)
|
|
|
|
|
|
|
| 93 |
|
| 94 |
grass_lbp_features = extract_lbp_features(grass_dataset)
|
| 95 |
+
wood_lbp_features = extract_lbp_features(wood_dataset)
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# GLCM features for grass and wood
|
| 98 |
plot_features(np.array(grass_glcm_features), "GLCM Features for Grass Images")
|
|
|
|
| 100 |
|
| 101 |
# LBP features for grass and wood
|
| 102 |
plot_features(np.array(grass_lbp_features), "LBP Features for Grass Images")
|
| 103 |
+
plot_features(np.array(wood_lbp_features), "LBP Features for Wood Images")
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# Combine labels and features for GLCM classifier
|
| 106 |
glcm_features = grass_glcm_features + wood_glcm_features
|
| 107 |
+
glcm_labels = grass_labels + wood_labels
|
| 108 |
|
| 109 |
# Convert to numpy array
|
| 110 |
glcm_features = np.array(glcm_features)
|
| 111 |
|
|
|
|
|
|
|
|
|
|
| 112 |
# Prepare labels and features for LBP classifier
|
| 113 |
lbp_features = grass_lbp_features + wood_lbp_features
|
| 114 |
+
lbp_labels = grass_labels + wood_labels
|
| 115 |
|
| 116 |
# Convert to numpy array
|
| 117 |
lbp_features = np.array(lbp_features)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
num_grass = len(grass_dataset) # Number of grass images
|
| 120 |
+
num_wood = len(wood_dataset) # Number of wood images
|
| 121 |
|
| 122 |
# Create the labels: 0 for grass, 1 for wood
|
| 123 |
y = np.array([0] * num_grass + [1] * num_wood)
|
|
|
|
| 131 |
lbp_accuracy_list = []
|
| 132 |
|
| 133 |
# Parameter tuning using GridSearchCV for KNN classifier
|
| 134 |
+
param_grid = {"n_neighbors": [3, 5, 7], "p": [1, 2]}
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# GLCM Classifier Training and Evaluation
|
| 137 |
glcm_knn = KNeighborsClassifier()
|
|
|
|
| 144 |
for train_index, test_index in kf.split(glcm_features):
|
| 145 |
x_train, x_test = glcm_features[train_index], glcm_features[test_index]
|
| 146 |
y_train, y_test = y[train_index], y[test_index]
|
| 147 |
+
|
| 148 |
+
glcm_classifier = KNeighborsClassifier(
|
| 149 |
+
n_neighbors=glcm_grid_search.best_params_["n_neighbors"]
|
| 150 |
+
)
|
| 151 |
glcm_classifier.fit(x_train, y_train)
|
| 152 |
y_pred = glcm_classifier.predict(x_test)
|
| 153 |
+
|
| 154 |
accuracy = accuracy_score(y_test, y_pred)
|
| 155 |
glcm_accuracy_list.append(accuracy)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
# Print overall GLCM accuracy
|
| 158 |
print(f"GLCM Cross-validated accuracy: {np.mean(glcm_accuracy_list) * 100:.2f}%")
|
|
|
|
| 168 |
for train_index, test_index in kf.split(lbp_features):
|
| 169 |
x_train, x_test = lbp_features[train_index], lbp_features[test_index]
|
| 170 |
y_train, y_test = y[train_index], y[test_index]
|
| 171 |
+
|
| 172 |
+
lbp_classifier = KNeighborsClassifier(
|
| 173 |
+
n_neighbors=lbp_grid_search.best_params_["n_neighbors"],
|
| 174 |
+
p=lbp_grid_search.best_params_["p"]
|
| 175 |
+
)
|
| 176 |
lbp_classifier.fit(x_train, y_train)
|
| 177 |
y_pred = lbp_classifier.predict(x_test)
|
| 178 |
+
|
| 179 |
accuracy = accuracy_score(y_test, y_pred)
|
| 180 |
lbp_accuracy_list.append(accuracy)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
# Print overall LBP accuracy
|
| 183 |
print(f"LBP Cross-validated accuracy: {np.mean(lbp_accuracy_list) * 100:.2f}%")
|
| 184 |
|
| 185 |
+
# Preprocess uploaded image for classification
|
| 186 |
+
def preprocess_image(image):
|
| 187 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert to grayscale
|
| 188 |
+
resized_image = cv2.resize(gray_image, TARGET_SIZE, interpolation=cv2.INTER_AREA) # Resize to match training size
|
| 189 |
+
return resized_image
|
| 190 |
|
| 191 |
+
# Define the Gradio interface
|
| 192 |
+
def classify_uploaded_image(image, algorithm):
|
| 193 |
+
image = preprocess_image(np.array(image, dtype=np.uint8)) # Preprocess uploaded image
|
| 194 |
+
|
| 195 |
+
if algorithm == "GLCM":
|
| 196 |
+
features = calc_glcm_features([image]) # Use parentheses, pass as a list
|
| 197 |
+
prediction = glcm_classifier.predict(features)
|
| 198 |
+
elif algorithm == "LBP":
|
| 199 |
+
features = extract_lbp_features([image])
|
| 200 |
+
prediction = lbp_classifier.predict(features)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
else:
|
| 202 |
raise ValueError(f"Algorithm '{algorithm}' is not recognized.")
|
| 203 |
|
| 204 |
return "Grass" if prediction[0] == 0 else "Wood"
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
# Gradio Interface Setup
|
| 207 |
iface = gr.Interface(
|
| 208 |
+
fn=classify_uploaded_image,
|
| 209 |
inputs=[
|
| 210 |
+
gr.Image(type="numpy", label="Upload an Image"),
|
| 211 |
+
gr.Dropdown(choices=["GLCM", "LBP"], label="Algorithm"),
|
| 212 |
+
],
|
| 213 |
outputs="text",
|
| 214 |
+
title="Texture Classification",
|
| 215 |
)
|
| 216 |
|
| 217 |
# Launch the interface
|
| 218 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|