ooferdoodles commited on
Commit
242ebc0
1 Parent(s): cb53dbd
Files changed (2) hide show
  1. app.py +28 -5
  2. changechip.py +2 -42
app.py CHANGED
@@ -1,16 +1,26 @@
1
- import os
2
  import gradio as gr
3
 
4
  from changechip import *
5
 
6
- app_port = os.getenv("APP_PORT", "7860")
7
 
8
-
9
- def process(input_image, reference_image, resize_factor, output_alpha):
 
 
 
 
 
 
 
 
10
  return pipeline(
11
  (input_image, reference_image),
12
  resize_factor=resize_factor,
13
  output_alpha=output_alpha,
 
 
 
 
14
  )
15
 
16
 
@@ -29,6 +39,10 @@ with gr.Blocks() as demo:
29
  with gr.Accordion(label="Other Options", open=False):
30
  resize_factor = gr.Slider(0.1, 1, 0.5, step=0.1, label="Resize Factor")
31
  output_alpha = gr.Slider(0, 255, 50, step=1, label="Output Alpha")
 
 
 
 
32
 
33
  with gr.Column(scale=2):
34
  output_image = gr.Image(label="Output Image", scale=9)
@@ -36,7 +50,16 @@ with gr.Blocks() as demo:
36
 
37
  btn.click(
38
  fn=process,
39
- inputs=[input_image, reference_image, resize_factor, output_alpha],
 
 
 
 
 
 
 
 
 
40
  outputs=output_image,
41
  )
42
 
 
 
1
  import gradio as gr
2
 
3
  from changechip import *
4
 
 
5
 
6
+ def process(
7
+ input_image,
8
+ reference_image,
9
+ resize_factor,
10
+ output_alpha,
11
+ window_size,
12
+ clusters,
13
+ pca_dim_gray,
14
+ pca_dim_rgb,
15
+ ):
16
  return pipeline(
17
  (input_image, reference_image),
18
  resize_factor=resize_factor,
19
  output_alpha=output_alpha,
20
+ window_size=window_size,
21
+ clusters=clusters,
22
+ pca_dim_gray=pca_dim_gray,
23
+ pca_dim_rgb=pca_dim_rgb,
24
  )
25
 
26
 
 
39
  with gr.Accordion(label="Other Options", open=False):
40
  resize_factor = gr.Slider(0.1, 1, 0.5, step=0.1, label="Resize Factor")
41
  output_alpha = gr.Slider(0, 255, 50, step=1, label="Output Alpha")
42
+ window_size = gr.Slider(0, 10, 5, step=1, label="Window Size")
43
+ clusters = gr.Slider(0, 32, 16, step=1, label="Clusters")
44
+ pca_dim_gray = gr.Slider(0, 9, 3, step=1, label="PCA Dim Gray")
45
+ pca_dim_rgb = gr.Slider(0, 27, 9, step=1, label="PCA Dim RGB")
46
 
47
  with gr.Column(scale=2):
48
  output_image = gr.Image(label="Output Image", scale=9)
 
50
 
51
  btn.click(
52
  fn=process,
53
+ inputs=[
54
+ input_image,
55
+ reference_image,
56
+ resize_factor,
57
+ output_alpha,
58
+ window_size,
59
+ clusters,
60
+ pca_dim_gray,
61
+ pca_dim_rgb,
62
+ ],
63
  outputs=output_image,
64
  )
65
 
changechip.py CHANGED
@@ -16,19 +16,15 @@ import time
16
  def resize_images(images, resize_factor=1.0):
17
  """
18
  Resizes the input and reference images based on the average dimensions of the two images and a resize factor.
19
-
20
  Parameters:
21
  images (tuple): A tuple containing two images (input_image, reference_image). Both images should be numpy arrays.
22
  resize_factor (float): A factor by which to resize the images. Default is 1.0, which means the images will be resized to the average dimensions of the two images.
23
-
24
  Returns:
25
  tuple: A tuple containing the resized input and reference images.
26
-
27
  Example:
28
  >>> input_image = cv2.imread('input.jpg')
29
  >>> reference_image = cv2.imread('reference.jpg')
30
  >>> resized_images = resize_images((input_image, reference_image), resize_factor=0.5)
31
-
32
  """
