Kalhar.Pandya commited on
Commit
abd2768
·
1 Parent(s): 85c3feb

Added images and model with Git LFS

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. app.py +43 -13
  2. images_dataset/brick/patch_001.png +0 -0
  3. images_dataset/brick/patch_002.png +0 -0
  4. images_dataset/brick/patch_003.png +0 -0
  5. images_dataset/brick/patch_004.png +0 -0
  6. images_dataset/brick/patch_005.png +0 -0
  7. images_dataset/brick/patch_006.png +0 -0
  8. images_dataset/brick/patch_007.png +0 -0
  9. images_dataset/brick/patch_008.png +0 -0
  10. images_dataset/brick/patch_009.png +0 -0
  11. images_dataset/brick/patch_010.png +0 -0
  12. images_dataset/brick/patch_011.png +0 -0
  13. images_dataset/brick/patch_012.png +0 -0
  14. images_dataset/brick/patch_013.png +0 -0
  15. images_dataset/brick/patch_014.png +0 -0
  16. images_dataset/brick/patch_015.png +0 -0
  17. images_dataset/brick/patch_016.png +0 -0
  18. images_dataset/brick/patch_017.png +0 -0
  19. images_dataset/brick/patch_018.png +0 -0
  20. images_dataset/brick/patch_019.png +0 -0
  21. images_dataset/brick/patch_020.png +0 -0
  22. images_dataset/brick/patch_021.png +0 -0
  23. images_dataset/brick/patch_022.png +0 -0
  24. images_dataset/brick/patch_023.png +0 -0
  25. images_dataset/brick/patch_024.png +0 -0
  26. images_dataset/brick/patch_025.png +0 -0
  27. images_dataset/brick/patch_026.png +0 -0
  28. images_dataset/brick/patch_027.png +0 -0
  29. images_dataset/brick/patch_028.png +0 -0
  30. images_dataset/brick/patch_029.png +0 -0
  31. images_dataset/brick/patch_030.png +0 -0
  32. images_dataset/brick/patch_031.png +0 -0
  33. images_dataset/brick/patch_032.png +0 -0
  34. images_dataset/brick/patch_033.png +0 -0
  35. images_dataset/brick/patch_034.png +0 -0
  36. images_dataset/brick/patch_035.png +0 -0
  37. images_dataset/brick/patch_036.png +0 -0
  38. images_dataset/brick/patch_037.png +0 -0
  39. images_dataset/brick/patch_038.png +0 -0
  40. images_dataset/brick/patch_039.png +0 -0
  41. images_dataset/brick/patch_040.png +0 -0
  42. images_dataset/brick/patch_041.png +0 -0
  43. images_dataset/brick/patch_042.png +0 -0
  44. images_dataset/brick/patch_043.png +0 -0
  45. images_dataset/brick/patch_044.png +0 -0
  46. images_dataset/brick/patch_045.png +0 -0
  47. images_dataset/brick/patch_046.png +0 -0
  48. images_dataset/brick/patch_047.png +0 -0
  49. images_dataset/brick/patch_048.png +0 -0
  50. images_dataset/brick/patch_049.png +0 -0
app.py CHANGED
@@ -34,8 +34,12 @@ def load_model(model_filename):
34
  def classify_new_image(input_image_path, model_choice):
35
  """
36
  Expects input_image_path as a file path and model_choice as one of the keys in models.
37
- Loads the image, processes it by extracting patches and computing predictions on each patch,
38
- aggregates patch predictions, and returns the final predicted class and probabilities.
 
 
 
 
39
  """
40
  global models, training_log, class_names
41
  progress_log = training_log + "\nStarting classification...\n"
@@ -69,13 +73,12 @@ def classify_new_image(input_image_path, model_choice):
69
 
70
  # Patch extraction parameters
71
  patch_size = (100, 100)
72
- edge_density_thresh_low = 0.0
73
- edge_density_thresh_high = 0.5
74
  patch_w, patch_h = patch_size
75
  img_h, img_w = gray.shape
76
 
77
  valid_patch_count = 0
78
  summed_probabilities = None
 
79
 
80
  # Loop over non-overlapping patches
81
  for y in range(0, img_h - patch_h + 1, patch_h):
