nnibras commited on
Commit
1dc1ab5
·
verified ·
1 Parent(s): 7b46d95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -47,10 +47,39 @@ def load_and_convert_images(directory):
47
  dataset.append(resized_image)
48
  return dataset
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # Load datasets using glob
51
  grass_dataset = load_and_convert_images(grass_dir)
52
  wood_dataset = load_and_convert_images(wood_dir)
53
 
 
 
 
 
 
 
 
 
54
  def calc_glcm_features(images):
55
  """Calculate GLCM features for a list of images.
56
 
@@ -90,10 +119,6 @@ def extract_lbp_features(images):
90
  return features
91
 
92
  # Train classifiers (for example purposes, using KNN)
93
- X = np.concatenate([grass_dataset, wood_dataset])
94
- y = np.concatenate([np.zeros(len(grass_dataset)), np.ones(len(wood_dataset))]) # Labels: 0 for Grass, 1 for Wood
95
-
96
- # Extract GLCM and LBP features
97
  glcm_features = calc_glcm_features(X)
98
  lbp_features = extract_lbp_features(X)
99
 
 
47
  dataset.append(resized_image)
48
  return dataset
49
 
50
+ # Ensure that both datasets contain 2D arrays (grayscale images)
51
+ def check_and_reshape(dataset):
52
+ """Ensure all images in the dataset are 2D arrays.
53
+
54
+ Args:
55
+ dataset (list): A list of images.
56
+
57
+ Returns:
58
+ list: A list of reshaped images to ensure they are all 2D.
59
+ """
60
+ reshaped_dataset = []
61
+ for img in dataset:
62
+ if img.ndim == 3: # If the image has 3 dimensions (like RGB), convert it to grayscale
63
+ gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
64
+ reshaped_dataset.append(gray_img)
65
+ elif img.ndim == 2: # If it's already grayscale (2D), just append it
66
+ reshaped_dataset.append(img)
67
+ else:
68
+ raise ValueError("Unexpected image dimension: {}".format(img.ndim))
69
+ return reshaped_dataset
70
+
71
  # Load datasets using glob
72
  grass_dataset = load_and_convert_images(grass_dir)
73
  wood_dataset = load_and_convert_images(wood_dir)
74
 
75
+ # Apply the reshaping to both datasets
76
+ grass_dataset = check_and_reshape(grass_dataset)
77
+ wood_dataset = check_and_reshape(wood_dataset)
78
+
79
+ # Now concatenate the datasets
80
+ X = np.concatenate([grass_dataset, wood_dataset])
81
+ y = np.concatenate([np.zeros(len(grass_dataset)), np.ones(len(wood_dataset))]) # Labels: 0 for Grass, 1 for Wood
82
+
83
  def calc_glcm_features(images):
84
  """Calculate GLCM features for a list of images.
85
 
 
119
  return features
120
 
121
  # Train classifiers (for example purposes, using KNN)
 
 
 
 
122
  glcm_features = calc_glcm_features(X)
123
  lbp_features = extract_lbp_features(X)
124