Tanzeer commited on
Commit
3489552
·
1 Parent(s): cb374b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -14,6 +14,9 @@ model.load_state_dict(torch.load('pretrained_models/TranSalNet_Res.pth', map_loc
14
  model.to(device)
15
  model.eval()
16
 
 
 
 
17
  def count_and_label_red_patches(heatmap, threshold=200):
18
  red_mask = heatmap[:, :, 2] > threshold
19
  contours, _ = cv2.findContours(red_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
@@ -23,13 +26,7 @@ def count_and_label_red_patches(heatmap, threshold=200):
23
 
24
  original_image = np.array(image)
25
 
26
- # Find the centroid of the red spot with the highest area
27
- M_largest = cv2.moments(contours[0])
28
- if M_largest["m00"] != 0:
29
- cX_largest = int(M_largest["m10"] / M_largest["m00"])
30
- cY_largest = int(M_largest["m01"] / M_largest["m00"])
31
- else:
32
- cX_largest, cY_largest = 0, 0
33
 
34
  for i, contour in enumerate(contours, start=1):
35
  # Compute the centroid of the current contour
@@ -44,18 +41,24 @@ def count_and_label_red_patches(heatmap, threshold=200):
44
  circle_color = (0, 0, 0) # Blue color
45
  cv2.circle(original_image, (cX, cY), radius, circle_color, -1) # Draw blue circle
46
 
47
- # Connect the current red spot to the red spot with the highest area
48
- line_color = (0, 0, 0) # Red color
49
- cv2.line(original_image, (cX, cY), (cX_largest, cY_largest), line_color, 2)
50
-
51
  font = cv2.FONT_HERSHEY_SIMPLEX
52
  font_scale = 1
53
  font_color = (255, 255, 255)
54
  line_type = cv2.LINE_AA
55
  cv2.putText(original_image, str(i), (cX - 10, cY + 10), font, font_scale, font_color, 2, line_type)
 
 
 
 
 
 
 
 
 
56
 
57
  return original_image, len(contours)
58
 
 
59
  st.title('Saliency Detection App')
60
  st.write('Upload an image for saliency detection:')
61
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
@@ -95,6 +98,6 @@ if uploaded_image:
95
 
96
  st.image(blended_img, caption='Blended Image', use_column_width=True, channels='BGR')
97
 
98
- # Create a dir with name example to save
99
  cv2.imwrite('example/result15.png', blended_img, [int(cv2.IMWRITE_JPEG_QUALITY), 200])
100
  st.success('Saliency detection complete. Result saved as "example/result15.png".')
 
14
  model.to(device)
15
  model.eval()
16
 
17
+ import cv2
18
+ import numpy as np
19
+
20
  def count_and_label_red_patches(heatmap, threshold=200):
21
  red_mask = heatmap[:, :, 2] > threshold
22
  contours, _ = cv2.findContours(red_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
26
 
27
  original_image = np.array(image)
28
 
29
+ centroid_list = [] # List to store the centroids of the contours in order
 
 
 
 
 
 
30
 
31
  for i, contour in enumerate(contours, start=1):
32
  # Compute the centroid of the current contour
 
41
  circle_color = (0, 0, 0) # Blue color
42
  cv2.circle(original_image, (cX, cY), radius, circle_color, -1) # Draw blue circle
43
 
 
 
 
 
44
  font = cv2.FONT_HERSHEY_SIMPLEX
45
  font_scale = 1
46
  font_color = (255, 255, 255)
47
  line_type = cv2.LINE_AA
48
  cv2.putText(original_image, str(i), (cX - 10, cY + 10), font, font_scale, font_color, 2, line_type)
49
+
50
+ centroid_list.append((cX, cY)) # Add the centroid to the list
51
+
52
+ # Connect the red spots in the desired order
53
+ for i in range(len(centroid_list) - 1):
54
+ start_point = centroid_list[i]
55
+ end_point = centroid_list[i + 1]
56
+ line_color = (0, 0, 0) # Red color
57
+ cv2.line(original_image, start_point, end_point, line_color, 2)
58
 
59
  return original_image, len(contours)
60
 
61
+
62
  st.title('Saliency Detection App')
63
  st.write('Upload an image for saliency detection:')
64
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
98
 
99
  st.image(blended_img, caption='Blended Image', use_column_width=True, channels='BGR')
100
 
101
+ # Create a dir with the name example to save
102
  cv2.imwrite('example/result15.png', blended_img, [int(cv2.IMWRITE_JPEG_QUALITY), 200])
103
  st.success('Saliency detection complete. Result saved as "example/result15.png".')