Harsh-7300 commited on
Commit
70885fe
·
verified ·
1 Parent(s): 9aaf36d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -55
app.py CHANGED
@@ -1,103 +1,86 @@
1
  import cv2
2
  import numpy as np
3
  import matplotlib.pyplot as plt
 
4
  from skimage import measure
 
5
 
6
- def preprocess_image(image_path):
7
- # Read image
8
- img = cv2.imread(image_path)
9
 
10
- # Check if the image is loaded correctly
11
  if img is None:
12
- print(f"Error: Unable to load image at {image_path}")
13
  return None, None
14
 
15
- # Convert to grayscale
16
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
17
-
18
- # Apply Gaussian blur
19
  blurred = cv2.GaussianBlur(gray, (5, 5), 0)
20
 
21
  return img, blurred
22
 
23
  def detect_edges(blurred_image):
24
- # Detect edges using Canny edge detector
25
  edges = cv2.Canny(blurred_image, 50, 150)
26
  return edges
27
 
28
  def find_contours(edges_image):
29
- # Find contours in the edges image
30
  contours, _ = cv2.findContours(edges_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
31
  return contours
32
 
33
  def filter_contours(contours, min_area=1000):
34
- # Filter out small contours (e.g., noise, unwanted areas)
35
  filtered_contours = [contour for contour in contours if cv2.contourArea(contour) > min_area]
36
  return filtered_contours
37
 
38
  def draw_contours(image, contours):
39
- # Draw contours on the original image
40
  output_image = image.copy()
41
  cv2.drawContours(output_image, contours, -1, (0, 255, 0), 3)
42
  return output_image
43
 
44
  def calculate_area(contours):
45
- # Calculate the area of the detected contours
46
  areas = [cv2.contourArea(contour) for contour in contours]
47
  total_area = sum(areas)
48
  return total_area
49
 
50
  def remove_shadows(image):
51
- # Convert to HSV to better detect shadows
52
  hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
53
-
54
- # Define shadow color range in HSV
55
  lower_shadow = np.array([0, 0, 0])
56
  upper_shadow = np.array([180, 255, 80])
57
-
58
- # Mask for shadows
59
  shadow_mask = cv2.inRange(hsv, lower_shadow, upper_shadow)
60
-
61
- # Remove shadow by inpainting (filling shadow areas with nearby pixel values)
62
  result = cv2.inpaint(image, shadow_mask, 3, cv2.INPAINT_TELEA)
63
-
64
  return result
65
 
66
  def main():
67
- # Ask the user to provide the image path
68
- image_path = input("Please provide the path to the rooftop image (e.g., 'image.jpg'): ")
69
-
70
- # Step 1: Preprocess image
71
- img, blurred = preprocess_image(image_path)
72
-
73
- # If the image was not loaded successfully, exit the program
74
- if img is None:
75
- return
76
-
77
- # Step 2: Detect edges
78
- edges = detect_edges(blurred)
79
-
80
- # Step 3: Find contours
81
- contours = find_contours(edges)
82
-
83
- # Step 4: Filter contours to exclude unwanted areas
84
- filtered_contours = filter_contours(contours)
85
-
86
- # Step 5: Remove shadows from the image
87
- image_no_shadows = remove_shadows(img)
88
-
89
- # Step 6: Draw the predicted rooftop boundaries
90
- output_img = draw_contours(image_no_shadows, filtered_contours)
91
-
92
- # Step 7: Calculate the area of the rooftop
93
- total_area = calculate_area(filtered_contours)
94
- print(f"Total Rooftop Area: {total_area} pixels")
95
-
96
- # Step 8: Display the result
97
- plt.figure(figsize=(10, 10))
98
- plt.imshow(cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB))
99
- plt.axis('off')
100
- plt.show()
101
 
102
  if __name__ == "__main__":
103
  main()
 
1
  import cv2
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
+ import streamlit as st
5
  from skimage import measure
6
+ from io import BytesIO
7
 
8
+ def preprocess_image(image):
9
+ img = cv2.imdecode(np.frombuffer(image.read(), np.uint8), cv2.IMREAD_COLOR)
 
10
 
 
11
  if img is None:
12
+ st.error("Error: Unable to load image")
13
  return None, None
14
 
 
15
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
 
16
  blurred = cv2.GaussianBlur(gray, (5, 5), 0)
17
 
18
  return img, blurred
19
 
20
  def detect_edges(blurred_image):
 
21
  edges = cv2.Canny(blurred_image, 50, 150)
22
  return edges
23
 
24
  def find_contours(edges_image):
 
25
  contours, _ = cv2.findContours(edges_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
26
  return contours
27
 
28
  def filter_contours(contours, min_area=1000):
 
29
  filtered_contours = [contour for contour in contours if cv2.contourArea(contour) > min_area]
30
  return filtered_contours
31
 
32
  def draw_contours(image, contours):
 
33
  output_image = image.copy()
34
  cv2.drawContours(output_image, contours, -1, (0, 255, 0), 3)
35
  return output_image
36
 
37
  def calculate_area(contours):
 
38
  areas = [cv2.contourArea(contour) for contour in contours]
39
  total_area = sum(areas)
40
  return total_area
41
 
42
  def remove_shadows(image):
 
43
  hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
 
 
44
  lower_shadow = np.array([0, 0, 0])
45
  upper_shadow = np.array([180, 255, 80])
 
 
46
  shadow_mask = cv2.inRange(hsv, lower_shadow, upper_shadow)
 
 
47
  result = cv2.inpaint(image, shadow_mask, 3, cv2.INPAINT_TELEA)
 
48
  return result
49
 
50
  def main():
51
+ st.title("Rooftop Area Calculation from Satellite Image")
52
+
53
+ # Allow the user to upload a file
54
+ uploaded_file = st.file_uploader("Upload a rooftop image", type=["jpg", "png"])
55
+
56
+ if uploaded_file is not None:
57
+ # Step 1: Preprocess image
58
+ img, blurred = preprocess_image(uploaded_file)
59
+
60
+ if img is None:
61
+ return
62
+
63
+ # Step 2: Detect edges
64
+ edges = detect_edges(blurred)
65
+
66
+ # Step 3: Find contours
67
+ contours = find_contours(edges)
68
+
69
+ # Step 4: Filter contours to exclude unwanted areas
70
+ filtered_contours = filter_contours(contours)
71
+
72
+ # Step 5: Remove shadows from the image
73
+ image_no_shadows = remove_shadows(img)
74
+
75
+ # Step 6: Draw the predicted rooftop boundaries
76
+ output_img = draw_contours(image_no_shadows, filtered_contours)
77
+
78
+ # Step 7: Calculate the area of the rooftop
79
+ total_area = calculate_area(filtered_contours)
80
+
81
+ # Step 8: Display the result
82
+ st.image(output_img, channels="BGR", caption="Detected Rooftop with Boundaries")
83
+ st.write(f"Total Rooftop Area: {total_area} pixels")
 
84
 
85
  if __name__ == "__main__":
86
  main()