Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,55 +29,46 @@ from utils import (
|
|
| 29 |
get_debug_messages, # โ
make sure this is imported
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
return None, "โ Please upload an image!", None, "๐ No statistics yet", ""
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
# Run YOLO detection
|
| 37 |
detections = detector.detect_items(image)
|
| 38 |
annotated = detector.draw_boxes(image, detections)
|
| 39 |
severity = detector.calculate_severity(detections)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
gps_data = {"coords": None, "address": None}
|
| 43 |
if use_gps:
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
gps_data = extract_gps_from_image(image.name)
|
| 47 |
-
else: # fallback: pass the PIL image
|
| 48 |
-
gps_data = extract_gps_from_image(image)
|
| 49 |
except Exception as e:
|
| 50 |
add_debug(f"โ GPS extraction failed: {e}")
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"),
|
| 55 |
-
"severity": severity,
|
| 56 |
"count": len(detections),
|
| 57 |
-
"
|
| 58 |
"gps": gps_data,
|
| 59 |
}
|
| 60 |
-
save_detection_to_history(
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
-
summary = f"๐ Severity: {severity}\n๐ฆ Items: {len(detections)}"
|
| 64 |
-
if gps_data["coords"]:
|
| 65 |
-
lat, lon = gps_data["coords"]
|
| 66 |
-
summary += f"\n๐ Location: {lat:.6f}, {lon:.6f}"
|
| 67 |
-
elif gps_data["address"]:
|
| 68 |
-
summary += f"\n๐ Location: {gps_data['address']}"
|
| 69 |
-
else:
|
| 70 |
-
summary += "\n๐ No GPS/location data available"
|
| 71 |
-
|
| 72 |
-
# ---------------- Map + Stats ----------------
|
| 73 |
history = load_detection_history()
|
| 74 |
map_html = create_detection_map(history)
|
| 75 |
-
stats =
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
debug_output = get_debug_messages()
|
| 79 |
|
| 80 |
-
return annotated, summary, map_html, stats, debug_output # โ
now returns 5 values
|
| 81 |
|
| 82 |
|
| 83 |
|
|
@@ -91,10 +82,12 @@ def create_interface():
|
|
| 91 |
with gr.Row():
|
| 92 |
with gr.Column():
|
| 93 |
input_img = gr.Image(
|
| 94 |
-
type="
|
| 95 |
label="๐ท Take/Upload Photo",
|
| 96 |
sources=["upload", "webcam"],
|
| 97 |
)
|
|
|
|
|
|
|
| 98 |
gps_toggle = gr.Checkbox("๐ Extract GPS", value=True)
|
| 99 |
detect_btn = gr.Button("๐ Detect", variant="primary")
|
| 100 |
|
|
|
|
| 29 |
get_debug_messages, # โ
make sure this is imported
|
| 30 |
)
|
| 31 |
|
| 32 |
+
from PIL import Image
|
| 33 |
+
|
| 34 |
+
def process_image(image_path, use_gps=True): # Now we get a filepath, not a PIL image
|
| 35 |
+
if not image_path:
|
| 36 |
return None, "โ Please upload an image!", None, "๐ No statistics yet", ""
|
| 37 |
|
| 38 |
+
# Load image for YOLO detection
|
| 39 |
+
image = Image.open(image_path)
|
| 40 |
+
|
| 41 |
# Run YOLO detection
|
| 42 |
detections = detector.detect_items(image)
|
| 43 |
annotated = detector.draw_boxes(image, detections)
|
| 44 |
severity = detector.calculate_severity(detections)
|
| 45 |
|
| 46 |
+
# GPS extraction from original file
|
| 47 |
gps_data = {"coords": None, "address": None}
|
| 48 |
if use_gps:
|
| 49 |
try:
|
| 50 |
+
gps_data = extract_gps_from_image(image_path) # โ
pass file path
|
|
|
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
add_debug(f"โ GPS extraction failed: {e}")
|
| 53 |
|
| 54 |
+
# Save to history
|
| 55 |
+
detection_entry = {
|
| 56 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
| 57 |
"count": len(detections),
|
| 58 |
+
"severity": severity,
|
| 59 |
"gps": gps_data,
|
| 60 |
}
|
| 61 |
+
save_detection_to_history(detection_entry)
|
| 62 |
+
|
| 63 |
+
# Build map + stats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
history = load_detection_history()
|
| 65 |
map_html = create_detection_map(history)
|
| 66 |
+
stats = generate_statistics()
|
| 67 |
+
stats_text = format_statistics_text(stats)
|
| 68 |
+
debug_log = get_debug_messages()
|
| 69 |
|
| 70 |
+
return annotated, f"๐ Severity: {severity}\n๐ฆ Items: {len(detections)}", map_html, stats_text, debug_log
|
|
|
|
| 71 |
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
|
|
|
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column():
|
| 84 |
input_img = gr.Image(
|
| 85 |
+
type="filepath",
|
| 86 |
label="๐ท Take/Upload Photo",
|
| 87 |
sources=["upload", "webcam"],
|
| 88 |
)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
gps_toggle = gr.Checkbox("๐ Extract GPS", value=True)
|
| 92 |
detect_btn = gr.Button("๐ Detect", variant="primary")
|
| 93 |
|