krupal02 commited on
Commit
8b483f2
·
verified ·
1 Parent(s): d05495c

Upload app/utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app/utils.py +101 -0
app/utils.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import requests
3
+ from app.model import model
4
+
5
+ # Must match data.yaml: ['Accident', 'Non-accident', 'Fire']
6
+ CLASS_NAMES = {0: "Accident", 1: "Non-accident", 2: "Fire"}
7
+ ALERT_CLASSES = {"Accident", "Fire"}
8
+
9
+
10
+ def predict_image(file_path, conf_threshold=0.3):
11
+ """Run YOLO inference on an image and return detections list."""
12
+ results = model(file_path, conf=conf_threshold, verbose=False)
13
+ result = results[0]
14
+
15
+ detections = []
16
+ for box in result.boxes:
17
+ cls_id = int(box.cls[0])
18
+ conf = float(box.conf[0])
19
+ label = CLASS_NAMES.get(cls_id, "unknown")
20
+ detections.append({"label": label, "confidence": round(conf, 3)})
21
+
22
+ return detections
23
+
24
+
25
+ def haversine_km(lat1, lon1, lat2, lon2):
26
+ """Calculate great-circle distance in km between two lat/lon points."""
27
+ R = 6371
28
+ dlat = math.radians(lat2 - lat1)
29
+ dlon = math.radians(lon2 - lon1)
30
+ a = (
31
+ math.sin(dlat / 2) ** 2
32
+ + math.cos(math.radians(lat1))
33
+ * math.cos(math.radians(lat2))
34
+ * math.sin(dlon / 2) ** 2
35
+ )
36
+ return R * 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
37
+
38
+
39
+ def get_ip_location():
40
+ """Get approximate location via free IP geolocation (no API key)."""
41
+ try:
42
+ r = requests.get("http://ip-api.com/json/", timeout=5)
43
+ d = r.json()
44
+ if d.get("status") == "success":
45
+ return {
46
+ "lat": d["lat"],
47
+ "lon": d["lon"],
48
+ "city": d.get("city", ""),
49
+ "region": d.get("regionName", ""),
50
+ "country": d.get("country", ""),
51
+ }
52
+ except Exception:
53
+ pass
54
+ return None
55
+
56
+
57
+ def fetch_hospitals(lat, lon, radius_m=5000):
58
+ """Query OpenStreetMap Overpass API for hospitals within radius_m metres."""
59
+ query = f"""
60
+ [out:json][timeout:10];
61
+ (
62
+ node["amenity"="hospital"](around:{radius_m},{lat},{lon});
63
+ way["amenity"="hospital"](around:{radius_m},{lat},{lon});
64
+ relation["amenity"="hospital"](around:{radius_m},{lat},{lon});
65
+ );
66
+ out center body;
67
+ """
68
+ try:
69
+ r = requests.post(
70
+ "https://overpass-api.de/api/interpreter",
71
+ data={"data": query},
72
+ timeout=15,
73
+ )
74
+ elements = r.json().get("elements", [])
75
+ except Exception:
76
+ return []
77
+
78
+ hospitals = []
79
+ for el in elements:
80
+ tags = el.get("tags", {})
81
+ if el["type"] == "node":
82
+ h_lat, h_lon = el["lat"], el["lon"]
83
+ else:
84
+ c = el.get("center", {})
85
+ h_lat = c.get("lat", lat)
86
+ h_lon = c.get("lon", lon)
87
+
88
+ dist = haversine_km(lat, lon, h_lat, h_lon)
89
+ hospitals.append(
90
+ {
91
+ "name": tags.get("name", "Unnamed Hospital"),
92
+ "lat": h_lat,
93
+ "lon": h_lon,
94
+ "distance_km": round(dist, 2),
95
+ "phone": tags.get("phone", tags.get("contact:phone", None)),
96
+ "emergency": tags.get("emergency", "unknown"),
97
+ }
98
+ )
99
+
100
+ hospitals.sort(key=lambda h: h["distance_km"])
101
+ return hospitals