Ariel
commited on
Commit
Β·
f8fbdd8
1
Parent(s):
c6d84d3
Upload app
Browse files- README.md +1 -2
- app.py +193 -0
- detector.pt +3 -0
- examples/Ceske-Budejovice.jpg +0 -0
- examples/Gottingen.jpg +0 -0
- examples/San-Diego.jpg +0 -0
- examples/Tubingen.jpg +0 -0
- requirements.txt +0 -0
README.md
CHANGED
@@ -10,5 +10,4 @@ pinned: false
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
-
sadasd
|
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
app.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;}
|
143 |
+
.centered-text {text-align: center; width: 100%;}
|
144 |
+
"""
|
145 |
+
|
146 |
+
with gr.Blocks(theme="HaleyCH/HaleyCH_Theme", title="Solar Panel Detector", css=custom_css) as app:
|
147 |
+
# add logo
|
148 |
+
gr.Markdown("# **Solar Panel Detector 2.0** π°οΈβοΈ", elem_classes="centered-text")
|
149 |
+
with gr.Column(scale=1, variant="default"):
|
150 |
+
gr.HTML(f"""
|
151 |
+
<div style='text-align: center;'>
|
152 |
+
<img src='https://github.com/ArielDrabkin/solar-panel-detector2/blob/master/deployment/examples/DALL-E.jpeg?raw=true'
|
153 |
+
height='500' width='1200';'/>
|
154 |
+
</div>
|
155 |
+
""")
|
156 |
+
gr.Markdown("## This app provides the ability to detect solar panels in a given address or a given image.")
|
157 |
+
|
158 |
+
gr.Markdown("### Using by address with google maps:\n1. Enter your address.\n"
|
159 |
+
"2. Insert your Google maps api key which you can get from - "
|
160 |
+
"https://developers.google.com/maps/documentation/maps-static/get-api-key .\n"
|
161 |
+
"3. Choose the zoom level (19 is the default).")
|
162 |
+
address = gr.Textbox(label="Address")
|
163 |
+
api_key = gr.Textbox(label="Google maps api key", type="password")
|
164 |
+
zoom = gr.Slider(minimum=18, maximum=22, step=1, value=19, label="zoom")
|
165 |
+
btn = gr.Button(value="Submit")
|
166 |
+
with gr.Row():
|
167 |
+
predicted_image_address = gr.Image(type="pil", show_label=False, scale=1)
|
168 |
+
prediction_address = gr.Textbox(type="text", show_label=False, scale=1, elem_classes="feedback")
|
169 |
+
btn.click(detector, inputs=[address, api_key, zoom], outputs=[predicted_image_address, prediction_address])
|
170 |
+
|
171 |
+
gr.Markdown("### Using by a given image:\nUpload an image or use the examples below.")
|
172 |
+
with gr.Row():
|
173 |
+
im = gr.Image(type="pil", show_label=False, scale=1)
|
174 |
+
predicted_image = gr.Image(type="pil", show_label=False, scale=1)
|
175 |
+
|
176 |
+
prediction = gr.Textbox(type="text", show_label=False, elem_classes="feedback")
|
177 |
+
btn = gr.Button(value="Submit")
|
178 |
+
btn.click(solar_panel_predict, inputs=im, outputs=[predicted_image, prediction])
|
179 |
+
|
180 |
+
gr.Markdown("### Image Examples")
|
181 |
+
gr.Examples(
|
182 |
+
examples=[os.path.join(os.path.dirname(__file__), "examples/Gottingen.jpg"),
|
183 |
+
os.path.join(os.path.dirname(__file__), "examples/Tubingen.jpg"),
|
184 |
+
os.path.join(os.path.dirname(__file__), "examples/San-Diego.jpg"),
|
185 |
+
os.path.join(os.path.dirname(__file__), "examples/Ceske-Budejovice.jpg")],
|
186 |
+
inputs=im,
|
187 |
+
outputs=[predicted_image, prediction],
|
188 |
+
fn=solar_panel_predict,
|
189 |
+
cache_examples=False,
|
190 |
+
)
|
191 |
+
|
192 |
+
if __name__ == "__main__":
|
193 |
+
app.launch()
|
detector.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:08ba1e94a8c8850342d995a95875b6bbf2d0cb567803d33c945a8a4b4ba9e345
|
3 |
+
size 6261081
|
examples/Ceske-Budejovice.jpg
ADDED
![]() |
examples/Gottingen.jpg
ADDED
![]() |
examples/San-Diego.jpg
ADDED
![]() |
examples/Tubingen.jpg
ADDED
![]() |
requirements.txt
ADDED
Binary file (142 Bytes). View file
|
|