@@ -87,12 +90,16 @@ def classify_new_image(input_image_path, model_choice):
87
  density = num_edge_pixels / total_pixels
88
  progress_log += f"Patch at ({x}, {y}) - edge density: {density:.3f}\n"
89
 
90
- if edge_density_thresh_low < density < edge_density_thresh_high:
91
  valid_patch_count += 1
92
  features = extract_features_from_image(patch)
93
  feature_vector = features['combined_features'].reshape(1, -1)
94
  patch_probabilities = classifier.predict_proba(feature_vector)[0]
95
- progress_log += f"Patch at ({x}, {y}) predicted probabilities: {patch_probabilities}\n"
 
 
 
 
96
 
97
  if summed_probabilities is None:
98
  summed_probabilities = patch_probabilities
@@ -107,10 +114,8 @@ def classify_new_image(input_image_path, model_choice):
107
  summed_probabilities = classifier.predict_proba(feature_vector)[0]
108
  valid_patch_count = 1
109
 
110
- # Average the probabilities from all valid patches
111
  averaged_probabilities = summed_probabilities / valid_patch_count
112
-
113
- # Normalize the averaged probabilities so they sum to 1
114
  normalized_probabilities = averaged_probabilities / np.sum(averaged_probabilities)
115
 
116
  final_prediction_index = np.argmax(normalized_probabilities)
@@ -120,7 +125,32 @@ def classify_new_image(input_image_path, model_choice):
120
 
121
  print(progress_log)
122
  print(prob_dict)
123
- return final_prediction, prob_dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  # ---------------------------------------------------------------------
126
  # Gradio Interface Setup using file paths and model selection
@@ -130,7 +160,6 @@ if __name__ == "__main__":
130
  load_model(model_filename)
131
 
132
  # Create a dropdown for model selection.
133
- # If models dict is not yet populated, we set default choices.
134
  model_choices = list(models.keys()) if models else ['svm', 'rf', 'combined']
135
 
136
  iface = gr.Interface(
@@ -141,11 +170,12 @@ if __name__ == "__main__":
141
  ],
142
  outputs=[
143
  gr.Label(label="Predicted Class"),
144
- gr.Label(label="Probabilities")
 
145
  ],
146
  title="Stone, Wood, Brick Classifier",
147
  description=("Upload an image and select a classifier model (svm, rf, combined) to classify it.\n\n"
148
  "The image is processed by subdividing it into patches and aggregating the predictions. "
149
- "Progress logs are printed to the terminal.")
150
  )
151
  iface.launch(share=True)
 
34
  def classify_new_image(input_image_path, model_choice):
35
  """
36
  Expects input_image_path as a file path and model_choice as one of the keys in models.
37
+ Loads the image, processes it by extracting patches, classifies each patch, and aggregates
38
+ patch predictions. Also, draws transparent overlays on each patch according to its predicted label.
39
+ Returns:
40
+ annotated_image_rgb (numpy array): The image with transparent overlays.
41
+ final_prediction (str): The final predicted class.
42
+ prob_dict (dict): Dictionary of class probabilities.
43
  """
44
  global models, training_log, class_names
45
  progress_log = training_log + "\nStarting classification...\n"
 
73
 
74
  # Patch extraction parameters
75
  patch_size = (100, 100)
 
 
76
  patch_w, patch_h = patch_size
77
  img_h, img_w = gray.shape
78
 
79
  valid_patch_count = 0
80
  summed_probabilities = None
81
+ overlays_list = [] # To store (x, y, w, h, predicted_label) for each valid patch
82
 
83
  # Loop over non-overlapping patches
84
  for y in range(0, img_h - patch_h + 1, patch_h):
 
90
  density = num_edge_pixels / total_pixels
91
  progress_log += f"Patch at ({x}, {y}) - edge density: {density:.3f}\n"
92
 
93
+ if 0.0 < density < 0.5:
94
  valid_patch_count += 1
95
  features = extract_features_from_image(patch)
96
  feature_vector = features['combined_features'].reshape(1, -1)
97
  patch_probabilities = classifier.predict_proba(feature_vector)[0]
