mustafa2ak commited on
Commit
e9869ef
ยท
verified ยท
1 Parent(s): 266350e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -29,55 +29,46 @@ from utils import (
29
  get_debug_messages, # โœ… make sure this is imported
30
  )
31
 
32
- def process_image(image, use_gps=True):
33
- if image is None:
 
 
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
- # ---------------- GPS Extraction ----------------
42
  gps_data = {"coords": None, "address": None}
43
  if use_gps:
44
  try:
45
- if hasattr(image, "name"): # file path
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
- # ---------------- Save Detection ----------------
53
- detection_data = {
54
- "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"),
55
- "severity": severity,
56
  "count": len(detections),
57
- "types": [d["type"] for d in detections],
58
  "gps": gps_data,
59
  }
60
- save_detection_to_history(detection_data)
61
-
62
- # ---------------- Summary ----------------
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 = format_statistics_text(generate_statistics())
 
 
76
 
77
- # ---------------- Debug Logs ----------------
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="pil",
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