Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def process_image(image, use_gps=True):
|
| 2 |
if image is None:
|
| 3 |
-
return None, "โ Please upload an image!",
|
| 4 |
|
| 5 |
detections = detector.detect_items(image)
|
| 6 |
annotated = detector.draw_boxes(image, detections)
|
|
@@ -13,7 +33,7 @@ def process_image(image, use_gps=True):
|
|
| 13 |
"severity": severity,
|
| 14 |
"count": len(detections),
|
| 15 |
"types": [d["type"] for d in detections],
|
| 16 |
-
"gps": gps_data
|
| 17 |
}
|
| 18 |
save_detection_to_history(detection_data)
|
| 19 |
|
|
@@ -21,12 +41,52 @@ def process_image(image, use_gps=True):
|
|
| 21 |
if gps_data["coords"]:
|
| 22 |
summary += f"\n๐ Location: {gps_data['coords'][0]:.6f}, {gps_data['coords'][1]:.6f}"
|
| 23 |
elif gps_data["address"]:
|
| 24 |
-
summary += f"\n๐
|
| 25 |
else:
|
| 26 |
summary += "\n๐ No GPS/location data available"
|
| 27 |
|
| 28 |
history = load_detection_history()
|
| 29 |
-
map_html = create_detection_map(
|
| 30 |
stats = format_statistics_text(generate_statistics())
|
| 31 |
|
| 32 |
return annotated, summary, map_html, stats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app.py - Hugging Face Gradio App
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from detector import RoadsideDetector
|
| 8 |
+
from utils import (
|
| 9 |
+
extract_gps_from_image,
|
| 10 |
+
save_detection_to_history,
|
| 11 |
+
load_detection_history,
|
| 12 |
+
create_detection_map,
|
| 13 |
+
generate_statistics,
|
| 14 |
+
format_statistics_text,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
detector = RoadsideDetector()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
def process_image(image, use_gps=True):
|
| 22 |
if image is None:
|
| 23 |
+
return None, "โ Please upload an image!", "<p>No map</p>", "๐ No statistics yet"
|
| 24 |
|
| 25 |
detections = detector.detect_items(image)
|
| 26 |
annotated = detector.draw_boxes(image, detections)
|
|
|
|
| 33 |
"severity": severity,
|
| 34 |
"count": len(detections),
|
| 35 |
"types": [d["type"] for d in detections],
|
| 36 |
+
"gps": gps_data,
|
| 37 |
}
|
| 38 |
save_detection_to_history(detection_data)
|
| 39 |
|
|
|
|
| 41 |
if gps_data["coords"]:
|
| 42 |
summary += f"\n๐ Location: {gps_data['coords'][0]:.6f}, {gps_data['coords'][1]:.6f}"
|
| 43 |
elif gps_data["address"]:
|
| 44 |
+
summary += f"\n๐ Location: {gps_data['address']}"
|
| 45 |
else:
|
| 46 |
summary += "\n๐ No GPS/location data available"
|
| 47 |
|
| 48 |
history = load_detection_history()
|
| 49 |
+
map_html = create_detection_map(history)
|
| 50 |
stats = format_statistics_text(generate_statistics())
|
| 51 |
|
| 52 |
return annotated, summary, map_html, stats
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def create_interface():
|
| 56 |
+
with gr.Blocks(title="๐ฎ RoadsideClean") as demo:
|
| 57 |
+
gr.Markdown("# ๐ฎ RoadsideClean - Illegal Dumping Detector")
|
| 58 |
+
|
| 59 |
+
with gr.Tabs():
|
| 60 |
+
with gr.TabItem("๐ธ Detect"):
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column():
|
| 63 |
+
input_img = gr.Image(
|
| 64 |
+
type="pil",
|
| 65 |
+
label="๐ท Take/Upload Photo",
|
| 66 |
+
sources=["upload", "webcam"],
|
| 67 |
+
)
|
| 68 |
+
gps_toggle = gr.Checkbox("๐ Extract GPS", value=True)
|
| 69 |
+
detect_btn = gr.Button("๐ Detect", variant="primary")
|
| 70 |
+
|
| 71 |
+
with gr.Column():
|
| 72 |
+
out_img = gr.Image(label="Detection Results", type="pil")
|
| 73 |
+
summary = gr.Textbox(label="Summary", lines=6)
|
| 74 |
+
|
| 75 |
+
with gr.TabItem("๐บ๏ธ Map"):
|
| 76 |
+
map_display = gr.HTML("<p>๐บ๏ธ No detections yet</p>")
|
| 77 |
+
|
| 78 |
+
with gr.TabItem("๐ Statistics"):
|
| 79 |
+
stats_display = gr.Markdown("๐ No statistics yet")
|
| 80 |
+
|
| 81 |
+
detect_btn.click(
|
| 82 |
+
process_image,
|
| 83 |
+
inputs=[input_img, gps_toggle],
|
| 84 |
+
outputs=[out_img, summary, map_display, stats_display],
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
return demo
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
app = create_interface()
|
| 92 |
+
app.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
|