carlosriverat commited on
Commit
24647ae
·
verified ·
1 Parent(s): 176f783

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -10,7 +10,7 @@ def preprocess_image(image, blur_value):
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)
@@ -23,8 +23,8 @@ def compare_images(image1, image2, blur_value, technique):
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,10 +39,15 @@ def compare_images(image1, image2, blur_value, technique):
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,
@@ -50,14 +55,18 @@ demo = gr.Interface(
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()
 
10
  blurred = cv2.GaussianBlur(gray, (blur_value, blur_value), 0)
11
  return blurred
12
 
13
+ def compare_images(image1, image2, blur_value, technique, threshold_value):
14
  # Preprocess images
15
  gray1 = preprocess_image(image1, blur_value)
16
  gray2 = preprocess_image(image2, blur_value)
 
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: # Simple Binary
27
+ _, thresh = cv2.threshold(diff, threshold_value, 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
+ # Overlay the difference in magenta over the first image
43
+ diff_colored = cv2.cvtColor(diff, cv2.COLOR_GRAY2BGR)
44
+ diff_colored[:, :, 1:] = 0 # Keep only red channel for magenta effect
45
+ overlayed = cv2.addWeighted(image1, 0.7, diff_colored, 0.3, 0)
46
 
47
+ return highlighted, overlayed
48
+
49
+ def update_threshold_visibility(technique):
50
+ return gr.update(visible=(technique == "Simple Binary"))
51
 
52
  demo = gr.Interface(
53
  fn=compare_images,
 
55
  gr.Image(type="numpy", label="Image Without Object"),
56
  gr.Image(type="numpy", label="Image With Object"),
57
  gr.Slider(minimum=1, maximum=15, step=2, value=5, label="Gaussian Blur"),
58
+ gr.Dropdown(["Adaptive Threshold", "Otsu's Threshold", "Simple Binary"], label="Thresholding Technique", value="Adaptive Threshold", interactive=True),
59
+ gr.Slider(minimum=0, maximum=255, step=1, value=50, label="Threshold Value", visible=False)
60
  ],
61
  outputs=[
62
  gr.Image(type="numpy", label="Highlighted Differences"),
63
+ gr.Image(type="numpy", label="Raw Difference Overlay (Magenta)")
64
  ],
65
  title="Object Difference Highlighter",
66
+ 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 overlayed on the original image.",
67
+ live=True
68
  )
69
 
70
+ demo.load(update_threshold_visibility, inputs=["Thresholding Technique"], outputs=["Threshold Value"])
71
+
72
  demo.launch()