98
+ predicted_index = np.argmax(patch_probabilities)
99
+ predicted_label = class_names[predicted_index]
100
+ progress_log += f"Patch at ({x}, {y}) predicted: {predicted_label} with probabilities {patch_probabilities}\n"
101
+
102
+ overlays_list.append((x, y, patch_w, patch_h, predicted_label))
103
 
104
  if summed_probabilities is None:
105
  summed_probabilities = patch_probabilities
 
114
  summed_probabilities = classifier.predict_proba(feature_vector)[0]
115
  valid_patch_count = 1
116
 
117
+ # Average the probabilities from all valid patches and normalize them
118
  averaged_probabilities = summed_probabilities / valid_patch_count
 
 
119
  normalized_probabilities = averaged_probabilities / np.sum(averaged_probabilities)
120
 
121
  final_prediction_index = np.argmax(normalized_probabilities)
 
125
 
126
  print(progress_log)
127
  print(prob_dict)
128
+
129
+ # Create an annotated image with transparent overlays
130
+ annotated_image = resized_image.copy()
131
+ overlay = annotated_image.copy()
132
+ alpha = 0.4 # Transparency factor
133
+
134
+ # Define overlay colors in BGR for each class (adjust as desired)
135
+ color_map = {
136
+ 'wood': (0, 255, 255), # Yellow (BGR format)
137
+ 'brick': (0, 0, 255), # Red (BGR format)
138
+ 'stone': (128, 128, 128) # Gray (BGR format)
139
+ }
140
+
141
+ for (x, y, w, h, label) in overlays_list:
142
+ label_lower = label.lower()
143
+ if prob_dict[label] < 0.2:
144
+ continue
145
+ color = color_map.get(label_lower, (0, 255, 0)) # Default to green if unknown
146
+ cv2.rectangle(overlay, (x, y), (x+w, y+h), color, thickness=-1)
147
+
148
+ # Blend the overlay with the original image
149
+ annotated_image = cv2.addWeighted(overlay, alpha, annotated_image, 1 - alpha, 0)
150
+ # Convert annotated image from BGR to RGB for Gradio display
151
+ annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
152
+
153
+ return final_prediction, prob_dict, annotated_image_rgb
154
 
155
  # ---------------------------------------------------------------------
156
  # Gradio Interface Setup using file paths and model selection
 
160
  load_model(model_filename)
161
 
162
  # Create a dropdown for model selection.
 
163
  model_choices = list(models.keys()) if models else ['svm', 'rf', 'combined']
164
 
165
  iface = gr.Interface(
 
170
  ],
171
  outputs=[
172
  gr.Label(label="Predicted Class"),
173
+ gr.Label(label="Probabilities"),
174
+ gr.Image(label="Annotated Image")
175
  ],
176
  title="Stone, Wood, Brick Classifier",
177
  description=("Upload an image and select a classifier model (svm, rf, combined) to classify it.\n\n"
178
  "The image is processed by subdividing it into patches and aggregating the predictions. "
179
+ "Transparent overlays are drawn on detected objects. Progress logs are printed to the terminal.")
180
  )
181
  iface.launch(share=True)
images_dataset/brick/patch_001.png CHANGED

Git LFS Details

  • SHA256: 559ad2d7d95294af12a6875059f3044c105e6677b9ed86c2d7d5bc4900b7ab34
  • Pointer size: 130 Bytes
  • Size of remote file: 24.8 kB
images_dataset/brick/patch_002.png CHANGED

Git LFS Details

  • SHA256: a46d0ea30a4af71d0eb3d00a33ec23e724c3830e58b104b78208638fc4ad6915
  • Pointer size: 130 Bytes
  • Size of remote file: 24 kB
images_dataset/brick/patch_003.png CHANGED

Git LFS Details

  • SHA256: 94b121a7b150bc17bed5c09602a8e2ea8f495ca747fee90184f4b14d6d16a443
  • Pointer size: 130 Bytes
  • Size of remote file: 23.6 kB
images_dataset/brick/patch_004.png CHANGED

Git LFS Details

  • SHA256: 5f4288e10f2df00c73d85f1f56c436f4dd43b8db854ea6c2dde505a7f8e41db5
  • Pointer size: 130 Bytes
  • Size of remote file: 24.7 kB
images_dataset/brick/patch_005.png CHANGED