33
  input_image, reference_image = images
34
  average_width = (input_image.shape[1] + reference_image.shape[1]) * 0.5
@@ -49,12 +45,10 @@ def resize_images(images, resize_factor=1.0):
49
  def homography(images, debug=False, output_directory=None):
50
  """
51
  Apply homography transformation to align two images.
52
-
53
  Args:
54
  images (tuple): A tuple containing two images, where the first image is the input image and the second image is the reference image.
55
  debug (bool, optional): If True, debug images will be generated. Defaults to False.
56
  output_directory (str, optional): The directory to save the debug images. Defaults to None.
57
-
58
  Returns:
59
  tuple: A tuple containing the aligned input image and the reference image.
60
  """
@@ -130,12 +124,10 @@ def homography(images, debug=False, output_directory=None):
130
  def histogram_matching(images, debug=False, output_directory=None):
131
  """
132
  Perform histogram matching between an input image and a reference image.
133
-
134
  Args:
135
  images (tuple): A tuple containing the input image and the reference image.
136
  debug (bool, optional): If True, save the histogram-matched image to the output directory. Defaults to False.
137
  output_directory (str, optional): The directory to save the histogram-matched image. Defaults to None.
138
-
139
  Returns:
140
  tuple: A tuple containing the input image and the histogram-matched reference image.
141
  """
