Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,9 @@ import torch.nn as nn
|
|
| 17 |
import numpy as np
|
| 18 |
import pandas as pd
|
| 19 |
import joblib
|
|
|
|
|
|
|
|
|
|
| 20 |
import threading
|
| 21 |
import uuid
|
| 22 |
import time
|
|
@@ -53,8 +56,10 @@ print("--- BOOT SEQUENCE INITIATED ---", flush=True)
|
|
| 53 |
|
| 54 |
app = Flask(__name__)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
UPLOAD_FOLDER = 'uploads'
|
| 60 |
MODEL_FOLDER = 'models'
|
|
@@ -123,28 +128,27 @@ def process_accident_async(stream_id, frame, location, confidence, severity):
|
|
| 123 |
if stream_id in active_streams:
|
| 124 |
active_streams[stream_id]["plates"] = detected_plates
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
"
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
print(f"📡 SOS Push failed: {e}", flush=True)
|
| 148 |
|
| 149 |
def run_temporal_analysis(stream_id, frames_3d_copy, prob_max, prob_mean, prob_min, yolo_max_conf, raw_frame, location):
|
| 150 |
state = active_streams.get(stream_id)
|
|
@@ -160,20 +164,21 @@ def run_temporal_analysis(stream_id, frames_3d_copy, prob_max, prob_mean, prob_m
|
|
| 160 |
|
| 161 |
try:
|
| 162 |
ensemble_probs = model_svm.predict_proba(combined)[0]
|
| 163 |
-
final_idx = model_svm.predict(combined)[0]
|
| 164 |
confidence = float(ensemble_probs[final_idx] * 100)
|
| 165 |
severity = classes[final_idx]
|
| 166 |
-
except:
|
| 167 |
-
final_idx = min(int(np.argmax(prob_max)), 2)
|
| 168 |
-
confidence = float(np.max(prob_max)) * 100
|
| 169 |
severity = classes[final_idx]
|
| 170 |
|
| 171 |
state["severity"] = severity.capitalize()
|
| 172 |
state["confidence"] = round(confidence, 1)
|
| 173 |
-
state["cnn"] = round(yolo_max_conf, 1)
|
| 174 |
state["rcnn"] = round(float(np.max(prob_3d)) * 100, 1)
|
| 175 |
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
state["final_decision"] = True
|
| 178 |
state["label"] = f"Incident Logged: {severity.capitalize()}"
|
| 179 |
threading.Thread(target=process_accident_async, args=(stream_id, raw_frame, location, confidence, severity)).start()
|
|
@@ -205,15 +210,20 @@ def init_upload():
|
|
| 205 |
|
| 206 |
@app.route('/init_stream', methods=['POST'])
|
| 207 |
def init_stream():
|
| 208 |
-
url = request.json.get('url')
|
| 209 |
|
| 210 |
if 'youtube.com' in url or 'youtu.be' in url:
|
| 211 |
try:
|
| 212 |
-
ydl_opts = {'format': 'best
|
| 213 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 214 |
info = ydl.extract_info(url, download=False)
|
| 215 |
-
if 'url' in info:
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
except Exception as e:
|
| 218 |
print(f"yt-dlp extraction failed: {e}", flush=True)
|
| 219 |
|
|
@@ -302,7 +312,7 @@ def video_stream_gen(stream_id, source, location):
|
|
| 302 |
target_frame = int(elapsed * fps)
|
| 303 |
frames_to_skip = target_frame - current_frame_idx
|
| 304 |
if frames_to_skip > 0:
|
| 305 |
-
for _ in range(min(frames_to_skip,
|
| 306 |
cap.grab()
|
| 307 |
current_frame_idx += 1
|
| 308 |
|
|
@@ -353,19 +363,36 @@ def video_stream_gen(stream_id, source, location):
|
|
| 353 |
yolo_probs.append(probs)
|
| 354 |
if len(yolo_probs) > 10: yolo_probs.pop(0)
|
| 355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
f_3d = cv2.resize(frame, (112, 112))
|
| 357 |
frames_3d.append(cv2.cvtColor(f_3d, cv2.COLOR_BGR2RGB))
|
| 358 |
if len(frames_3d) > 16: frames_3d.pop(0)
|
| 359 |
|
| 360 |
-
if len(frames_3d) =
|
| 361 |
state["is_analyzing"] = True
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
threading.Thread(target=run_temporal_analysis, args=(
|
| 366 |
-
stream_id,
|
| 367 |
)).start()
|
| 368 |
-
except:
|
|
|
|
| 369 |
|
| 370 |
_, buffer = cv2.imencode('.jpg', display_frame)
|
| 371 |
last_buffer = buffer.tobytes()
|
|
|
|
| 17 |
import numpy as np
|
| 18 |
import pandas as pd
|
| 19 |
import joblib
|
| 20 |
+
import smtplib
|
| 21 |
+
import ssl
|
| 22 |
+
from email.message import EmailMessage
|
| 23 |
import threading
|
| 24 |
import uuid
|
| 25 |
import time
|
|
|
|
| 56 |
|
| 57 |
app = Flask(__name__)
|
| 58 |
|
| 59 |
+
ALERT_EMAIL_SENDER = "gowreeshgowri50@gmail.com"
|
| 60 |
+
ALERT_EMAIL_PASSWORD = "oynu ulet pynk xsza".replace(" ", "")
|
| 61 |
+
ALERT_EMAIL_RECEIVER = "mcblackdevil12342@gmail.com"
|
| 62 |
+
ENABLE_EMAIL_ALERTS = True
|
| 63 |
|
| 64 |
UPLOAD_FOLDER = 'uploads'
|
| 65 |
MODEL_FOLDER = 'models'
|
|
|
|
| 128 |
if stream_id in active_streams:
|
| 129 |
active_streams[stream_id]["plates"] = detected_plates
|
| 130 |
|
| 131 |
+
if ENABLE_EMAIL_ALERTS:
|
| 132 |
+
try:
|
| 133 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
| 134 |
+
msg = EmailMessage()
|
| 135 |
+
msg['Subject'] = f"🚨 ALERT: {severity.upper()} ACCIDENT DETECTED"
|
| 136 |
+
msg['From'] = ALERT_EMAIL_SENDER
|
| 137 |
+
msg['To'] = ALERT_EMAIL_RECEIVER
|
| 138 |
+
msg.set_content(f"Incident Report\nLocation: {location}\nSeverity: {severity}\nConfidence: {confidence}%\nTime: {datetime.datetime.now()}\n\nSystem has locked this stream for investigation.")
|
| 139 |
+
|
| 140 |
+
msg.add_attachment(buffer.tobytes(), maintype='image', subtype='jpeg', filename='incident.jpg')
|
| 141 |
+
|
| 142 |
+
server = smtplib.SMTP('smtp.gmail.com', 587, timeout=15)
|
| 143 |
+
server.ehlo()
|
| 144 |
+
server.starttls()
|
| 145 |
+
server.login(ALERT_EMAIL_SENDER, ALERT_EMAIL_PASSWORD)
|
| 146 |
+
server.send_message(msg)
|
| 147 |
+
server.quit()
|
| 148 |
+
|
| 149 |
+
print(f"📧 Dispatch Email Sent Successfully for Stream {stream_id}", flush=True)
|
| 150 |
+
except Exception as e:
|
| 151 |
+
print(f"📧 Email Failed to Send: {str(e)}", flush=True)
|
|
|
|
| 152 |
|
| 153 |
def run_temporal_analysis(stream_id, frames_3d_copy, prob_max, prob_mean, prob_min, yolo_max_conf, raw_frame, location):
|
| 154 |
state = active_streams.get(stream_id)
|
|
|
|
| 164 |
|
| 165 |
try:
|
| 166 |
ensemble_probs = model_svm.predict_proba(combined)[0]
|
| 167 |
+
final_idx = int(model_svm.predict(combined)[0])
|
| 168 |
confidence = float(ensemble_probs[final_idx] * 100)
|
| 169 |
severity = classes[final_idx]
|
| 170 |
+
except Exception as svm_err:
|
| 171 |
+
final_idx = min(int(np.argmax(prob_max)), 2) if np.sum(prob_max) > 0 else min(int(np.argmax(prob_3d)), 2)
|
| 172 |
+
confidence = float(np.max(prob_max)) * 100 if np.sum(prob_max) > 0 else float(np.max(prob_3d)) * 100
|
| 173 |
severity = classes[final_idx]
|
| 174 |
|
| 175 |
state["severity"] = severity.capitalize()
|
| 176 |
state["confidence"] = round(confidence, 1)
|
|
|
|
| 177 |
state["rcnn"] = round(float(np.max(prob_3d)) * 100, 1)
|
| 178 |
|
| 179 |
+
# 🚨 FIX: Strict Dual-Consensus checks to prevent IP camera frame-drop false positives!
|
| 180 |
+
# Requires 85% overall confidence AND at least 45% spatial confirmation of an actual object collision.
|
| 181 |
+
if confidence > 85 and yolo_max_conf > 45.0:
|
| 182 |
state["final_decision"] = True
|
| 183 |
state["label"] = f"Incident Logged: {severity.capitalize()}"
|
| 184 |
threading.Thread(target=process_accident_async, args=(stream_id, raw_frame, location, confidence, severity)).start()
|
|
|
|
| 210 |
|
| 211 |
@app.route('/init_stream', methods=['POST'])
|
| 212 |
def init_stream():
|
| 213 |
+
url = request.json.get('url', '').strip()
|
| 214 |
|
| 215 |
if 'youtube.com' in url or 'youtu.be' in url:
|
| 216 |
try:
|
| 217 |
+
ydl_opts = {'format': 'best', 'quiet': True, 'noplaylist': True}
|
| 218 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 219 |
info = ydl.extract_info(url, download=False)
|
| 220 |
+
if 'url' in info:
|
| 221 |
+
url = info['url']
|
| 222 |
+
elif 'formats' in info and len(info['formats']) > 0:
|
| 223 |
+
for f in reversed(info['formats']):
|
| 224 |
+
if f.get('vcodec') != 'none':
|
| 225 |
+
url = f['url']
|
| 226 |
+
break
|
| 227 |
except Exception as e:
|
| 228 |
print(f"yt-dlp extraction failed: {e}", flush=True)
|
| 229 |
|
|
|
|
| 312 |
target_frame = int(elapsed * fps)
|
| 313 |
frames_to_skip = target_frame - current_frame_idx
|
| 314 |
if frames_to_skip > 0:
|
| 315 |
+
for _ in range(min(frames_to_skip, 3)):
|
| 316 |
cap.grab()
|
| 317 |
current_frame_idx += 1
|
| 318 |
|
|
|
|
| 363 |
yolo_probs.append(probs)
|
| 364 |
if len(yolo_probs) > 10: yolo_probs.pop(0)
|
| 365 |
|
| 366 |
+
# 🚨 FIX: LIVE update of Spatial UI data so the screen doesn't feel stuck
|
| 367 |
+
if len(yolo_probs) > 0:
|
| 368 |
+
curr_max = np.max(yolo_probs, axis=0)
|
| 369 |
+
acc_conf = float(np.max(curr_max[:3])) * 100 if len(curr_max) >= 3 else float(np.max(curr_max)) * 100
|
| 370 |
+
state["cnn"] = round(acc_conf, 1)
|
| 371 |
+
if state["cnn"] > 0 and state["confidence"] == 0:
|
| 372 |
+
state["label"] = "Scanning Spatial Features..."
|
| 373 |
+
|
| 374 |
f_3d = cv2.resize(frame, (112, 112))
|
| 375 |
frames_3d.append(cv2.cvtColor(f_3d, cv2.COLOR_BGR2RGB))
|
| 376 |
if len(frames_3d) > 16: frames_3d.pop(0)
|
| 377 |
|
| 378 |
+
if len(frames_3d) >= 8 and frame_count % 10 == 0 and not state.get("is_analyzing"):
|
| 379 |
state["is_analyzing"] = True
|
| 380 |
+
|
| 381 |
+
analysis_frames = list(frames_3d)
|
| 382 |
+
while len(analysis_frames) < 16:
|
| 383 |
+
analysis_frames.append(analysis_frames[-1])
|
| 384 |
+
|
| 385 |
+
p_max = np.max(yolo_probs, axis=0) if len(yolo_probs) > 0 else np.zeros(4)
|
| 386 |
+
p_mean = np.mean(yolo_probs, axis=0) if len(yolo_probs) > 0 else np.zeros(4)
|
| 387 |
+
p_min = np.min(yolo_probs, axis=0) if len(yolo_probs) > 0 else np.zeros(4)
|
| 388 |
+
|
| 389 |
+
acc_max_conf = float(np.max(p_max[:3])) * 100 if len(p_max) >= 3 else float(np.max(p_max)) * 100
|
| 390 |
+
|
| 391 |
threading.Thread(target=run_temporal_analysis, args=(
|
| 392 |
+
stream_id, analysis_frames, p_max, p_mean, p_min, acc_max_conf, frame.copy(), location
|
| 393 |
)).start()
|
| 394 |
+
except Exception as e:
|
| 395 |
+
state["is_analyzing"] = False
|
| 396 |
|
| 397 |
_, buffer = cv2.imencode('.jpg', display_frame)
|
| 398 |
last_buffer = buffer.tobytes()
|