local5 commited on
Commit
4aa8baf
·
verified ·
1 Parent(s): ea6930e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -142
app.py CHANGED
@@ -1,142 +1,129 @@
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()
 
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()