@@ -161,16 +153,13 @@ def preprocess_images(images, resize_factor=1.0, debug=False, output_directory=N
161
  1. Resizes the images based on the given resize factor.
162
  2. Applies homography to align the resized images.
163
  3. Performs histogram matching on the aligned images.
164
-
165
  Args:
166
  images (tuple): A tuple containing the input image and the reference image.
167
  resize_factor (float, optional): The factor by which to resize the images. Defaults to 1.0.
168
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
169
  output_directory (str, optional): The directory to save the output images. Defaults to None.
170
-
171
  Returns:
172
  tuple: The preprocessed images.
173
-
174
  Example:
175
  >>> images = (input_image, reference_image)
176
  >>> preprocess_images(images, resize_factor=0.5, debug=True, output_directory='output/')
@@ -193,12 +182,10 @@ def preprocess_images(images, resize_factor=1.0, debug=False, output_directory=N
193
  def find_vector_set(descriptors, jump_size, shape):
194
  """
195
  Find the vector set from the given descriptors.
196
-
197
  Args:
198
  descriptors (numpy.ndarray): The input descriptors.
199
  jump_size (int): The jump size for sampling the descriptors.
200
  shape (tuple): The shape of the descriptors.
201
-
202
  Returns:
203
  tuple: A tuple containing the vector set and the mean vector.
204
  """
@@ -219,15 +206,12 @@ def find_FVS(descriptors, EVS, mean_vec):
219
  """
220
  Calculate the feature vector space (FVS) by performing dot product of descriptors and EVS,
221
  and subtracting the mean vector from the result.
222
-
223
  Args:
224
  descriptors (numpy.ndarray): Array of descriptors.
225
  EVS (numpy.ndarray): Eigenvalue matrix.
226
  mean_vec (numpy.ndarray): Mean vector.
227
-
228
  Returns:
229
  numpy.ndarray: The calculated feature vector space (FVS).
230
-
231
  """
232
  FVS = np.dot(descriptors, EVS)
233
  FVS = FVS - mean_vec
@@ -240,13 +224,11 @@ def find_FVS(descriptors, EVS, mean_vec):
240
  def descriptors_to_pca(descriptors, pca_target_dim, window_size, shape):
241
  """
242
  Applies Principal Component Analysis (PCA) to a set of descriptors.
243
-
244
  Args:
245
  descriptors (list): List of descriptors.
246
  pca_target_dim (int): Target dimensionality for PCA.
247
  window_size (int): Size of the sliding window.
248
  shape (tuple): Shape of the descriptors.
249
-
250
  Returns:
251
  list: Feature vector set after applying PCA.
252
  """
@@ -269,7 +251,6 @@ def get_descriptors(
269
  ):
270
  """
271
  Compute descriptors for input images using sliding window technique and PCA.
272
-
273
  Args:
274
  images (tuple): A tuple containing the input image and reference image.
275
  window_size (int): The size of the sliding window.
@@ -277,10 +258,8 @@ def get_descriptors(
277
  pca_dim_rgb (int): The number of dimensions to keep for RGB PCA.
278
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
279
  output_directory (str, optional): The directory to save debug images. Required if debug is True.
280
-
281
  Returns:
282
  numpy.ndarray: The computed descriptors.
283
-
284
  Raises:
285
  AssertionError: If debug is True but output_directory is not provided.
286
  """
@@ -379,15 +358,12 @@ def get_descriptors(
379
  def k_means_clustering(FVS, components, image_shape):
380
  """
381
  Perform K-means clustering on the given feature vectors.
382
-
383
  Args:
384
  FVS (array-like): The feature vectors to be clustered.
385
  components (int): The number of clusters (components) to create.
386
  image_shape (tuple): The size of the images used to reshape the change map.
387
-
388
  Returns:
389
  array-like: The change map obtained from the K-means clustering.
390
-
391
  """
392
  kmeans = KMeans(components, verbose=0)
393
  kmeans.fit(FVS)
@@ -399,16 +375,13 @@ def k_means_clustering(FVS, components, image_shape):
399
  def clustering_to_mse_values(change_map, input_image, reference_image, n):
400
  """
401
  Compute the normalized mean squared error (MSE) values for each cluster in a change map.
402
-
403
  Args:
404
  change_map (numpy.ndarray): Array representing the cluster labels for each pixel in the change map.
405
  input_image (numpy.ndarray): Array representing the input image.
406
  reference_image (numpy.ndarray): Array representing the reference image.
407
  n (int): Number of clusters.
408
-
409
  Returns:
410
  list: Normalized MSE values for each cluster.
411
-
412
  """
413
 
414
  # Ensure the images are in integer format for calculations
@@ -446,7 +419,6 @@ def compute_change_map(
446
  ):
447
  """
448
  Compute the change map and mean squared error (MSE) array for a pair of input and reference images.
449
-
450
  Args:
451
  images (tuple): A tuple containing the input and reference images.
452
  window_size (int): The size of the sliding window for feature extraction.
@@ -455,13 +427,10 @@ def compute_change_map(
455
  pca_dim_rgb (int): The number of dimensions to reduce to for RGB images.
456
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
457
  output_directory (str, optional): The directory to save the output files. Required if debug mode is enabled.
458
-
459
  Returns:
460
  tuple: A tuple containing the change map and MSE array.
461
-
462
  Raises:
463
  AssertionError: If debug mode is enabled but output_directory is not provided.
464
-
465
  """
466
  input_image, reference_image = images
467
  descriptors = get_descriptors(
@@ -537,12 +506,10 @@ def find_group_of_accepted_classes_DBSCAN(
537
  ):
538
  """
539
  Finds the group of accepted classes using the DBSCAN algorithm.
540
-
541
  Parameters:
542
  - MSE_array (list): A list of mean squared error values.
543
  - debug (bool): Flag indicating whether to enable debug mode or not. Default is False.
544
  - output_directory (str): The directory where the output files will be saved. Default is None.
545
-
546
  Returns:
547
  - accepted_classes (list): A list of indices of the accepted classes.
548
  """
@@ -551,7 +518,7 @@ def find_group_of_accepted_classes_DBSCAN(
551
  number_of_clusters = len(set(clustering.labels_))
552
  if number_of_clusters == 1:
553
  print("No significant changes are detected.")
554
-
555
  # print(clustering.labels_)
556
  classes = [[] for _ in range(number_of_clusters)]
557
  centers = np.zeros(number_of_clusters)
@@ -595,13 +562,11 @@ def draw_combination_on_transparent_input_image(
595
  ):
596
  """
597
  Draws a combination of classes on a transparent input image based on their mean squared error (MSE) order.
598
-
599
  Args:
600
  classes_mse (numpy.ndarray): Array of mean squared errors for each class.
601
  clustering (dict): Dictionary containing the clustering information for each class.
602
  combination (list): List of classes to be drawn on the image.
603
  transparent_input_image (numpy.ndarray): Transparent input image.
604
-
605
  Returns:
606
  numpy.ndarray: Transparent input image with the specified combination of classes drawn on it.
607
  """
@@ -633,7 +598,6 @@ def detect_changes(
633
  ):
634
  """
635
  Detects changes between two images using a combination of clustering and image processing techniques.
636
-
637
  Args:
638
  images (tuple): A tuple containing two input images.
639
  output_alpha (int): The alpha value for the output image.
@@ -643,10 +607,8 @@ def detect_changes(
643
  pca_dim_rgb (int): The number of dimensions to reduce the RGB image to using PCA.
644
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
645
  output_directory (str, optional): The output directory for saving intermediate results. Defaults to None.
646
-
647
  Returns:
648
  numpy.ndarray: The resulting image with detected changes.
649
-
650
  """
651
  start_time = time.time()
652
  input_image, _ = images
@@ -699,7 +661,6 @@ def pipeline(
699
  ):
700
  """
701
  Applies a pipeline of image processing steps to detect changes in a sequence of images.
702
-
703
  Args:
704
  images (tuple): A list of input images.
705
  resize_factor (float, optional): The factor by which to resize the images. Defaults to 1.0.
@@ -710,7 +671,6 @@ def pipeline(
710
  pca_dim_rgb (int, optional): The number of dimensions to keep for RGB PCA. Defaults to 9.
711
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
712
  output_directory (str, optional): The directory to save the output images. Defaults to None.
713
-
714
  Returns:
715
  numpy.ndarray: The resulting image with detected changes.
716
  """
@@ -734,4 +694,4 @@ def pipeline(
734
  output_directory=output_directory,
735
  )
736
 
737
- return result
 
16
  def resize_images(images, resize_factor=1.0):
17
  """
18
  Resizes the input and reference images based on the average dimensions of the two images and a resize factor.
 
19
  Parameters:
20
  images (tuple): A tuple containing two images (input_image, reference_image). Both images should be numpy arrays.
21
  resize_factor (float): A factor by which to resize the images. Default is 1.0, which means the images will be resized to the average dimensions of the two images.
 
22
  Returns:
23
  tuple: A tuple containing the resized input and reference images.
 
24
  Example:
25
  >>> input_image = cv2.imread('input.jpg')
26
  >>> reference_image = cv2.imread('reference.jpg')
27
  >>> resized_images = resize_images((input_image, reference_image), resize_factor=0.5)
 
28
  """
29
  input_image, reference_image = images
30
  average_width = (input_image.shape[1] + reference_image.shape[1]) * 0.5
 
45
  def homography(images, debug=False, output_directory=None):
46
  """
47
  Apply homography transformation to align two images.
 
48
  Args:
49
  images (tuple): A tuple containing two images, where the first image is the input image and the second image is the reference image.
50
  debug (bool, optional): If True, debug images will be generated. Defaults to False.
51
  output_directory (str, optional): The directory to save the debug images. Defaults to None.
 
52
  Returns:
53
  tuple: A tuple containing the aligned input image and the reference image.
54
  """
 
124
  def histogram_matching(images, debug=False, output_directory=None):
125
  """
126
  Perform histogram matching between an input image and a reference image.
 
127
  Args:
128
  images (tuple): A tuple containing the input image and the reference image.
129
  debug (bool, optional): If True, save the histogram-matched image to the output directory. Defaults to False.
130
  output_directory (str, optional): The directory to save the histogram-matched image. Defaults to None.
 
131
  Returns:
132
  tuple: A tuple containing the input image and the histogram-matched reference image.
133
  """
 
153
  1. Resizes the images based on the given resize factor.
154
  2. Applies homography to align the resized images.
155
  3. Performs histogram matching on the aligned images.
 
156
  Args:
157
  images (tuple): A tuple containing the input image and the reference image.
158
  resize_factor (float, optional): The factor by which to resize the images. Defaults to 1.0.
159
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
160
  output_directory (str, optional): The directory to save the output images. Defaults to None.
 
161
  Returns:
162
  tuple: The preprocessed images.
 
163
  Example:
164
  >>> images = (input_image, reference_image)
165
  >>> preprocess_images(images, resize_factor=0.5, debug=True, output_directory='output/')
 
182
  def find_vector_set(descriptors, jump_size, shape):
183
  """
184
  Find the vector set from the given descriptors.
 
185
  Args:
186
  descriptors (numpy.ndarray): The input descriptors.
187
  jump_size (int): The jump size for sampling the descriptors.
188
  shape (tuple): The shape of the descriptors.
 
189
  Returns:
190
  tuple: A tuple containing the vector set and the mean vector.
191
  """
 
206
  """
207
  Calculate the feature vector space (FVS) by performing dot product of descriptors and EVS,
208
  and subtracting the mean vector from the result.
 
209
  Args:
210
  descriptors (numpy.ndarray): Array of descriptors.
211
  EVS (numpy.ndarray): Eigenvalue matrix.
212
  mean_vec (numpy.ndarray): Mean vector.
 
213
  Returns:
214
  numpy.ndarray: The calculated feature vector space (FVS).
 
215
  """
216
  FVS = np.dot(descriptors, EVS)
217
  FVS = FVS - mean_vec
 
224
  def descriptors_to_pca(descriptors, pca_target_dim, window_size, shape):
225
  """
226
  Applies Principal Component Analysis (PCA) to a set of descriptors.
 
227
  Args:
228
  descriptors (list): List of descriptors.
229
  pca_target_dim (int): Target dimensionality for PCA.
230
  window_size (int): Size of the sliding window.
231
  shape (tuple): Shape of the descriptors.
 
232
  Returns:
233
  list: Feature vector set after applying PCA.
234
  """
 
251
  ):
252
  """
253
  Compute descriptors for input images using sliding window technique and PCA.
 
254
  Args:
255
  images (tuple): A tuple containing the input image and reference image.
256
  window_size (int): The size of the sliding window.
 
258
  pca_dim_rgb (int): The number of dimensions to keep for RGB PCA.
259
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
260
  output_directory (str, optional): The directory to save debug images. Required if debug is True.
 
261
  Returns:
262
  numpy.ndarray: The computed descriptors.
 
263
  Raises:
264
  AssertionError: If debug is True but output_directory is not provided.
265
  """
 
358
  def k_means_clustering(FVS, components, image_shape):
359
  """
360
  Perform K-means clustering on the given feature vectors.
 
361
  Args:
362
  FVS (array-like): The feature vectors to be clustered.
363
  components (int): The number of clusters (components) to create.
364
  image_shape (tuple): The size of the images used to reshape the change map.
 
365
  Returns:
366
  array-like: The change map obtained from the K-means clustering.
 
367
  """
368
  kmeans = KMeans(components, verbose=0)
369
  kmeans.fit(FVS)
 
375
  def clustering_to_mse_values(change_map, input_image, reference_image, n):
376
  """
377
  Compute the normalized mean squared error (MSE) values for each cluster in a change map.
 
378
  Args:
379
  change_map (numpy.ndarray): Array representing the cluster labels for each pixel in the change map.
380
  input_image (numpy.ndarray): Array representing the input image.
381
  reference_image (numpy.ndarray): Array representing the reference image.
382
  n (int): Number of clusters.
 
383
  Returns:
384
  list: Normalized MSE values for each cluster.
 
385
  """
386
 
387
  # Ensure the images are in integer format for calculations
 
419
  ):
420
  """
421
  Compute the change map and mean squared error (MSE) array for a pair of input and reference images.
 
422
  Args:
423
  images (tuple): A tuple containing the input and reference images.
424
  window_size (int): The size of the sliding window for feature extraction.
 
427
  pca_dim_rgb (int): The number of dimensions to reduce to for RGB images.
428
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
429
  output_directory (str, optional): The directory to save the output files. Required if debug mode is enabled.
 
430
  Returns:
431
  tuple: A tuple containing the change map and MSE array.
 
432
  Raises:
433
  AssertionError: If debug mode is enabled but output_directory is not provided.
 
434
  """
435
  input_image, reference_image = images
436
  descriptors = get_descriptors(
 
506
  ):
507
  """
508
  Finds the group of accepted classes using the DBSCAN algorithm.
 
509
  Parameters:
510
  - MSE_array (list): A list of mean squared error values.
511
  - debug (bool): Flag indicating whether to enable debug mode or not. Default is False.
512
  - output_directory (str): The directory where the output files will be saved. Default is None.
 
513
  Returns:
514
  - accepted_classes (list): A list of indices of the accepted classes.
515
  """
 
518
  number_of_clusters = len(set(clustering.labels_))
519
  if number_of_clusters == 1:
520
  print("No significant changes are detected.")
521
+
522
  # print(clustering.labels_)
523
  classes = [[] for _ in range(number_of_clusters)]
524
  centers = np.zeros(number_of_clusters)
 
562
  ):
563
  """
564
  Draws a combination of classes on a transparent input image based on their mean squared error (MSE) order.
 
565
  Args:
566
  classes_mse (numpy.ndarray): Array of mean squared errors for each class.
567
  clustering (dict): Dictionary containing the clustering information for each class.
568
  combination (list): List of classes to be drawn on the image.
569
  transparent_input_image (numpy.ndarray): Transparent input image.
 
570
  Returns:
571
  numpy.ndarray: Transparent input image with the specified combination of classes drawn on it.
572
  """
 
598
  ):
599
  """
600
  Detects changes between two images using a combination of clustering and image processing techniques.
 
601
  Args:
602
  images (tuple): A tuple containing two input images.
603
  output_alpha (int): The alpha value for the output image.
 
607
  pca_dim_rgb (int): The number of dimensions to reduce the RGB image to using PCA.
608
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
609
  output_directory (str, optional): The output directory for saving intermediate results. Defaults to None.
 
610
  Returns:
611
  numpy.ndarray: The resulting image with detected changes.
 
612
  """
613
  start_time = time.time()
614
  input_image, _ = images
 
661
  ):
662
  """
663
  Applies a pipeline of image processing steps to detect changes in a sequence of images.
 
664
  Args:
665
  images (tuple): A list of input images.
666
  resize_factor (float, optional): The factor by which to resize the images. Defaults to 1.0.
 
671
  pca_dim_rgb (int, optional): The number of dimensions to keep for RGB PCA. Defaults to 9.
672
  debug (bool, optional): Whether to enable debug mode. Defaults to False.
673
  output_directory (str, optional): The directory to save the output images. Defaults to None.
 
674
  Returns:
675
  numpy.ndarray: The resulting image with detected changes.
676
  """
 
694
  output_directory=output_directory,
695
  )
696
 
697
+ return result