Ariel commited on
Commit
d972dd4
β€’
1 Parent(s): 4d87648

adjust image

Browse files
Files changed (2) hide show
  1. SolarPanelDetector.py +138 -0
  2. app.py +1 -136
SolarPanelDetector.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ from PIL import Image
3
+ import requests
4
+ import os
5
+ import random
6
+
7
+ model = YOLO('detector.pt')
8
+
9
+
10
+ def satellite_image_params(address, api_key, zoom, size):
11
+ """
12
+ Generate parameters for Google Maps API request based on given address, API key, zoom level, and image size.
13
+
14
+ Parameters:
15
+ address (str): The address to center the map on.
16
+ api_key (str): Google Maps API key.
17
+ zoom (int): Zoom level for the map.
18
+ size (str): Size of the requested map image.
19
+
20
+ Returns:
21
+ dict: A dictionary of parameters for the API request.
22
+ """
23
+ params = {
24
+ "center": address,
25
+ "zoom": str(zoom),
26
+ "size": size,
27
+ "maptype": "satellite",
28
+ "key": api_key
29
+ }
30
+ return params
31
+
32
+
33
+ def fetch_satellite_image(address, api_key, zoom=18, size="640x640"):
34
+ """
35
+ Fetches a satellite image from Google Maps API based on the given address, api_key, zoom level, and size.
36
+
37
+ Parameters:
38
+ address (str): The address for the satellite image.
39
+ api_key (str): Google Maps API key.
40
+ zoom (int): Zoom level for the satellite image.
41
+ size (str): Size of the satellite image.
42
+
43
+ Returns:
44
+ str: File name of the saved satellite image or None if the request fails.
45
+ """
46
+ base_url = "https://maps.googleapis.com/maps/api/staticmap?"
47
+ params = satellite_image_params(address, api_key, zoom=zoom, size=size)
48
+ try:
49
+ response = requests.get(base_url, params=params)
50
+ except requests.exceptions.RequestException as e:
51
+ print(e)
52
+ return None
53
+ if response.status_code == 200:
54
+ image_data = response.content
55
+ img_name = f"{'_'.join(address.split()[-2:])}.jpg"
56
+ with open(img_name, "wb") as file:
57
+ file.write(image_data)
58
+ print("Image was downloaded successfully")
59
+ return img_name
60
+
61
+
62
+ def plot_results(im_array, save_image=False, img_path="results.jpg"):
63
+ """
64
+ Converts an image array to a PIL image and optionally saves it.
65
+
66
+ Parameters:
67
+ im_array (numpy.ndarray): The image array to be converted.
68
+ save_image (bool): Whether to save the image.
69
+ img_path (str): Path to save the image.
70
+
71
+ Returns:
72
+ PIL.Image: The converted PIL image.
73
+ """
74
+ im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
75
+ if save_image:
76
+ im.save(img_path) # save image
77
+ return im
78
+
79
+
80
+ def solar_panel_predict(image, conf=0.45):
81
+ """
82
+ Analyzes an image to detect solar panels and returns an annotated image along with a relevant message.
83
+
84
+ This function uses a model to detect solar panels in the given image. If solar panels are detected with confidence
85
+ above the specified threshold, it selects a positive sentence; otherwise, it chooses a sentence encouraging
86
+ solar panel installation. It also annotates the image with detection results.
87
+
88
+ Parameters:
89
+ image: The input image for solar panel detection.
90
+ conf: Confidence threshold for detection, default is 0.5.
91
+
92
+ Returns:
93
+ Tuple of (annotated image, prediction message)
94
+ """
95
+ negative_setences = [
96
+ "No solar panels yet? Your roof is a blank canvas waiting for a green masterpiece! 🎨🌱",
97
+ "It's lonely up here without solar panels. Imagine the sun-powered parties you're missing! πŸŒžπŸŽ‰",
98
+ "Your roof could be a superhero in disguise. Just needs its solar cape! πŸ¦Έβ€β™‚οΈβ˜€οΈ",
99
+ "Clear skies, empty roof. It's the perfect opportunity to harness the sun! πŸŒ€οΈπŸ”‹",
100
+ "No panels detected – but don't worry, it's never too late to join the solar revolution and be a ray of hope! πŸŒπŸ’‘"]
101
+
102
+ positive_sentences = [
103
+ "Solar panels detected: You're not just saving money, you're also charging up Mother Earth's good vibes! πŸŒπŸ’š",
104
+ "Roof status: Sunny side up! Your panels are turning rays into awesome days! β˜€οΈπŸ˜Ž",
105
+ "You've got solar power! Now your roof is cooler than a polar bear in sunglasses. πŸ»β€β„οΈπŸ•ΆοΈ",
106
+ "Green alert: Your roof is now a climate hero's cape! Solar panels are saving the day, one ray at a time. πŸ¦Έβ€β™‚οΈπŸŒž",
107
+ "Solar panels spotted: Your roof is now officially a member of the Renewable Energy Rockstars Club! ⭐🌱"]
108
+
109
+ results = model(image, stream=True, conf=conf)
110
+ for result in results:
111
+ annotated_image = result.plot()
112
+ im = plot_results(annotated_image)
113
+
114
+ r = result.boxes
115
+ confi = r.conf.numpy().tolist()
116
+ if not confi:
117
+ prediction = random.choice(negative_setences)
118
+ else:
119
+ prediction = random.choice(positive_sentences)
120
+ return im, prediction
121
+
122
+
123
+ def detector(address, api_key, zoom=18, size="640x640"):
124
+ """
125
+ Detects solar panels in a satellite image fetched based on the given address.
126
+
127
+ Parameters:
128
+ address (str): The address to fetch the satellite image of.
129
+ api_key (str): Google Maps API key.
130
+ zoom (int): Zoom level for the image.
131
+ size (str): Size of the image.
132
+
133
+ Returns:
134
+ tuple: Prediction text and detected image.
135
+ """
136
+ img_name = fetch_satellite_image(address, api_key, zoom=zoom, size=size)
137
+ im, prediction = solar_panel_predict(img_name)
138
+ return im, prediction
app.py CHANGED
@@ -1,142 +1,7 @@
1
  import gradio as gr
