Spaces:
Build error
Build error
| import gradio as gr | |
| import cv2, mediapipe as mp | |
| import numpy as np | |
| import pandas as pd | |
| import sqlite3, geocoder, folium, threading, time | |
| from datetime import datetime | |
| from sklearn.linear_model import LinearRegression | |
| import speech_recognition as sr | |
| import matplotlib.pyplot as plt | |
| from io import BytesIO | |
| import base64 | |
| # ========================= | |
| # DATABASE | |
| # ========================= | |
| conn = sqlite3.connect("emergency.db", check_same_thread=False) | |
| cur = conn.cursor() | |
| cur.execute(""" | |
| CREATE TABLE IF NOT EXISTS emergency ( | |
| time TEXT, | |
| trigger TEXT, | |
| type TEXT, | |
| severity INT, | |
| location TEXT, | |
| status TEXT | |
| ) | |
| """) | |
| conn.commit() | |
| # ========================= | |
| # ML SEVERITY MODEL | |
| # ========================= | |
| X = np.array([[0,0,0],[1,0,0],[0,1,0],[0,0,1],[1,1,1]]) | |
| y = np.array([1,4,5,6,9]) | |
| model = LinearRegression() | |
| model.fit(X, y) | |
| # ========================= | |
| # AI MODELS | |
| # ========================= | |
| mp_hands = mp.solutions.hands | |
| hands = mp_hands.Hands(False,1,0.7) | |
| mp_draw = mp.solutions.drawing_utils | |
| face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
| VOICE_SOS = {"flag": False} | |
| WOMEN_MODE = {"on": False} | |
| # ========================= | |
| # VOICE LISTENER THREAD | |
| # ========================= | |
| def voice_listener(): | |
| r = sr.Recognizer() | |
| mic = sr.Microphone() | |
| while True: | |
| try: | |
| with mic as source: | |
| audio = r.listen(source, phrase_time_limit=3) | |
| text = r.recognize_google(audio).lower() | |
| if any(k in text for k in ["help","sos","emergency","save me"]): | |
| VOICE_SOS["flag"] = True | |
| except: | |
| pass | |
| time.sleep(0.2) | |
| threading.Thread(target=voice_listener, daemon=True).start() | |
| # ========================= | |
| # LOCATION | |
| # ========================= | |
| def get_location(): | |
| g = geocoder.ip("me") | |
| if g.ok: | |
| return g.latlng, f"{g.city}, {g.state}, {g.country}" | |
| return [0,0], "Unknown" | |
| # ========================= | |
| # MAP | |
| # ========================= | |
| def make_map(df): | |
| if df.empty: | |
| m = folium.Map(location=[20,78], zoom_start=5) | |
| return m._repr_html_() | |
| m = folium.Map(location=[df.iloc[-1]['lat'], df.iloc[-1]['lon']], zoom_start=12) | |
| for _, row in df.iterrows(): | |
| color = "red" if row['severity']>5 else "orange" | |
| folium.Marker([row['lat'], row['lon']], | |
| popup=f"{row['type']} | Severity: {row['severity']}", | |
| icon=folium.Icon(color=color)).add_to(m) | |
| return m._repr_html_() | |
| # ========================= | |
| # PROCESS FRAME | |
| # ========================= | |
| def process(frame): | |
| gesture, voice, face = 0,0,0 | |
| status = "Monitoring..." | |
| img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| res = hands.process(img) | |
| # Hand gesture detection | |
| if res.multi_hand_landmarks: | |
| for lm in res.multi_hand_landmarks: | |
| mp_draw.draw_landmarks(frame, lm, mp_hands.HAND_CONNECTIONS) | |
| if lm.landmark[8].y < lm.landmark[6].y: | |
| gesture = 1 | |
| status = "β SOS Gesture" | |
| if lm.landmark[8].y > lm.landmark[6].y: | |
| gesture = 1 | |
| status = "β Threat Gesture" | |
| # Voice SOS | |
| if VOICE_SOS["flag"]: | |
| voice = 1 | |
| status = "π€ SOS Voice" | |
| VOICE_SOS["flag"] = False | |
| # Face distress detection | |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| faces = face_cascade.detectMultiScale(gray,1.3,5) | |
| if len(faces)>0: | |
| face = 1 | |
| status = "π Face Distress Detected" | |
| if gesture or voice or face: | |
| severity = int(model.predict([[gesture,voice,face]])[0]) | |
| latlon, loc = get_location() | |
| cur.execute("INSERT INTO emergency VALUES (?,?,?,?,?,?)", | |
| (datetime.now().strftime("%H:%M:%S"), "AI", "SOS", severity, loc, "Pending")) | |
| conn.commit() | |
| df_sql = pd.read_sql("SELECT * FROM emergency", conn) | |
| df_sql[['lat','lon']] = df_sql['location'].apply(lambda x: pd.Series(get_location()[0])) | |
| map_html = make_map(df_sql) | |
| return frame, status, df_sql, map_html | |
| # ========================= | |
| # ADMIN | |
| # ========================= | |
| def admin_action(action): | |
| cur.execute("UPDATE emergency SET status=? WHERE rowid=(SELECT max(rowid) FROM emergency)", | |
| ("Approved" if action=="Approve" else "Cancelled",)) | |
| conn.commit() | |
| df_sql = pd.read_sql("SELECT * FROM emergency", conn) | |
| df_sql[['lat','lon']] = df_sql['location'].apply(lambda x: pd.Series(get_location()[0])) | |
| map_html = make_map(df_sql) | |
| return df_sql, map_html | |
| # ========================= | |
| # WOMEN SAFETY MODE | |
| # ========================= | |
| def toggle_women(on): | |
| WOMEN_MODE["on"] = on | |
| return "π© Women Safety Mode ON" if on else "Women Mode OFF" | |
| # ========================= | |
| # GRADIO UI | |
| # ========================= | |
| with gr.Blocks(theme=gr.themes.Soft(), title="Ultra-Advanced AI Emergency System") as app: | |
| gr.Markdown(""" | |
| # π¨ Ultra-Advanced Real-Time Emergency System | |
| **Hand β | Voice π€ | Face π | GPS π | ML Severity π€ | Admin π | Women Safety π©** | |
| """) | |
| with gr.Row(): | |
| cam = gr.Image(source="webcam", streaming=True) | |
| out = gr.Image() | |
| status = gr.Textbox(label="Live Status") | |
| table = gr.Dataframe(label="Emergency Table") | |
| map_view = gr.HTML(label="Live Map") | |
| with gr.Row(): | |
| admin = gr.Radio(["Approve","Cancel"], label="Admin Action") | |
| women = gr.Checkbox(label="π© Women Safety Mode") | |
| cam.stream(process, cam, [out,status,table,map_view]) | |
| admin.change(admin_action, admin, [table,map_view]) | |
| women.change(toggle_women, women, status) | |
| app.launch() | |