Guru-25 commited on
Commit
b9c2e9c
·
verified ·
1 Parent(s): 1b36b40
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ venv
app.py CHANGED
@@ -7,6 +7,8 @@ import time
7
  from scripts.inference import GazePredictor
8
  from utils.ear_utils import BlinkDetector
9
  from gradio_webrtc import WebRTC
 
 
10
 
11
  def smooth_values(history, current_value, window_size=5):
12
  if current_value is not None:
@@ -31,11 +33,25 @@ def smooth_values(history, current_value, window_size=5):
31
  else:
32
  return history[-1] if history else None
33
 
34
- MODEL_PATH = os.path.join("models", "gaze_estimation_model.pth")
 
 
35
 
36
- gaze_predictor = GazePredictor(MODEL_PATH)
 
37
  blink_detector = BlinkDetector()
38
 
 
 
 
 
 
 
 
 
 
 
 
39
  gaze_history = []
40
  head_history = []
41
  ear_history = []
@@ -46,16 +62,22 @@ blink_count = 0
46
  start_time = 0
47
  is_unconscious = False
48
  frame_count_webcam = 0
 
 
 
 
49
 
 
50
  GAZE_STABILITY_THRESHOLD = 0.5
51
  TIME_THRESHOLD = 15
52
  BLINK_RATE_THRESHOLD = 1
53
  EYE_CLOSURE_THRESHOLD = 10
54
  HEAD_STABILITY_THRESHOLD = 0.05
 
55
 
56
  def analyze_video(input_video):
57
  cap = cv2.VideoCapture(input_video)
58
- local_gaze_predictor = GazePredictor(MODEL_PATH)
59
  local_blink_detector = BlinkDetector()
60
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
61
  temp_fd, temp_path = tempfile.mkstemp(suffix='.mp4')
@@ -180,9 +202,36 @@ def analyze_video(input_video):
180
  out.release()
181
  return temp_path
182
 
183
- def process_webrtc_frame(frame):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  global gaze_history, head_history, ear_history, stable_gaze_time, stable_head_time
185
- global eye_closed_time, blink_count, start_time, is_unconscious, frame_count_webcam
 
 
 
186
 
187
  if frame is None:
188
  return np.zeros((480, 640, 3), dtype=np.uint8)
