local5 commited on
Commit
63d22c4
·
verified ·
1 Parent(s): fea359b

Upload 3 files

Browse files
Files changed (3) hide show
  1. GLCM_model.joblib +3 -0
  2. LBP_model.joblib +3 -0
  3. app.py +142 -0
GLCM_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e26973ca8d5c08b72265cef7a400807223b1b2d30c0210995bcf2077ef13bd86
3
+ size 763
LBP_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ec7246212ad49b76d2143fb7308f2789c4b8abad5e00a7357fecce9d8471f772
3
+ size 827
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import joblib # Import directly from joblib
4
+ from sklearn.svm import LinearSVC
5
+ from skimage.feature import local_binary_pattern, graycomatrix, graycoprops
6
+ from PIL import Image
7
+
8
+ IMAGE_SIZE_GLCM = 256
9
+ IMAGE_SIZE_LBP = 128
10
+ RADIUS = 1
11
+ N_POINTS = 8 * RADIUS
12
+ LBP_METHOD = "uniform"
13
+
14
+ def get_feature_vector(img, feature_type):
15
+
16
+
17
+ img_gray = img.convert("L")
18
+ if feature_type == "GLCM":
19
+ img_resized = image_resize(img_gray, IMAGE_SIZE_GLCM)
20
+ feature_vector = compute_glcm_histogram_pil(img_resized)
21
+ else:
22
+ img_resized = image_resize(img_gray, IMAGE_SIZE_LBP)
23
+ feature_vector = get_lbp_hist(np.array(img_resized), N_POINTS, RADIUS, LBP_METHOD)
24
+ return [feature_vector]
25
+
26
+ def compute_glcm_histogram_pil(image, distances=[1], angles=[0], levels=8, symmetric=True):
27
+
28
+ # Step 2: Convert the PIL image to a NumPy array
29
+ image_np = np.array(image)
30
+
31
+ # Step 3: Quantize the grayscale image to the specified number of levels (e.g., 256)
32
+ image_np = (image_np * (levels - 1) / 255).astype(np.uint8) # Scale to desired levels
33
+
34
+
35
+ # Step 4: Compute the GLCM using skimage's graycomatrix function
36
+ glcm = graycomatrix(image_np,
37
+ distances=distances,
38
+ angles=angles,
39
+ levels=levels,
40
+ symmetric=symmetric,
41
+ normed=True)
42
+
43
+ # Extract GLCM properties
44
+ homogeneity = graycoprops(glcm, 'homogeneity')[0, 0]
45
+ correlation = graycoprops(glcm, 'correlation')[0, 0]
46
+
47
+
48
+ # Create the feature vector
49
+ feature_vector = np.array([homogeneity, correlation])
50
+
51
+ # feature_vector = np.array([contrast,homogeneity, correlation, energy, dissimilarity, asm, entropy])
52
+
53
+ return feature_vector
54
+
55
+ def get_lbp_hist(gray_image, n_points, radius, method):
56
+ # Step 3: Compute LBP for the image
57
+ lbp = local_binary_pattern(gray_image, n_points, radius, method)
58
+
59
+ # Step 4: Compute LBP histogram
60
+ # The histogram will have 'n_points + 2' bins if using the 'uniform' method.
61
+ lbp_hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), range=(0, n_points + 2))
62
+
63
+ # Normalize the histogram
64
+ lbp_hist = lbp_hist.astype("float")
65
+ lbp_hist /= (lbp_hist.sum() + 1e-6) # Normalized histogram
66
+ return lbp_hist
67
+
68
+ def image_resize(img, n):
69
+ # Crop the image to a square by finding the minimum dimension
70
+ min_dimension = min(img.size)
71
+ left = (img.width - min_dimension) / 2
72
+ top = (img.height - min_dimension) / 2
73
+ right = (img.width + min_dimension) / 2
74
+ bottom = (img.height + min_dimension) / 2
75
+ img = img.crop((left, top, right, bottom))
76
+
77
+ # Resize to 128x128 pixels
78
+ img = img.resize((n, n))
79
+ return img
80
+
81
+
82
+ def rgb_to_quantized_gray_pil(image, num_levels):
83
+ """
84
+ Convert an RGB image to a quantized grayscale image using PIL.
85
+
86
+ Parameters:
87
+ - image: PIL Image object in grayscale format.
88
+ - num_levels: Number of gray levels for quantization.
89
+
90
+ Returns:
91
+ - quantized_image: PIL Image object in quantized grayscale.
92
+ """
93
+ # Convert the grayscale image to a NumPy array for manipulation
94
+ gray_array = np.array(image)
95
+
96
+ # Quantize the grayscale image to the specified number of gray levels
97
+ max_val = 255 # Max value for an 8-bit grayscale image
98
+ quantized_array = np.floor(gray_array / (max_val / (num_levels - 1))) * (max_val / (num_levels - 1))
99
+ quantized_array = quantized_array.astype(np.uint8) # Convert back to 8-bit values
100
+
101
+ # Convert the quantized NumPy array back to a PIL Image
102
+ quantized_image = Image.fromarray(quantized_array, mode="L")
103
+
104
+ return quantized_image
105
+
106
+ # Function to classify the image
107
+ def classify(img, feature_type):
108
+
109
+ # To load the model later
110
+ loaded_model = joblib.load(feature_type + '_model.joblib')
111
+
112
+ feature_vector = get_feature_vector(img, feature_type)
113
+ # Make predictions with the loaded model
114
+ label = loaded_model.predict(feature_vector)
115
+
116
+ return f"{label[0]}"
117
+
118
+ def main():
119
+ # Gradio interface
120
+ with gr.Blocks() as interface:
121
+ gr.Markdown("## Image Classifier")
122
+
123
+ # Image upload input
124
+ img_input = gr.Image(type="pil")
125
+
126
+ # Dropdown for selecting classifier
127
+ classifier_dropdown = gr.Dropdown(choices=["GLCM", "LBP"], label="Feature Vector Type")
128
+
129
+ # Button for classification
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()