Git LFS Details

  • SHA256: 2707e7d992ed488412672d9e23a4c5742aa18771d711dd2a003d628d965ece75
  • Pointer size: 130 Bytes
  • Size of remote file: 25.3 kB
images_dataset/brick/patch_006.png CHANGED

Git LFS Details

  • SHA256: 58768ee736004dd4748664c0783a4e58cfe0ae884fc6374b003565e384674351
  • Pointer size: 130 Bytes
  • Size of remote file: 24.8 kB
images_dataset/brick/patch_007.png CHANGED

Git LFS Details

  • SHA256: 4908aa1e859bed8cf249fd947502c7c269b7b6a4eb5343109e452be4d54de62f
  • Pointer size: 130 Bytes
  • Size of remote file: 24.5 kB
images_dataset/brick/patch_008.png CHANGED

Git LFS Details

  • SHA256: 12ccc43c3ea6783c9ab1e72ed5881a28104b439054d1da77e78fd54312dd8cb5
  • Pointer size: 130 Bytes
  • Size of remote file: 23.5 kB
images_dataset/brick/patch_009.png CHANGED

Git LFS Details

  • SHA256: 5b90c40a3703b957016c09269cf828cc86820e8e8dfdd357bf56ffd6203e3910
  • Pointer size: 130 Bytes
  • Size of remote file: 23.1 kB
images_dataset/brick/patch_010.png CHANGED

Git LFS Details

  • SHA256: 8f02e25dc18f200dc6eefe4df256670be348a1cc5a000d15060ffe15e8f72a62
  • Pointer size: 130 Bytes
  • Size of remote file: 23.3 kB
images_dataset/brick/patch_011.png CHANGED

Git LFS Details

  • SHA256: a11faa37a825d5846801ad3a9456556284d0f0cafe11a04b1c97a6d7b255dc1d
  • Pointer size: 130 Bytes
  • Size of remote file: 23.1 kB
images_dataset/brick/patch_012.png CHANGED

Git LFS Details

  • SHA256: ae53de82816d07500d21a39d26239a06bbeca12cc74875a3a7fb543c31d7a9aa
  • Pointer size: 130 Bytes
  • Size of remote file: 24.3 kB
images_dataset/brick/patch_013.png CHANGED

Git LFS Details

  • SHA256: 2970a41d0f0a400b84cd4cb5b49613c0eed27af64575eacc63eedd24f469f77a
  • Pointer size: 130 Bytes
  • Size of remote file: 25.5 kB
images_dataset/brick/patch_014.png CHANGED

Git LFS Details

  • SHA256: e6d224dc55d4190131b9cb0d618ba47abdfe73902d64eb6cfc0570a189c902b3
  • Pointer size: 130 Bytes
  • Size of remote file: 24.9 kB
images_dataset/brick/patch_015.png CHANGED

Git LFS Details

  • SHA256: fc751d5c2d1ff0bdb87065cf38db448df9751bd3206988a9a62dcd14d7c8ce4d
  • Pointer size: 130 Bytes
  • Size of remote file: 24.6 kB
images_dataset/brick/patch_016.png CHANGED

Git LFS Details

  • SHA256: 29f6d3b80dff50bbebc2262eec5443a7e82f0bd1c54079ebe65e78e5a042dd3c
  • Pointer size: 130 Bytes
  • Size of remote file: 25.6 kB
images_dataset/brick/patch_017.png CHANGED

Git LFS Details

  • SHA256: e2482572223eeb0010b28fe11d44282b7c0ac1f2b03da133373c6dd581943b14
  • Pointer size: 130 Bytes
  • Size of remote file: 25.2 kB
images_dataset/brick/patch_018.png CHANGED

Git LFS Details

  • SHA256: 24e21b5307db9b01317a11f696ac17d515459637d1e816e55b762be4f3318c25
  • Pointer size: 130 Bytes
  • Size of remote file: 24.1 kB
images_dataset/brick/patch_019.png CHANGED

Git LFS Details

  • SHA256: 49eb93490a0fab5e99b16939f5ffb62d619f0eb386e0baafe690b0695d28f9b6
  • Pointer size: 130 Bytes
  • Size of remote file: 24.7 kB
images_dataset/brick/patch_020.png CHANGED