@@ -289,17 +338,95 @@ def process_webrtc_frame(frame):
289
  cv2.putText(error_frame, f"Error: {e}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
290
  return error_frame
291
 
292
- def create_webcam_interface():
293
- with gr.Blocks() as webcam_demo:
294
- gr.Markdown("## Real-time Gaze Tracking via Webcam")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  with gr.Row():
296
  webcam_stream = WebRTC(label="Webcam Stream")
 
 
 
297
  webcam_stream.stream(
298
- fn=process_webrtc_frame,
299
  inputs=[webcam_stream],
300
- outputs=[webcam_stream]
 
301
  )
302
- return webcam_demo
 
 
 
303
 
304
  def create_video_interface():
305
  video_demo = gr.Interface(
@@ -312,9 +439,9 @@ def create_video_interface():
312
  return video_demo
313
 
314
  demo = gr.TabbedInterface(
315
- [create_video_interface(), create_webcam_interface()],
316
- ["Video Upload", "Webcam"],
317
- title="Gaze Tracker"
318
  )
319
 
320
  if __name__ == "__main__":
@@ -328,4 +455,6 @@ if __name__ == "__main__":
328
  start_time = 0
329
  is_unconscious = False
330
  frame_count_webcam = 0
 
 
331
  demo.launch()
 
7
  from scripts.inference import GazePredictor
8
  from utils.ear_utils import BlinkDetector
9
  from gradio_webrtc import WebRTC
10
+ from ultralytics import YOLO
11
+ import torch
12
 
13
  def smooth_values(history, current_value, window_size=5):
14
  if current_value is not None:
 
33
  else:
34
  return history[-1] if history else None
35
 
36
+ # --- Model Paths ---
37
+ GAZE_MODEL_PATH = os.path.join("models", "gaze_estimation_model.pth")
38
+ DISTRACTION_MODEL_PATH = "best.pt"
39
 
40
+ # --- Global Initializations ---
41
+ gaze_predictor = GazePredictor(GAZE_MODEL_PATH)
42
  blink_detector = BlinkDetector()
43
 
44
+ # Load Distraction Model
45
+ distraction_model = YOLO(DISTRACTION_MODEL_PATH)
46
+ distraction_model.to('cpu')
47
+
48
+ # Distraction Class Names
49
+ distraction_class_names = [
50
+ 'safe driving', 'drinking', 'eating', 'hair and makeup',
51
+ 'operating radio', 'talking on phone', 'talking to passenger'
52
+ ]
53
+
54
+ # --- Global State Variables for Gaze Webcam ---
55
  gaze_history = []
56
  head_history = []
57
  ear_history = []
 
62
  start_time = 0
63
  is_unconscious = False
64
  frame_count_webcam = 0
65
+ stop_gaze_processing = False
66
+
67
+ # --- Global State Variables for Distraction Webcam ---
68
+ stop_distraction_processing = False
69
 
70
+ # Constants
71
  GAZE_STABILITY_THRESHOLD = 0.5
72
  TIME_THRESHOLD = 15
73
  BLINK_RATE_THRESHOLD = 1
74
  EYE_CLOSURE_THRESHOLD = 10
75
  HEAD_STABILITY_THRESHOLD = 0.05
76
+ DISTRACTION_CONF_THRESHOLD = 0.1
77
 
78
  def analyze_video(input_video):
79
  cap = cv2.VideoCapture(input_video)
80
+ local_gaze_predictor = GazePredictor(GAZE_MODEL_PATH)
81
  local_blink_detector = BlinkDetector()
82
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
83
  temp_fd, temp_path = tempfile.mkstemp(suffix='.mp4')
 
202
  out.release()
203
  return temp_path
204
 
205
+ def terminate_gaze_stream():
206
+ global gaze_history, head_history, ear_history, stable_gaze_time, stable_head_time
207
+ global eye_closed_time, blink_count, start_time, is_unconscious, frame_count_webcam, stop_gaze_processing
208
+
209
+ print("Gaze Termination signal received. Stopping processing and resetting state.")
210
+ stop_gaze_processing = True
211
+ gaze_history = []
212
+ head_history = []
213
+ ear_history = []
214
+ stable_gaze_time = 0
215
+ stable_head_time = 0
216
+ eye_closed_time = 0
217
+ blink_count = 0
218
+ start_time = 0
219
+ is_unconscious = False
220
+ frame_count_webcam = 0
221
+ return "Gaze Processing Terminated. State Reset."
222
+
223
+ def terminate_distraction_stream():
224
+ global stop_distraction_processing
225
+ print("Distraction Termination signal received. Stopping processing.")
226
+ stop_distraction_processing = True
227
+ return "Distraction Processing Terminated."
228
+
229
+ def process_gaze_frame(frame):
230
  global gaze_history, head_history, ear_history, stable_gaze_time, stable_head_time
231
+ global eye_closed_time, blink_count, start_time, is_unconscious, frame_count_webcam, stop_gaze_processing
232
+
233
+ if stop_gaze_processing:
234
+ return np.zeros((480, 640, 3), dtype=np.uint8)
235
 
236
  if frame is None:
237
  return np.zeros((480, 640, 3), dtype=np.uint8)
 
338
  cv2.putText(error_frame, f"Error: {e}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
339
  return error_frame
340
 
341
+ def process_distraction_frame(frame):
342
+ global stop_distraction_processing
343
+
344
+ if stop_distraction_processing:
345
+ return np.zeros((480, 640, 3), dtype=np.uint8)
346
+
347
+ if frame is None:
348
+ return np.zeros((480, 640, 3), dtype=np.uint8)
349
+
350
+ try:
351
+ frame_to_process = frame
352
+ results = distraction_model(frame_to_process, conf=DISTRACTION_CONF_THRESHOLD, verbose=False)
353
+
354
+ display_text = "safe driving"
355
+ alarm_action = None
356
+
357
+ for result in results:
358
+ if result.boxes is not None and len(result.boxes) > 0:
359
+ boxes = result.boxes.xyxy.cpu().numpy()
360
+ scores = result.boxes.conf.cpu().numpy()
361
+ classes = result.boxes.cls.cpu().numpy()
362
+
363
+ if len(boxes) > 0:
364
+ max_score_idx = scores.argmax()
365
+ detected_action_idx = int(classes[max_score_idx])
366
+ if 0 <= detected_action_idx < len(distraction_class_names):
367
+ detected_action = distraction_class_names[detected_action_idx]
368
+ confidence = scores[max_score_idx]
369
+ display_text = f"{detected_action}: {confidence:.2f}"
370
+ if detected_action != 'safe driving':
371
+ alarm_action = detected_action
372
+ else:
373
+ print(f"Warning: Detected class index {detected_action_idx} out of bounds.")
374
+ display_text = "Unknown Detection"
375
+
376
+ if alarm_action:
377
+ print(f"ALARM: Unsafe behavior detected - {alarm_action}!")
378
+ cv2.putText(frame, f"ALARM: {alarm_action}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
379
+
380
+ text_color = (0, 255, 0) if alarm_action is None else (0, 255, 255)
381
+ cv2.putText(frame, display_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
382
+
383
+ return frame
384
+
385
+ except Exception as e:
386
+ print(f"Error processing distraction frame: {e}")
387
+ error_frame = np.zeros((480, 640, 3), dtype=np.uint8)
388
+ if not error_frame.flags.writeable:
389
+ error_frame = error_frame.copy()
390
+ cv2.putText(error_frame, f"Error: {e}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
391
+ return error_frame
392
+
393
+ def create_gaze_interface():
394
+ with gr.Blocks() as gaze_demo:
395
+ gr.Markdown("## Real-time Gaze & Drowsiness Tracking")
396
+ with gr.Row():
397
+ webcam_stream = WebRTC(label="Webcam Stream")
398
+ with gr.Row():
399
+ terminate_btn = gr.Button("Terminate Process")
400
+
401
+ webcam_stream.stream(
402
+ fn=process_gaze_frame,
403
+ inputs=[webcam_stream],
404
+ outputs=[webcam_stream],
405
+ api_name="gaze_stream"
406
+ )
407
+
408
+ terminate_btn.click(fn=terminate_gaze_stream, inputs=None, outputs=None)
409
+
410
+ return gaze_demo
411
+
412
+ def create_distraction_interface():
413
+ with gr.Blocks() as distraction_demo:
414
+ gr.Markdown("## Real-time Distraction Detection")
415
  with gr.Row():
416
  webcam_stream = WebRTC(label="Webcam Stream")
417
+ with gr.Row():
418
+ terminate_btn = gr.Button("Terminate Process")
419
+
420
  webcam_stream.stream(
421
+ fn=process_distraction_frame,
422
  inputs=[webcam_stream],
423
+ outputs=[webcam_stream],
424
+ api_name="distraction_stream"
425
  )
426
+
427
+ terminate_btn.click(fn=terminate_distraction_stream, inputs=None, outputs=None)
428
+
429
+ return distraction_demo
430
 
431
  def create_video_interface():
432
  video_demo = gr.Interface(
 
439
  return video_demo
440
 
441
  demo = gr.TabbedInterface(
442
+ [create_video_interface(), create_gaze_interface(), create_distraction_interface()],
443
+ ["Video Upload", "Gaze & Drowsiness", "Distraction Detection"],
444
+ title="Driver Monitoring System"
445
  )
446
 
447
  if __name__ == "__main__":
 
455
  start_time = 0
456
  is_unconscious = False
457
  frame_count_webcam = 0
458
+ stop_gaze_processing = False
459
+ stop_distraction_processing = False
460
  demo.launch()
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ab3c1caa87376460d162ee69a5874246127a2868ec4e13b25cf983cbda702839
3
+ size 6202851
req.txt ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.1.0
2
+ accelerate==1.4.0
3
+ altair==5.4.1
4
+ anyio==4.2.0
5
+ appdirs==1.4.4
6
+ appnope==0.1.3
7
+ argon2-cffi==23.1.0
8
+ argon2-cffi-bindings==21.2.0
9
+ arrow==1.3.0
10
+ asttokens==2.4.1
11
+ astunparse==1.6.3
12
+ async-lru==2.0.4
13
+ attrs==23.2.0
14
+ Babel==2.14.0
15
+ beautifulsoup4==4.12.3
16
+ bleach==6.1.0
17
+ bleak==0.21.1
18
+ blinker==1.7.0
19
+ build==1.2.2.post1
20
+ CacheControl==0.14.1
21
+ cachetools==5.5.0
22
+ catboost==1.2.7
23
+ certifi==2023.11.17
24
+ cffi==1.16.0
25
+ charset-normalizer==3.3.2
26
+ cleo==2.1.0
27
+ click==8.1.7
28
+ comm==0.2.1
29
+ contourpy==1.2.0
30
+ coolname==2.2.0
31
+ crashtest==0.4.1
32
+ cvzone==1.6.1
33
+ cycler==0.12.1
34
+ debugpy==1.8.0
35
+ decorator==5.1.1
36
+ deepface==0.0.93
37
+ defusedxml==0.7.1
38
+ distlib==0.3.9
39
+ dlib==19.24.6
40
+ docstring_parser==0.16
41
+ duckdb==1.2.1
42
+ dulwich==0.21.7
43
+ durationpy==0.9
44
+ ecdsa==0.19.0
45
+ emoji==2.14.1
46
+ et-xmlfile==1.1.0
47
+ eval_type_backport==0.2.2
48
+ Events==0.5
49
+ executing==2.0.1
50
+ faiss-cpu==1.10.0
51
+ fake-useragent==1.5.1
52
+ fastavro==1.9.7
53
+ fastjsonschema==2.19.1
54
+ filelock==3.16.1
55
+ filetype==1.2.0
56
+ fire==0.7.0
57
+ FLAML==2.3.4
58
+ Flask==3.0.1
59
+ Flask-Cors==5.0.0
60
+ flatbuffers==24.3.25
61
+ fonttools==4.48.1
62
+ fqdn==1.5.1
63
+ frozendict==2.4.6
64
+ frozenlist==1.5.0
65
+ fsspec==2024.6.1
66
+ future==1.0.0
67
+ gast==0.6.0
68
+ gdown==5.2.0
69
+ geomet==0.2.1.post1
70
+ git-lfs==1.6
71
+ gitdb==4.0.11
72
+ GitPython==3.1.43
73
+ google-crc32c==1.6.0
74
+ google-pasta==0.2.0
75
+ grandalf==0.8
76
+ graphviz==0.20.3
77
+ greenlet==3.1.1
78
+ grpcio==1.70.0
79
+ gunicorn==23.0.0
80
+ h11==0.14.0
81
+ h5py==3.12.1
82
+ hpack==4.1.0
83
+ html5lib==1.1
84
+ httplib2==0.22.0
85
+ httptools==0.6.4
86
+ httpx-sse==0.4.0
87
+ huggingface-hub==0.29.2
88
+ humanfriendly==10.0
89
+ hyperframe==6.1.0
90
+ idna==3.10
91
+ ijson==3.3.0
92
+ imbalanced-learn==0.13.0
93
+ imblearn==0.0
94
+ importlib_metadata==8.5.0
95
+ importlib_resources==6.5.2
96
+ inflection==0.5.1
97
+ iniconfig==2.0.0
98
+ installer==0.7.0
99
+ ipykernel==6.29.0
100
+ ipython==8.20.0
101
+ ipywidgets==8.1.1
102
+ isoduration==20.11.0
103
+ itsdangerous==2.1.2
104
+ jaraco.classes==3.4.0
105
+ jax==0.4.37
106
+ jaxlib==0.4.36
107
+ jedi==0.19.1
108
+ Jinja2==3.1.3
109
+ jiter==0.5.0
110
+ jmespath==1.0.1
111
+ joblib==1.4.2
112
+ jq==1.8.0
113
+ json5==0.10.0
114
+ json_repair==0.30.3
115
+ jsonpatch==1.33
116
+ jsonpath-python==1.0.6
117
+ jsonpickle==4.0.2
118
+ jsonpointer==2.4
119
+ jsonref==1.1.0
120
+ jsonschema==4.21.1
121
+ jsonschema-specifications==2023.12.1
122
+ jupyter==1.0.0
123
+ jupyter-console==6.6.3
124
+ jupyter-events==0.9.0
125
+ jupyter-lsp==2.2.2
126
+ jupyter_client==8.6.0
127
+ jupyter_core==5.7.1
128
+ jupyter_server==2.12.5
129
+ jupyter_server_terminals==0.5.2
130
+ jupyterlab==4.0.11
131
+ jupyterlab-widgets==3.0.9
132
+ jupyterlab_pygments==0.3.0
133
+ jupyterlab_server==2.25.2
134
+ keras==3.6.0
135
+ keyring==24.3.1
136
+ kiwisolver==1.4.5
137
+ labelImg==1.8.6
138
+ lark==1.2.2
139
+ libclang==18.1.1
140
+ logfire-api==3.7.1
141
+ loguru==0.7.3
142
+ lxml==5.3.1
143
+ lz4==4.3.3
144
+ magicattr==0.1.6
145
+ Markdown==3.7
146
+ markdown-it-py==3.0.0
147
+ MarkupSafe==3.0.2
148
+ matplotlib==3.8.2
149
+ matplotlib-inline==0.1.6
150
+ mdurl==0.1.2
151
+ mediapipe==0.10.20
152
+ milvus-lite==2.4.11
153
+ mistune==3.0.2
154
+ ml-dtypes==0.4.1
155
+ mmh3==5.1.0
156
+ monotonic==1.6
157
+ more-itertools==10.3.0
158
+ mpmath==1.3.0
159
+ msgpack==1.1.0
160
+ mtcnn==1.0.0
161
+ multidict==6.1.0
162
+ multitasking==0.0.11
163
+ mypy-extensions==1.0.0
164
+ namex==0.0.8
165
+ nanoid==2.0.0
166
+ narwhals==1.26.0
167
+ nbclient==0.9.0
168
+ nbconvert==7.14.2
169
+ nbformat==5.9.2
170
+ nest-asyncio==1.6.0
171
+ networkx==3.4.2
172
+ nltk==3.9.1
173
+ notebook==7.0.7
174
+ notebook_shim==0.2.3
175
+ numexpr==2.10.2
176
+ numpy==1.26.3
177
+ oauthlib==3.2.2
178
+ opencv-contrib-python==4.10.0.84
179
+ opencv-python==4.10.0.84
180
+ openinference-semantic-conventions==0.1.14
181
+ openpyxl==3.1.5
182
+ opentelemetry-util-http==0.51b0
183
+ opt_einsum==3.4.0
184
+ optree==0.13.0
185
+ orderly-set==5.3.0
186
+ orjson==3.10.15
187
+ overrides==7.6.0
188
+ packaging==24.2
189
+ pandas==2.2.0
190
+ pandocfilters==1.5.1
191
+ parso==0.8.3
192
+ passlib==1.7.4
193
+ patsy==1.0.1
194
+ peewee==3.17.9
195
+ pexpect==4.9.0
196
+ pgvector==0.3.6
197
+ pillow==11.1.0
198
+ pinecone-plugin-interface==0.0.7
199
+ pkginfo==1.12.0
200
+ platformdirs==4.3.6
201
+ plotly==6.0.0
202
+ pluggy==1.5.0
203
+ poetry==1.8.5
204
+ poetry-core==1.9.1
205
+ poetry-plugin-export==1.8.0
206
+ portalocker==2.10.1
207
+ primp==0.14.0
208
+ prometheus_client==0.21.1
209
+ prompt-toolkit==3.0.43
210
+ propcache==0.3.0
211
+ psutil==5.9.8
212
+ psycopg==3.2.5
213
+ psycopg-binary==3.2.5
214
+ psycopg2-binary==2.9.10
215
+ ptyprocess==0.7.0
216
+ pure-eval==0.2.2
217
+ py==1.11.0
218
+ py-cpuinfo==9.0.0
219
+ pyarrow==19.0.0
220
+ pyasn1==0.4.8
221
+ pyasn1_modules==0.4.1
222
+ pycparser==2.21
223
+ pycryptodome==3.21.0
224
+ pydantic_core==2.27.2
225
+ pydeck==0.9.1
226
+ Pygments==2.17.2
227
+ PyJWT==2.10.1
228
+ pyparsing==3.1.1
229
+ pypdf==5.1.0
230
+ PyPDF2==3.0.1
231
+ pypdfium2==4.30.1
232
+ pyperclip==1.9.0
233
+ PyPika==0.48.9
234
+ pyproject_hooks==1.2.0
235
+ PyQt5==5.15.11
236
+ PyQt5-Qt5==5.15.16
237
+ PyQt5_sip==12.17.0
238
+ PySocks==1.7.1
239
+ pytesseract==0.3.13
240
+ python-dateutil==2.8.2
241
+ python-dotenv==1.0.1
242
+ python-json-logger==2.0.7
243
+ python-lsp-jsonrpc==1.1.2
244
+ python-multipart==0.0.20
245
+ pyttsx3==2.98
246
+ pytube==15.0.0
247
+ pytz==2024.1
248
+ PyYAML==6.0.2
249
+ pyzmq==25.1.2
250
+ qtconsole==5.5.1
251
+ QtPy==2.4.1
252
+ Quandl==3.7.0
253
+ RapidFuzz==3.11.0
254
+ redis==5.2.1
255
+ referencing==0.32.1
256
+ regex==2024.11.6
257
+ requests==2.31.0
258
+ requests-toolbelt==1.0.0
259
+ retina-face==0.0.17
260
+ rfc3339-validator==0.1.4
261
+ rfc3986-validator==0.1.1
262
+ rich==13.7.1
263
+ rpds-py==0.17.1
264
+ rsa==4.9
265
+ ruff==0.9.10
266
+ safetensors==0.5.3
267
+ scikit-learn==1.5.0
268
+ scipy==1.12.0
269
+ seaborn==0.13.2
270
+ semver==3.0.4
271
+ Send2Trash==1.8.2
272
+ sentence-transformers==3.4.1
273
+ sentencepiece==0.2.0
274
+ shapely==2.0.7
275
+ shellingham==1.5.4
276
+ simsimd==6.2.1
277
+ six==1.16.0
278
+ sklearn-compat==0.1.3
279
+ smmap==5.0.1
280
+ sniffio==1.3.0
281
+ sounddevice==0.5.1
282
+ soupsieve==2.5
283
+ SQLAlchemy==2.0.38
284
+ sseclient-py==1.8.0
285
+ stack-data==0.6.3
286
+ statsmodels==0.14.4
287
+ streamlit==1.41.1
288
+ StrEnum==0.4.15
289
+ sympy==1.13.1
290
+ tabulate==0.9.0
291
+ tenacity==8.5.0
292
+ tensorboard==2.18.0
293
+ tensorboard-data-server==0.7.2
294
+ tensorflow==2.18.0
295
+ tensorflow-io-gcs-filesystem==0.37.1
296
+ termcolor==2.5.0
297
+ terminado==0.18.0
298
+ tf_keras==2.18.0
299
+ threadpoolctl==3.5.0
300
+ tinycss2==1.2.1
301
+ tokenizers==0.21.0
302
+ toml==0.10.2
303
+ tomli==2.2.1
304
+ tomli_w==1.2.0
305
+ tomlkit==0.13.2
306
+ torch==2.6.0
307
+ torchvision==0.21.0
308
+ tornado==6.4
309
+ tqdm==4.66.5
310
+ traitlets==5.14.1
311
+ transformers==4.49.0
312
+ tree-sitter==0.23.2
313
+ tree-sitter-python==0.23.6
314
+ trove-classifiers==2024.10.21.16
315
+ typer==0.12.3
316
+ types-cachetools==5.5.0.20240820
317
+ types-python-dateutil==2.8.19.20240106
318
+ types-requests==2.32.0.20250306
319
+ typing-inspection==0.4.0
320
+ typing_extensions==4.12.2
321
+ tzdata==2023.4
322
+ ujson==5.10.0
323
+ ultralytics==8.3.93
324
+ ultralytics-thop==2.0.14
325
+ uncurl==0.0.11
326
+ uri-template==1.3.0
327
+ uritemplate==4.1.1
328
+ urllib3==2.3.0
329
+ uuid6==2024.7.10
330
+ uv==0.6.5
331
+ uvloop==0.21.0
332
+ validators==0.34.0
333
+ virtualenv==20.28.1
334
+ wcwidth==0.2.13
335
+ webcolors==1.13
336
+ webencodings==0.5.1
337
+ websocket-client==1.7.0
338
+ websockets==12.0
339
+ Werkzeug==3.0.1
340
+ wget==3.2
341
+ widgetsnbextension==4.0.9
342
+ wrapt==1.16.0
343
+ xattr==1.1.0
344
+ xgboost==2.1.2
345
+ xmltodict==0.14.2
346
+ xxhash==3.5.0
347
+ yfinance==0.2.55
348
+ zipp==3.21.0
349
+ zstandard==0.23.0
requirements.txt CHANGED
@@ -9,4 +9,354 @@ gradio>=5.0.0
9
  gradio-webrtc==0.0.4
10
  tensorflow
11
  pygame
12
- twilio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  gradio-webrtc==0.0.4
10
  tensorflow
11
  pygame
12
+ twilio
13
+
14
+ absl-py==2.1.0
15
+ accelerate==1.4.0
16
+ altair==5.4.1
17
+ anyio==4.2.0
18
+ appdirs==1.4.4
19
+ appnope==0.1.3
20
+ argon2-cffi==23.1.0
21
+ argon2-cffi-bindings==21.2.0
22
+ arrow==1.3.0
23
+ asttokens==2.4.1
24
+ astunparse==1.6.3
25
+ async-lru==2.0.4
26
+ attrs==23.2.0
27
+ Babel==2.14.0
28
+ beautifulsoup4==4.12.3
29
+ bleach==6.1.0
30
+ bleak==0.21.1
31
+ blinker==1.7.0
32
+ build==1.2.2.post1
33
+ CacheControl==0.14.1
34
+ cachetools==5.5.0
35
+ catboost==1.2.7
36
+ certifi==2023.11.17
37
+ cffi==1.16.0
38
+ charset-normalizer==3.3.2
39
+ cleo==2.1.0
40
+ click==8.1.7
41
+ comm==0.2.1
42
+ contourpy==1.2.0
43
+ coolname==2.2.0
44
+ crashtest==0.4.1
45
+ cvzone==1.6.1
46
+ cycler==0.12.1
47
+ debugpy==1.8.0
48
+ decorator==5.1.1
49
+ deepface==0.0.93
50
+ defusedxml==0.7.1
51
+ distlib==0.3.9
52
+ dlib==19.24.6
53
+ docstring_parser==0.16
54
+ duckdb==1.2.1
55
+ dulwich==0.21.7
56
+ durationpy==0.9
57
+ ecdsa==0.19.0
58
+ emoji==2.14.1
59
+ et-xmlfile==1.1.0
60
+ eval_type_backport==0.2.2
61
+ Events==0.5
62
+ executing==2.0.1
63
+ faiss-cpu==1.10.0
64
+ fake-useragent==1.5.1
65
+ fastavro==1.9.7
66
+ fastjsonschema==2.19.1
67
+ filelock==3.16.1
68
+ filetype==1.2.0
69
+ fire==0.7.0
70
+ FLAML==2.3.4
71
+ Flask==3.0.1
72
+ Flask-Cors==5.0.0
73
+ flatbuffers==24.3.25
74
+ fonttools==4.48.1
75
+ fqdn==1.5.1
76
+ frozendict==2.4.6
77
+ frozenlist==1.5.0
78
+ fsspec==2024.6.1
79
+ future==1.0.0
80
+ gast==0.6.0
81
+ gdown==5.2.0
82
+ geomet==0.2.1.post1
83
+ git-lfs==1.6
84
+ gitdb==4.0.11
85
+ GitPython==3.1.43
86
+ google-crc32c==1.6.0
87
+ google-pasta==0.2.0
88
+ grandalf==0.8
89
+ graphviz==0.20.3
90
+ greenlet==3.1.1
91
+ grpcio==1.70.0
92
+ gunicorn==23.0.0
93
+ h11==0.14.0
94
+ h5py==3.12.1
95
+ hpack==4.1.0
96
+ html5lib==1.1
97
+ httplib2==0.22.0
98
+ httptools==0.6.4
99
+ httpx-sse==0.4.0
100
+ huggingface-hub==0.29.2
101
+ humanfriendly==10.0
102
+ hyperframe==6.1.0
103
+ idna==3.10
104
+ ijson==3.3.0
105
+ imbalanced-learn==0.13.0
106
+ imblearn==0.0
107
+ importlib_metadata==8.5.0
108
+ importlib_resources==6.5.2
109
+ inflection==0.5.1
110
+ iniconfig==2.0.0
111
+ installer==0.7.0
112
+ ipykernel==6.29.0
113
+ ipython==8.20.0
114
+ ipywidgets==8.1.1
115
+ isoduration==20.11.0
116
+ itsdangerous==2.1.2
117
+ jaraco.classes==3.4.0
118
+ jax==0.4.37
119
+ jaxlib==0.4.36
120
+ jedi==0.19.1
121
+ Jinja2==3.1.3
122
+ jiter==0.5.0
123
+ jmespath==1.0.1
124
+ joblib==1.4.2
125
+ jq==1.8.0
126
+ json5==0.10.0
127
+ json_repair==0.30.3
128
+ jsonpatch==1.33
129
+ jsonpath-python==1.0.6
130
+ jsonpickle==4.0.2
131
+ jsonpointer==2.4
132
+ jsonref==1.1.0
133
+ jsonschema==4.21.1
134
+ jsonschema-specifications==2023.12.1
135
+ jupyter==1.0.0
136
+ jupyter-console==6.6.3
137
+ jupyter-events==0.9.0
138
+ jupyter-lsp==2.2.2
139
+ jupyter_client==8.6.0
140
+ jupyter_core==5.7.1
141
+ jupyter_server==2.12.5
142
+ jupyter_server_terminals==0.5.2
143
+ jupyterlab==4.0.11
144
+ jupyterlab-widgets==3.0.9
145
+ jupyterlab_pygments==0.3.0
146
+ jupyterlab_server==2.25.2
147
+ keras==3.6.0
148
+ keyring==24.3.1
149
+ kiwisolver==1.4.5
150
+ labelImg==1.8.6
151
+ lark==1.2.2
152
+ libclang==18.1.1
153
+ logfire-api==3.7.1
154
+ loguru==0.7.3
155
+ lxml==5.3.1
156
+ lz4==4.3.3
157
+ magicattr==0.1.6
158
+ Markdown==3.7
159
+ markdown-it-py==3.0.0
160
+ MarkupSafe==3.0.2
161
+ matplotlib==3.8.2
162
+ matplotlib-inline==0.1.6
163
+ mdurl==0.1.2
164
+ mediapipe==0.10.20
165
+ milvus-lite==2.4.11
166
+ mistune==3.0.2
167
+ ml-dtypes==0.4.1
168
+ mmh3==5.1.0
169
+ monotonic==1.6
170
+ more-itertools==10.3.0
171
+ mpmath==1.3.0
172
+ msgpack==1.1.0
173
+ mtcnn==1.0.0
174
+ multidict==6.1.0
175
+ multitasking==0.0.11
176
+ mypy-extensions==1.0.0
177
+ namex==0.0.8
178
+ nanoid==2.0.0
179
+ narwhals==1.26.0
180
+ nbclient==0.9.0
181
+ nbconvert==7.14.2
182
+ nbformat==5.9.2
183
+ nest-asyncio==1.6.0
184
+ networkx==3.4.2
185
+ nltk==3.9.1
186
+ notebook==7.0.7
187
+ notebook_shim==0.2.3
188
+ numexpr==2.10.2
189
+ numpy==1.26.3
190
+ oauthlib==3.2.2
191
+ opencv-contrib-python==4.10.0.84
192
+ opencv-python==4.10.0.84
193
+ openinference-semantic-conventions==0.1.14
194
+ openpyxl==3.1.5
195
+ opentelemetry-util-http==0.51b0
196
+ opt_einsum==3.4.0
197
+ optree==0.13.0
198
+ orderly-set==5.3.0
199
+ orjson==3.10.15
200
+ overrides==7.6.0
201
+ packaging==24.2
202
+ pandas==2.2.0
203
+ pandocfilters==1.5.1
204
+ parso==0.8.3
205
+ passlib==1.7.4
206
+ patsy==1.0.1
207
+ peewee==3.17.9
208
+ pexpect==4.9.0
209
+ pgvector==0.3.6
210
+ pillow==11.1.0
211
+ pinecone-plugin-interface==0.0.7
212
+ pkginfo==1.12.0
213
+ platformdirs==4.3.6
214
+ plotly==6.0.0
215
+ pluggy==1.5.0
216
+ poetry==1.8.5
217
+ poetry-core==1.9.1
218
+ poetry-plugin-export==1.8.0
219
+ portalocker==2.10.1
220
+ primp==0.14.0
221
+ prometheus_client==0.21.1
222
+ prompt-toolkit==3.0.43
223
+ propcache==0.3.0
224
+ psutil==5.9.8
225
+ psycopg==3.2.5
226
+ psycopg-binary==3.2.5
227
+ psycopg2-binary==2.9.10
228
+ ptyprocess==0.7.0
229
+ pure-eval==0.2.2
230
+ py==1.11.0
231
+ py-cpuinfo==9.0.0
232
+ pyarrow==19.0.0
233
+ pyasn1==0.4.8
234
+ pyasn1_modules==0.4.1
235
+ pycparser==2.21
236
+ pycryptodome==3.21.0
237
+ pydantic_core==2.27.2
238
+ pydeck==0.9.1
239
+ Pygments==2.17.2
240
+ PyJWT==2.10.1
241
+ pyparsing==3.1.1
242
+ pypdf==5.1.0
243
+ PyPDF2==3.0.1
244
+ pypdfium2==4.30.1
245
+ pyperclip==1.9.0
246
+ PyPika==0.48.9
247
+ pyproject_hooks==1.2.0
248
+ PyQt5==5.15.11
249
+ PyQt5-Qt5==5.15.16
250
+ PyQt5_sip==12.17.0
251
+ PySocks==1.7.1
252
+ pytesseract==0.3.13
253
+ python-dateutil==2.8.2
254
+ python-dotenv==1.0.1
255
+ python-json-logger==2.0.7
256
+ python-lsp-jsonrpc==1.1.2
257
+ python-multipart==0.0.20
258
+ pyttsx3==2.98
259
+ pytube==15.0.0
260
+ pytz==2024.1
261
+ PyYAML==6.0.2
262
+ pyzmq==25.1.2
263
+ qtconsole==5.5.1
264
+ QtPy==2.4.1
265
+ Quandl==3.7.0
266
+ RapidFuzz==3.11.0
267
+ redis==5.2.1
268
+ referencing==0.32.1
269
+ regex==2024.11.6
270
+ requests==2.31.0
271
+ requests-toolbelt==1.0.0
272
+ retina-face==0.0.17
273
+ rfc3339-validator==0.1.4
274
+ rfc3986-validator==0.1.1
275
+ rich==13.7.1
276
+ rpds-py==0.17.1
277
+ rsa==4.9
278
+ ruff==0.9.10
279
+ safetensors==0.5.3
280
+ scikit-learn==1.5.0
281
+ scipy==1.12.0
282
+ seaborn==0.13.2
283
+ semver==3.0.4
284
+ Send2Trash==1.8.2
285
+ sentence-transformers==3.4.1
286
+ sentencepiece==0.2.0
287
+ shapely==2.0.7
288
+ shellingham==1.5.4
289
+ simsimd==6.2.1
290
+ six==1.16.0
291
+ sklearn-compat==0.1.3
292
+ smmap==5.0.1
293
+ sniffio==1.3.0
294
+ sounddevice==0.5.1
295
+ soupsieve==2.5
296
+ SQLAlchemy==2.0.38
297
+ sseclient-py==1.8.0
298
+ stack-data==0.6.3
299
+ statsmodels==0.14.4
300
+ streamlit==1.41.1
301
+ StrEnum==0.4.15
302
+ sympy==1.13.1
303
+ tabulate==0.9.0
304
+ tenacity==8.5.0
305
+ tensorboard==2.18.0
306
+ tensorboard-data-server==0.7.2
307
+ tensorflow==2.18.0
308
+ tensorflow-io-gcs-filesystem==0.37.1
309
+ termcolor==2.5.0
310
+ terminado==0.18.0
311
+ tf_keras==2.18.0
312
+ threadpoolctl==3.5.0
313
+ tinycss2==1.2.1
314
+ tokenizers==0.21.0
315
+ toml==0.10.2
316
+ tomli==2.2.1
317
+ tomli_w==1.2.0
318
+ tomlkit==0.13.2
319
+ torch==2.6.0
320
+ torchvision==0.21.0
321
+ tornado==6.4
322
+ tqdm==4.66.5
323
+ traitlets==5.14.1
324
+ transformers==4.49.0
325
+ tree-sitter==0.23.2
326
+ tree-sitter-python==0.23.6
327
+ trove-classifiers==2024.10.21.16
328
+ typer==0.12.3
329
+ types-cachetools==5.5.0.20240820
330
+ types-python-dateutil==2.8.19.20240106
331
+ types-requests==2.32.0.20250306
332
+ typing-inspection==0.4.0
333
+ typing_extensions==4.12.2
334
+ tzdata==2023.4
335
+ ujson==5.10.0
336
+ ultralytics==8.3.93
337
+ ultralytics-thop==2.0.14
338
+ uncurl==0.0.11
339
+ uri-template==1.3.0
340
+ uritemplate==4.1.1
341
+ urllib3==2.3.0
342
+ uuid6==2024.7.10
343
+ uv==0.6.5
344
+ uvloop==0.21.0
345
+ validators==0.34.0
346
+ virtualenv==20.28.1
347
+ wcwidth==0.2.13
348
+ webcolors==1.13
349
+ webencodings==0.5.1
350
+ websocket-client==1.7.0
351
+ websockets==12.0
352
+ Werkzeug==3.0.1
353
+ wget==3.2
354
+ widgetsnbextension==4.0.9
355
+ wrapt==1.16.0
356
+ xattr==1.1.0
357
+ xgboost==2.1.2
358
+ xmltodict==0.14.2
359
+ xxhash==3.5.0
360
+ yfinance==0.2.55
361
+ zipp==3.21.0
362
+ zstandard==0.23.0
scripts/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (149 Bytes). View file
 
scripts/__pycache__/inference.cpython-312.pyc ADDED
Binary file (4.08 kB). View file
 
tes_opencv2.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from ultralytics import YOLO
4
+ import torch
5
+ import os
6
+
7
+ # Verify MPS availability
8
+ print(f"MPS available: {torch.backends.mps.is_available()}")
9
+
10
+ # Load the trained model
11
+ model = YOLO('best.pt') # Path to your trained model
12
+ if torch.backends.mps.is_available():
13
+ model.to('mps')
14
+
15
+ # Define class names (match your custom_dataset.yaml)
16
+ class_names = ['safe driving', 'drinking', 'eating', 'hair and makeup', 'operating radio', 'talking on phone', 'talking to passenger']
17
+
18
+ # Open the webcam (0 is usually the default camera)
19
+ cap = cv2.VideoCapture(0)
20
+ if not cap.isOpened():
21
+ print("Error: Could not open webcam.")
22
+ exit()
23
+
24
+ # Set frame width and height
25
+ cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
26
+ cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 320)
27
+
28
+ def trigger_alarm(action):
29
+ """Trigger an alarm if the action is not 'safe driving'."""
30
+ if action != 'safe driving':
31
+ print(f"ALARM: Unsafe behavior detected - {action}!") # Text-based alarm for Mac
32
+ # Optional sound alarm (uncomment and add alarm.wav if using playsound)
33
+ # try:
34
+ # from playsound import playsound
35
+ # playsound(os.path.join(os.getcwd(), 'alarm.wav'))
36
+ # except Exception as e:
37
+ # print(f"Sound alarm failed: {e}")
38
+
39
+ while cap.isOpened():
40
+ # Read frame from webcam
41
+ ret, frame = cap.read()
42
+ if not ret:
43
+ print("Error: Could not read frame.")
44
+ break
45
+
46
+ # Perform inference with confidence threshold
47
+ results = model(frame, conf=0.1) # Lower confidence threshold for more detections
48
+
49
+ # Process results
50
+ display_text = "Safe driving"
51
+ for result in results:
52
+ boxes = result.boxes.xyxy.cpu().numpy() # Bounding box coordinates
53
+ scores = result.boxes.conf.cpu().numpy() # Confidence scores
54
+ classes = result.boxes.cls.cpu().numpy() # Class indices
55
+
56
+ if len(boxes) > 0:
57
+ # Get the most confident detection
58
+ max_score_idx = scores.argmax()
59
+ detected_action = class_names[int(classes[max_score_idx])]
60
+ confidence = scores[max_score_idx]
61
+ display_text = f"{detected_action}: {confidence:.2f}"
62
+ # Trigger alarm if not safe driving
63
+ trigger_alarm(detected_action)
64
+
65
+ # Display the action in top-left corner (no bounding boxes)
66
+ cv2.putText(frame, display_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
67
+
68
+ # Display the result
69
+ cv2.imshow('YOLO Webcam Detection', frame)
70
+
71
+ # Break loop if 'q' is pressed
72
+ if cv2.waitKey(1) & 0xFF == ord('q'):
73
+ break
74
+
75
+ # Cleanup
76
+ cap.release()
77
+ cv2.destroyAllWindows()
utils/__pycache__/ear_utils.cpython-312.pyc ADDED
Binary file (3.63 kB). View file
 
utils/__pycache__/preprocess.cpython-312.pyc ADDED
Binary file (625 Bytes). View file