-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti_stream_detector.py
More file actions
214 lines (183 loc) · 7.48 KB
/
Copy pathmulti_stream_detector.py
File metadata and controls
214 lines (183 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import cv2
import numpy as np
import os
import winsound
import threading
import time
import tkinter as tk
from twilio.rest import Client
from PIL import Image, ImageTk
from dotenv import load_dotenv
# your detection model import (unchanged)
from detection import AccidentDetectionModel
load_dotenv()
# Twilio Secrets
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
# Global control for clean shutdown
stop_event = threading.Event()
def save_accident_photo(frame, source_name):
try:
current_date_time = time.strftime("%Y-%m-%d-%H%M%S")
directory = "accident_photos"
if not os.path.exists(directory):
os.makedirs(directory)
# include source name to distinguish streams
safe_name = "".join(c if c.isalnum() else "_" for c in source_name)
filename = f"{directory}/{safe_name}_{current_date_time}.jpg"
cv2.imwrite(filename, frame)
print(f"[{source_name}] Accident photo saved as {filename}")
except Exception as e:
print(f"[{source_name}] Error saving accident photo: {e}")
def call_ambulance(source_name):
try:
account_sid = TWILIO_ACCOUNT_SID
auth_token = TWILIO_AUTH_TOKEN
client = Client(account_sid, auth_token)
call = client.calls.create(
url="https://handler.twilio.com/twiml/EH7dd72f68b969250a748d2c9e8b503a1c",
to="+91 6005971380", # add verified ambulance number
from_="+1 765 234 3207"
)
print(f"[{source_name}] Call SID: {call.sid}")
except Exception as e:
print(f"[{source_name}] Error calling ambulance: {e}")
def show_alert_message(source_name):
# Play beep
try:
frequency = 2500
duration = 2000
winsound.Beep(frequency, duration)
except Exception:
pass
# Note: creating a Tk() instance inside a thread can work on many systems,
# but tkinter is not fully thread-safe across all platforms.
alert_window = tk.Tk()
alert_window.title(f"Alert - {source_name}")
alert_window.geometry("400x220")
alert_label = tk.Label(alert_window, text=f"Alert: Accident detected on {source_name}\nIs the Accident Critical?",
fg="black", font=("Helvetica", 14))
alert_label.pack(pady=10)
# GIF path optional
gif_path = "" # put GIF path if available
if gif_path:
try:
gif = Image.open(gif_path)
resized_gif = gif.resize((150, 100), Image.BICUBIC)
global gif_image # keep reference
gif_image = ImageTk.PhotoImage(resized_gif)
gif_label = tk.Label(alert_window, image=gif_image)
gif_label.pack()
except Exception as e:
print(f"[{source_name}] Error loading GIF: {e}")
def on_call_ambulance():
call_ambulance(source_name)
alert_window.destroy()
call_button = tk.Button(alert_window, text="Call Ambulance", command=on_call_ambulance)
call_button.pack(pady=6)
cancel_button = tk.Button(alert_window, text="Cancel", command=alert_window.destroy)
cancel_button.pack(pady=6)
alert_window.mainloop()
def start_alert_thread(source_name):
t = threading.Thread(target=show_alert_message, args=(source_name,), daemon=True)
t.start()
def process_stream(source, source_name, model_json="model.json", model_weights="model_weights.keras", prob_threshold=95):
"""
Worker for each video source.
source: video path or int (camera index) or rtsp url
source_name: friendly name for logs and filenames
"""
global stop_all
# create model instance inside thread (safer for concurrency)
try:
model = AccidentDetectionModel(model_json, model_weights)
except Exception as e:
print(f"[{source_name}] Error loading model: {e}")
return
cap = cv2.VideoCapture(source)
if not cap.isOpened():
print(f"[{source_name}] Unable to open source: {source}")
return
window_name = f"Stream - {source_name}"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
alarm_triggered = False
while not stop_all:
ret, frame = cap.read()
if not ret:
# If it's a file, exit; if it's a camera, retry a bit, else break
print(f"[{source_name}] No frame (end or disconnect). Exiting worker.")
break
# Preprocess (same as your code)
try:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
roi = cv2.resize(rgb_frame, (250, 250))
pred, prob = model.predict_accident(roi[np.newaxis, :, :])
except Exception as e:
print(f"[{source_name}] Prediction error: {e}")
pred, prob = None, None
if pred == "Accident":
# adapt to your model's prob shape - original used prob[0][0]
try:
probability = round(prob[0][0] * 100, 2)
except Exception:
try:
probability = round(float(prob) * 100, 2)
except Exception:
probability = 0.0
if probability > prob_threshold and not alarm_triggered:
print(f"[{source_name}] Accident detected with prob {probability}%")
save_accident_photo(frame, source_name)
alarm_triggered = True
# trigger alert UI and Twilio call (threads)
start_alert_thread(source_name)
# also call ambulance directly (non-blocking)
threading.Thread(target=call_ambulance, args=(source_name,), daemon=True).start()
# overlay (for UI)
cv2.rectangle(frame, (0, 0), (360, 40), (0, 0, 0), -1)
cv2.putText(frame, f"{pred} {probability}%", (10, 28), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)
# Show frame for this stream
cv2.imshow(window_name, frame)
# handle keypress - q will stop everything
if cv2.waitKey(1) & 0xFF == ord('q'):
print(f"[{source_name}] Q pressed - stopping all streams.")
stop_all = True
break
cap.release()
cv2.destroyWindow(window_name)
print(f"[{source_name}] Worker stopped.")
def start_multiple_streams(video_sources):
"""
video_sources: list of tuples (source, source_name)
source can be path (str) or int for webcam.
source_name is a friendly display name.
"""
threads = []
for source, name in video_sources:
t = threading.Thread(target=process_stream, args=(source, name), daemon=True)
t.start()
threads.append(t)
time.sleep(0.2) # small stagger to ease I/O spikes
try:
# wait for threads to finish or global stop
while any(t.is_alive() for t in threads) and not stop_all:
time.sleep(0.5)
except KeyboardInterrupt:
print("KeyboardInterrupt received. Stopping all streams.")
finally:
# signal all threads to stop and join
global stop_all
stop_all = True
for t in threads:
t.join(timeout=1.0)
cv2.destroyAllWindows()
print("All streams stopped.")
if __name__ == "__main__":
# Example sources: adjust these to your files, camera indices, or RTSP URLs.
# Use friendly names so saved images and logs are readable.
video_sources = [
("camera1.mp4", "camera2.mp4"),
("camera3.mp4", "camera4.mp4"),
(0, "Webcam0"), # integer camera index works too
# ("rtsp://user:pass@camera_ip/stream1", "Camera1"),
]
start_multiple_streams(video_sources)