Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
import glob
|
| 2 |
import cv2
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
|
@@ -7,16 +7,17 @@ from sklearn.model_selection import KFold
|
|
| 7 |
from skimage.feature import graycomatrix, graycoprops
|
| 8 |
from sklearn.neighbors import KNeighborsClassifier
|
| 9 |
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
|
|
|
| 10 |
from sklearn.model_selection import GridSearchCV
|
|
|
|
| 11 |
from skimage.feature import local_binary_pattern
|
| 12 |
|
| 13 |
# Visualize GLCM features for grass and wood
|
| 14 |
def plot_features(features, title):
|
| 15 |
plt.figure(figsize=(10, 6))
|
| 16 |
for i, feature in enumerate(features.T): # Transpose to plot feature by feature
|
| 17 |
-
plt.plot(feature)
|
| 18 |
plt.title(title)
|
| 19 |
-
# plt.legend() # Removed the legend to avoid warning
|
| 20 |
plt.show()
|
| 21 |
|
| 22 |
# Define directories for grass and wood images
|
|
@@ -176,13 +177,19 @@ for train_index, test_index in kf.split(lbp_features):
|
|
| 176 |
# Print overall LBP accuracy
|
| 177 |
print(f"LBP Cross-validated accuracy: {np.mean(lbp_accuracy_list) * 100:.2f}%")
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# Define the Gradio interface
|
| 180 |
def classify_uploaded_image(image, algorithm):
|
| 181 |
-
image = np.array(image, dtype=np.uint8) #
|
| 182 |
|
| 183 |
if algorithm == "GLCM":
|
| 184 |
features = calc_glcm_features([image]) # Use parentheses, pass as a list
|
| 185 |
-
prediction =
|
| 186 |
elif algorithm == "LBP":
|
| 187 |
features = extract_lbp_features([image])
|
| 188 |
prediction = lbp_classifier.predict(features)
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import glob
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
| 7 |
from skimage.feature import graycomatrix, graycoprops
|
| 8 |
from sklearn.neighbors import KNeighborsClassifier
|
| 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 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
|
|
|
|
| 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)
|