2
- from ultralytics import YOLO
3
  from PIL import Image
4
- import requests
5
  import os
6
- import random
7
-
8
- model = YOLO('detector.pt')
9
-
10
-
11
- def satellite_image_params(address, api_key, zoom, size):
12
- """
13
- Generate parameters for Google Maps API request based on given address, API key, zoom level, and image size.
14
-
15
- Parameters:
16
- address (str): The address to center the map on.
17
- api_key (str): Google Maps API key.
18
- zoom (int): Zoom level for the map.
19
- size (str): Size of the requested map image.
20
-
21
- Returns:
22
- dict: A dictionary of parameters for the API request.
23
- """
24
- params = {
25
- "center": address,
26
- "zoom": str(zoom),
27
- "size": size,
28
- "maptype": "satellite",
29
- "key": api_key
30
- }
31
- return params
32
-
33
-
34
- def fetch_satellite_image(address, api_key, zoom=18, size="640x640"):
35
- """
36
- Fetches a satellite image from Google Maps API based on the given address, api_key, zoom level, and size.
37
-
38
- Parameters:
39
- address (str): The address for the satellite image.
40
- api_key (str): Google Maps API key.
41
- zoom (int): Zoom level for the satellite image.
42
- size (str): Size of the satellite image.
43
-
44
- Returns:
45
- str: File name of the saved satellite image or None if the request fails.
46
- """
47
- base_url = "https://maps.googleapis.com/maps/api/staticmap?"
48
- params = satellite_image_params(address, api_key, zoom=zoom, size=size)
49
- try:
50
- response = requests.get(base_url, params=params)
51
- except requests.exceptions.RequestException as e:
52
- print(e)
53
- return None
54
- if response.status_code == 200:
55
- image_data = response.content
56
- img_name = f"{'_'.join(address.split()[-2:])}.jpg"
57
- with open(img_name, "wb") as file:
58
- file.write(image_data)
59
- print("Image was downloaded successfully")
60
- return img_name
61
-
62
-
63
- def plot_results(im_array, save_image=False, img_path="results.jpg"):
64
- """
65
- Converts an image array to a PIL image and optionally saves it.
66
-
67
- Parameters:
68
- im_array (numpy.ndarray): The image array to be converted.
69
- save_image (bool): Whether to save the image.
70
- img_path (str): Path to save the image.
71
-
72
- Returns:
73
- PIL.Image: The converted PIL image.
74
- """
75
- im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
76
- if save_image:
77
- im.save(img_path) # save image
78
- return im
79
-
80
-
81
- def solar_panel_predict(image, conf=0.45):
82
- """
83
- Analyzes an image to detect solar panels and returns an annotated image along with a relevant message.
84
-
85
- This function uses a model to detect solar panels in the given image. If solar panels are detected with confidence
86
- above the specified threshold, it selects a positive sentence; otherwise, it chooses a sentence encouraging
87
- solar panel installation. It also annotates the image with detection results.
88
-
89
- Parameters:
90
- image: The input image for solar panel detection.
91
- conf: Confidence threshold for detection, default is 0.5.
92
-
93
- Returns:
94
- Tuple of (annotated image, prediction message)
95
- """
96
- negative_setences = [
97
- "No solar panels yet? Your roof is a blank canvas waiting for a green masterpiece! 🎨🌱",
98
- "It's lonely up here without solar panels. Imagine the sun-powered parties you're missing! πŸŒžπŸŽ‰",
99
- "Your roof could be a superhero in disguise. Just needs its solar cape! πŸ¦Έβ€β™‚οΈβ˜€οΈ",
100
- "Clear skies, empty roof. It's the perfect opportunity to harness the sun! πŸŒ€οΈπŸ”‹",
101
- "No panels detected – but don't worry, it's never too late to join the solar revolution and be a ray of hope! πŸŒπŸ’‘"]
102
-
103
- positive_sentences = [
104
- "Solar panels detected: You're not just saving money, you're also charging up Mother Earth's good vibes! πŸŒπŸ’š",
105
- "Roof status: Sunny side up! Your panels are turning rays into awesome days! β˜€οΈπŸ˜Ž",
106
- "You've got solar power! Now your roof is cooler than a polar bear in sunglasses. πŸ»β€β„οΈπŸ•ΆοΈ",
107
- "Green alert: Your roof is now a climate hero's cape! Solar panels are saving the day, one ray at a time. πŸ¦Έβ€β™‚οΈπŸŒž",
108
- "Solar panels spotted: Your roof is now officially a member of the Renewable Energy Rockstars Club! ⭐🌱"]
109
-
110
- results = model(image, stream=True, conf=conf)
111
- for result in results:
112
- annotated_image = result.plot()
113
- im = plot_results(annotated_image)
114
-
115
- r = result.boxes
116
- confi = r.conf.numpy().tolist()
117
- if not confi:
118
- prediction = random.choice(negative_setences)
119
- else:
120
- prediction = random.choice(positive_sentences)
121
- return im, prediction
122
-
123
-
124
- def detector(address, api_key, zoom=18, size="640x640"):
125
- """
126
- Detects solar panels in a satellite image fetched based on the given address.
127
-
128
- Parameters:
129
- address (str): The address to fetch the satellite image of.
130
- api_key (str): Google Maps API key.
131
- zoom (int): Zoom level for the image.
132
- size (str): Size of the image.
133
-
134
- Returns:
135
- tuple: Prediction text and detected image.
136
- """
137
- img_name = fetch_satellite_image(address, api_key, zoom=zoom, size=size)
138
- im, prediction = solar_panel_predict(img_name)
139
- return im, prediction
140
 
141
  custom_css = """
142
  .feedback textarea {font-size: 20px !important;}
 
1
  import gradio as gr
 
2
  from PIL import Image
 
3
  import os
4
+ from SolarPanelDetector import solar_panel_predict, detector
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  custom_css = """
7
  .feedback textarea {font-size: 20px !important;}