Git LFS Details

  • SHA256: 27818894afdf9b59337c4650599d23e78feebbf5be611da7fe492391f61c0c68
  • Pointer size: 130 Bytes
  • Size of remote file: 23.6 kB
images_dataset/brick/patch_021.png CHANGED

Git LFS Details

  • SHA256: cfb6cd64bbdd97202c483433165b75d06f059fe66630bd927939b851ba1328ea
  • Pointer size: 130 Bytes
  • Size of remote file: 23.6 kB
images_dataset/brick/patch_022.png CHANGED

Git LFS Details

  • SHA256: de1d12a04d6497d3f6c86da8315ae1cb938ce0249887ef181415daa7ddb8c3d1
  • Pointer size: 130 Bytes
  • Size of remote file: 24.3 kB
images_dataset/brick/patch_023.png CHANGED

Git LFS Details

  • SHA256: 0fc0b951e76633f49d57bc6444746e29fa61332942efdaeb7236d3e24a4983ae
  • Pointer size: 130 Bytes
  • Size of remote file: 24.3 kB
images_dataset/brick/patch_024.png CHANGED

Git LFS Details

  • SHA256: 75a3ba113f07c7fb86bb888b3e913b6073fe102ef90597f18461fc3a5f9810cd
  • Pointer size: 130 Bytes
  • Size of remote file: 24.6 kB
images_dataset/brick/patch_025.png CHANGED

Git LFS Details

  • SHA256: bc6db51ab21eaf16f9609f16e0dc18b05465546bd3c241198f1669c154b4552c
  • Pointer size: 130 Bytes
  • Size of remote file: 24.1 kB
images_dataset/brick/patch_026.png CHANGED

Git LFS Details

  • SHA256: cf450fa03b68266914e7e6df467df6053bc1e6924d78eecfb53974b8f18ceda1
  • Pointer size: 130 Bytes
  • Size of remote file: 24 kB
images_dataset/brick/patch_027.png CHANGED

Git LFS Details

  • SHA256: 19939732e379e8895c8719cfdf1432207975a326f5dd224fb940c3542088d69d
  • Pointer size: 130 Bytes
  • Size of remote file: 24.2 kB
images_dataset/brick/patch_028.png CHANGED

Git LFS Details

  • SHA256: 55d609d687873ba1f30c0da89564e8bfe9fa1d6a634ceea85c8cc3a87970b171
  • Pointer size: 130 Bytes
  • Size of remote file: 23.2 kB
images_dataset/brick/patch_029.png CHANGED

Git LFS Details

  • SHA256: 7b98b135fec8d68e07be804421e60765a60c237dfac2e30dc5cbe9dbd5767ed6
  • Pointer size: 130 Bytes
  • Size of remote file: 23.3 kB
images_dataset/brick/patch_030.png CHANGED

Git LFS Details

  • SHA256: d677a7f5504b9c95b0e02cce9c5db80ae8fa3dcecf0f7f5e2145e44b397f1d25
  • Pointer size: 130 Bytes
  • Size of remote file: 22.4 kB
images_dataset/brick/patch_031.png CHANGED

Git LFS Details

  • SHA256: 25ebb3f106c751be7171bd9c1f5d1628caa204e447d628eefb11e27f01316119
  • Pointer size: 130 Bytes
  • Size of remote file: 24.4 kB
images_dataset/brick/patch_032.png CHANGED

Git LFS Details

  • SHA256: 2097e7a07d9bceb1e465512170871601a8d0f65b8fe12bc255b5b1207b3f88cb
  • Pointer size: 130 Bytes
  • Size of remote file: 24 kB
images_dataset/brick/patch_033.png CHANGED

Git LFS Details

  • SHA256: ec7aef87f74c5e92638e874ff2e2b52f0af6bafffbfe7f908321beab06a3bc9a
  • Pointer size: 130 Bytes
  • Size of remote file: 24.4 kB
images_dataset/brick/patch_034.png CHANGED

Git LFS Details

  • SHA256: da6a09fe6d4f34de65c78b04196444fb17d903175a1acba534aca98d96f0bf60
  • Pointer size: 130 Bytes
  • Size of remote file: 24.8 kB
images_dataset/brick/patch_035.png CHANGED

