carlosriverat commited on
Commit
176f783
·
verified ·
1 Parent(s): 9e601d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -3,24 +3,28 @@ import cv2
3
  import numpy as np
4
  from skimage.metrics import structural_similarity as ssim
5
 
6
- def preprocess_image(image):
7
  # Convert to grayscale
8
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
  # Apply Gaussian blur to reduce noise
10
- blurred = cv2.GaussianBlur(gray, (5, 5), 0)
11
  return blurred
12
 
13
- def compare_images(image1, image2):
14
  # Preprocess images
15
- gray1 = preprocess_image(image1)
16
- gray2 = preprocess_image(image2)
17
 
18
  # Compute SSIM between the two images
19
  score, diff = ssim(gray1, gray2, full=True)
20
  diff = (diff * 255).astype("uint8")
21
 
22
- # Apply adaptive thresholding for better object isolation
23
- _, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY_INV)
 
 
 
 
24
 
25
  # Find contours of differences
26
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
@@ -35,17 +39,25 @@ def compare_images(image1, image2):
35
  # Apply the mask to highlight the object added in the second image
36
  highlighted = cv2.bitwise_and(image2, mask)
37
 
38
- return highlighted
 
 
 
39
 
40
  demo = gr.Interface(
41
  fn=compare_images,
42
  inputs=[
43
  gr.Image(type="numpy", label="Image Without Object"),
44
- gr.Image(type="numpy", label="Image With Object")
 
 
 
 
 
 
45
  ],
46
- outputs=gr.Image(type="numpy", label="Highlighted Differences"),
47
  title="Object Difference Highlighter",
48
- description="Upload two images: one without an object and one with an object. The app will highlight only the newly added object."
49
  )
50
 
51
  demo.launch()
 
3
  import numpy as np
4
  from skimage.metrics import structural_similarity as ssim
5
 
6
+ def preprocess_image(image, blur_value):
7
  # Convert to grayscale
8
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
  # Apply Gaussian blur to reduce noise
10
+ blurred = cv2.GaussianBlur(gray, (blur_value, blur_value), 0)
11
  return blurred
12
 
13
+ def compare_images(image1, image2, blur_value, technique):
14
  # Preprocess images
15
+ gray1 = preprocess_image(image1, blur_value)
16
+ gray2 = preprocess_image(image2, blur_value)
17
 
18
  # Compute SSIM between the two images
19
  score, diff = ssim(gray1, gray2, full=True)
20
  diff = (diff * 255).astype("uint8")
21
 
22
+ if technique == "Adaptive Threshold":
23
+ _, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY_INV)
24
+ elif technique == "Otsu's Threshold":
25
+ _, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
26
+ else: # Default to simple binary threshold
27
+ _, thresh = cv2.threshold(diff, 50, 255, cv2.THRESH_BINARY)
28
 
29
  # Find contours of differences
30
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
39
  # Apply the mask to highlight the object added in the second image
40
  highlighted = cv2.bitwise_and(image2, mask)
41
 
42
+ # Show the raw difference in magenta
43
+ diff_colored = cv2.merge([np.zeros_like(diff), diff, diff])
44
+
45
+ return highlighted, diff_colored
46
 
47
  demo = gr.Interface(
48
  fn=compare_images,
49
  inputs=[
50
  gr.Image(type="numpy", label="Image Without Object"),
51
+ gr.Image(type="numpy", label="Image With Object"),
52
+ gr.Slider(minimum=1, maximum=15, step=2, value=5, label="Gaussian Blur"),
53
+ gr.Dropdown(["Adaptive Threshold", "Otsu's Threshold", "Simple Binary"], label="Thresholding Technique", value="Adaptive Threshold")
54
+ ],
55
+ outputs=[
56
+ gr.Image(type="numpy", label="Highlighted Differences"),
57
+ gr.Image(type="numpy", label="Raw Difference (Magenta)")
58
  ],
 
59
  title="Object Difference Highlighter",
60
+ description="Upload two images: one without an object and one with an object. The app will highlight only the newly added object and show the real differences in magenta."
61
  )
62
 
63
  demo.launch()