Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,142 +1,129 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
classify_button = gr.Button("Classify Image")
|
| 131 |
-
|
| 132 |
-
# Output label
|
| 133 |
-
output_label = gr.Textbox(label="Predicted Texture")
|
| 134 |
-
|
| 135 |
-
# Set up interaction
|
| 136 |
-
classify_button.click(fn=classify, inputs=[img_input, classifier_dropdown], outputs=output_label)
|
| 137 |
-
|
| 138 |
-
# Launch the interface
|
| 139 |
-
interface.launch()
|
| 140 |
-
|
| 141 |
-
if __name__ == "__main__":
|
| 142 |
-
main()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CS5330 Fall 2024
|
| 3 |
+
Lab 2 Texture Classification
|
| 4 |
+
Calvin Lo
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
import joblib
|
| 10 |
+
from skimage.feature import local_binary_pattern, graycomatrix, graycoprops
|
| 11 |
+
|
| 12 |
+
IMAGE_SIZE_GLCM = 256
|
| 13 |
+
IMAGE_SIZE_LBP = 128
|
| 14 |
+
RADIUS = 1
|
| 15 |
+
N_POINTS = 8 * RADIUS
|
| 16 |
+
LBP_METHOD = "uniform"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def get_feature_vector(img, feature_type):
|
| 20 |
+
|
| 21 |
+
# convert to grayscale
|
| 22 |
+
img_gray = img.convert("L")
|
| 23 |
+
|
| 24 |
+
# resize and get feature vector
|
| 25 |
+
if feature_type == "GLCM":
|
| 26 |
+
img_resized = image_resize(img_gray, IMAGE_SIZE_GLCM)
|
| 27 |
+
feature_vector = compute_glcm_histogram_pil(img_resized)
|
| 28 |
+
else:
|
| 29 |
+
img_resized = image_resize(img_gray, IMAGE_SIZE_LBP)
|
| 30 |
+
feature_vector = get_lbp_hist(np.array(img_resized),
|
| 31 |
+
N_POINTS,
|
| 32 |
+
RADIUS,
|
| 33 |
+
LBP_METHOD)
|
| 34 |
+
return [feature_vector]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def compute_glcm_histogram_pil(image, distances=[1],
|
| 38 |
+
angles=[0],
|
| 39 |
+
levels=8,
|
| 40 |
+
symmetric=True):
|
| 41 |
+
|
| 42 |
+
# Convert the PIL image to a NumPy array
|
| 43 |
+
image_np = np.array(image)
|
| 44 |
+
|
| 45 |
+
# Quantize the grayscale image to the specified number of levels
|
| 46 |
+
image_np = (image_np * (levels - 1) / 255).astype(np.uint8)
|
| 47 |
+
|
| 48 |
+
# Compute the GLCM using skimage's graycomatrix function
|
| 49 |
+
glcm = graycomatrix(image_np,
|
| 50 |
+
distances=distances,
|
| 51 |
+
angles=angles,
|
| 52 |
+
levels=levels,
|
| 53 |
+
symmetric=symmetric,
|
| 54 |
+
normed=True)
|
| 55 |
+
|
| 56 |
+
# Extract GLCM properties
|
| 57 |
+
homogeneity = graycoprops(glcm, 'homogeneity')[0, 0]
|
| 58 |
+
correlation = graycoprops(glcm, 'correlation')[0, 0]
|
| 59 |
+
|
| 60 |
+
# Create the feature vector
|
| 61 |
+
feature_vector = np.array([homogeneity, correlation])
|
| 62 |
+
|
| 63 |
+
return feature_vector
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def get_lbp_hist(gray_image, n_points, radius, method):
|
| 67 |
+
# Compute LBP for the image
|
| 68 |
+
lbp = local_binary_pattern(gray_image, n_points, radius, method)
|
| 69 |
+
lbp_hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3),
|
| 70 |
+
range=(0, n_points + 2))
|
| 71 |
+
|
| 72 |
+
# Normalize the histogram
|
| 73 |
+
lbp_hist = lbp_hist.astype("float")
|
| 74 |
+
lbp_hist /= (lbp_hist.sum() + 1e-6) # Normalized histogram
|
| 75 |
+
return lbp_hist
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def image_resize(img, n):
|
| 79 |
+
# Crop the image to a square by finding the minimum dimension
|
| 80 |
+
min_dimension = min(img.size)
|
| 81 |
+
left = (img.width - min_dimension) / 2
|
| 82 |
+
top = (img.height - min_dimension) / 2
|
| 83 |
+
right = (img.width + min_dimension) / 2
|
| 84 |
+
bottom = (img.height + min_dimension) / 2
|
| 85 |
+
img = img.crop((left, top, right, bottom))
|
| 86 |
+
img = img.resize((n, n))
|
| 87 |
+
return img
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def classify(img, feature_type):
|
| 91 |
+
# To load the model later
|
| 92 |
+
loaded_model = joblib.load(feature_type + '_model.joblib')
|
| 93 |
+
|
| 94 |
+
feature_vector = get_feature_vector(img, feature_type)
|
| 95 |
+
# Make predictions with the loaded model
|
| 96 |
+
label = loaded_model.predict(feature_vector)
|
| 97 |
+
|
| 98 |
+
return f"{label[0]}"
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def main():
|
| 102 |
+
# Gradio interface
|
| 103 |
+
with gr.Blocks() as interface:
|
| 104 |
+
gr.Markdown("## Image Texture Classifier")
|
| 105 |
+
|
| 106 |
+
# Image upload input
|
| 107 |
+
img_input = gr.Image(type="pil")
|
| 108 |
+
|
| 109 |
+
# Dropdown for selecting classifier
|
| 110 |
+
classifier_dropdown = gr.Dropdown(choices=["GLCM", "LBP"],
|
| 111 |
+
label="Feature Vector Type")
|
| 112 |
+
|
| 113 |
+
# Button for classification
|
| 114 |
+
classify_button = gr.Button("Classify Image")
|
| 115 |
+
|
| 116 |
+
# Output label
|
| 117 |
+
output_label = gr.Textbox(label="Predicted Texture")
|
| 118 |
+
|
| 119 |
+
# Set up interaction
|
| 120 |
+
classify_button.click(fn=classify,
|
| 121 |
+
inputs=[img_input, classifier_dropdown],
|
| 122 |
+
outputs=output_label)
|
| 123 |
+
|
| 124 |
+
# Launch the interface
|
| 125 |
+
interface.launch()
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|