Git LFS Details

  • SHA256: 63511dde9f422461f4b3b01c5439cc82bdd9388337e9a1e8de667c1882f3cfbf
  • Pointer size: 130 Bytes
  • Size of remote file: 25.7 kB
images_dataset/brick/patch_036.png CHANGED

Git LFS Details

  • SHA256: 5600b3329b1c94542bb90b6a0a258dbb53ec75121f2a1534e18d1f2f69c27ac8
  • Pointer size: 130 Bytes
  • Size of remote file: 25.3 kB
images_dataset/brick/patch_037.png CHANGED

Git LFS Details

  • SHA256: db5c9eac27136576b67cc2f88dd9cb4c6502ad5cbf6dc5937c3e84cc6eca6760
  • Pointer size: 130 Bytes
  • Size of remote file: 24.3 kB
images_dataset/brick/patch_038.png CHANGED

Git LFS Details

  • SHA256: b4a1b742435098628a8183f0ffe4b5aaae807374c38855c29126204fa0a4d2ed
  • Pointer size: 130 Bytes
  • Size of remote file: 24.5 kB
images_dataset/brick/patch_039.png CHANGED

Git LFS Details

  • SHA256: 43652719db66d5d2f5bcdc7825e0b96a008bcac2213b82ae9da6802c225282d8
  • Pointer size: 130 Bytes
  • Size of remote file: 23.4 kB
images_dataset/brick/patch_040.png CHANGED

Git LFS Details

  • SHA256: 9cb4cdfadb457217e1fdf7329143504e1acb087f481e3ca6a5a0fc584fb0be52
  • Pointer size: 130 Bytes
  • Size of remote file: 23.7 kB
images_dataset/brick/patch_041.png CHANGED

Git LFS Details

  • SHA256: 4cee50bc057ec7f8cacb7244ccfe29b9a6026d450fac0d593e354e9d06bedc7f
  • Pointer size: 130 Bytes
  • Size of remote file: 24.6 kB
images_dataset/brick/patch_042.png CHANGED

Git LFS Details

  • SHA256: fff29154d9f1de0acf4708a29ba69cbf9e67d38fdef50920a0c770eece4f6ab4
  • Pointer size: 130 Bytes
  • Size of remote file: 24.4 kB
images_dataset/brick/patch_043.png CHANGED

Git LFS Details

  • SHA256: a1cc4b78e66780b064e3a7292d696cb70fde8707c7dafac7f4b2f7f09e3e899c
  • Pointer size: 130 Bytes
  • Size of remote file: 24.3 kB
images_dataset/brick/patch_044.png CHANGED

Git LFS Details

  • SHA256: edbacf4e2018abc9ba797479f3f74c611acff22df4b9e49903b8ce4a8432a4e8
  • Pointer size: 130 Bytes
  • Size of remote file: 24.3 kB
images_dataset/brick/patch_045.png CHANGED

Git LFS Details

  • SHA256: 6b3f6918004fc8af19c6a47f9108179280374970b78f1a99e3c4c8e909c3c734
  • Pointer size: 130 Bytes
  • Size of remote file: 24.3 kB
images_dataset/brick/patch_046.png CHANGED

Git LFS Details

  • SHA256: 58602351ca76b03d3929e1f7c862549bb8fdb20ff49075cede0a11ff5b14392a
  • Pointer size: 130 Bytes
  • Size of remote file: 24.7 kB
images_dataset/brick/patch_047.png CHANGED

Git LFS Details

  • SHA256: 53ca45888a30e0b6941908ab451ce6374f1205d74c5be234f28ec8d81bdb4a2f
  • Pointer size: 130 Bytes
  • Size of remote file: 23.7 kB
images_dataset/brick/patch_048.png CHANGED

Git LFS Details

  • SHA256: e6dc28f2c47173341c6ec02971d1e7409d9d3f4fdbbdeb76013990e92c8ba929
  • Pointer size: 130 Bytes
  • Size of remote file: 24 kB
images_dataset/brick/patch_049.png CHANGED

Git LFS Details

  • SHA256: b3d1110d927b19abcb2b0858d81d0422e01a29ec87960d863e1a976eecf85897
  • Pointer size: 130 Bytes
  • Size of remote file: 23 kB