Kiransubedi545 commited on
Commit
f165f91
·
verified ·
1 Parent(s): f767478

Update helmet_detect_alert.py

Browse files
Files changed (1) hide show
  1. helmet_detect_alert.py +22 -6
helmet_detect_alert.py CHANGED
@@ -1,7 +1,6 @@
1
  import cv2
2
  import os
3
- from gtts import gTTS
4
- import pygame
5
  from ultralytics import YOLO
6
  import numpy as np
7
  from tkinter import filedialog, messagebox
@@ -63,10 +62,18 @@ def log_alert(message):
63
  writer.writerow([timestamp, message])
64
 
65
  # ----------------- Run YOLO Prediction + Alerts -----------------
66
- def detect_and_alert(video_path, model, confidence=CONFIDENCE_THRESHOLD):
67
  cap = cv2.VideoCapture(video_path)
68
  frame_count = 0
69
  alert_cooldown = 15
 
 
 
 
 
 
 
 
70
  while cap.isOpened():
71
  ret, frame = cap.read()
72
  if not ret:
@@ -96,14 +103,19 @@ def detect_and_alert(video_path, model, confidence=CONFIDENCE_THRESHOLD):
96
  log_alert(f"Alert at frame {frame_count}: No helmet at ({hx1}, {hy1})")
97
  if alert_triggered and frame_count % alert_cooldown == 0:
98
  speak_alert("Alert! Person without helmet detected", ALERT_LANGUAGE)
 
 
99
  cv2.imshow("Helmet Detection", annotated_frame)
100
  if cv2.waitKey(1) & 0xFF == ord('q'):
101
  break
102
  cap.release()
 
 
103
  cv2.destroyAllWindows()
104
 
105
  # ----------------- Detect from Multiple Images -----------------
106
- def detect_from_images(image_paths, model, confidence=CONFIDENCE_THRESHOLD):
 
107
  for image_path in image_paths:
108
  frame = cv2.imread(image_path)
109
  results = model.predict(source=frame, conf=confidence, verbose=False)
@@ -126,11 +138,15 @@ def detect_from_images(image_paths, model, confidence=CONFIDENCE_THRESHOLD):
126
  cv2.putText(annotated_frame, "⚠ No Helmet!", (hx1, hy1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
127
  alert_triggered = True
128
  log_alert(f"Alert: No helmet at ({hx1}, {hy1}) in image {image_path}")
 
 
 
 
129
  if alert_triggered:
130
  speak_alert("Alert! Person without helmet detected", ALERT_LANGUAGE)
131
- cv2.imshow(f"Result - {os.path.basename(image_path)}", annotated_frame)
132
- cv2.waitKey(0)
133
  cv2.destroyAllWindows()
 
 
134
 
135
  # ----------------- Snapshot -----------------
136
  def save_snapshot(frame, path="snapshot.jpg"):
 
1
  import cv2
2
  import os
3
+ import pyttsx3
 
4
  from ultralytics import YOLO
5
  import numpy as np
6
  from tkinter import filedialog, messagebox
 
62
  writer.writerow([timestamp, message])
63
 
64
  # ----------------- Run YOLO Prediction + Alerts -----------------
65
+ def detect_and_alert(video_path, output_path, model, confidence=CONFIDENCE_THRESHOLD):
66
  cap = cv2.VideoCapture(video_path)
67
  frame_count = 0
68
  alert_cooldown = 15
69
+ out = None
70
+ if output_path:
71
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
72
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
73
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
74
+ fps = cap.get(cv2.CAP_PROP_FPS)
75
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
76
+
77
  while cap.isOpened():
78
  ret, frame = cap.read()
79
  if not ret:
 
103
  log_alert(f"Alert at frame {frame_count}: No helmet at ({hx1}, {hy1})")
104
  if alert_triggered and frame_count % alert_cooldown == 0:
105
  speak_alert("Alert! Person without helmet detected", ALERT_LANGUAGE)
106
+ if out:
107
+ out.write(annotated_frame)
108
  cv2.imshow("Helmet Detection", annotated_frame)
109
  if cv2.waitKey(1) & 0xFF == ord('q'):
110
  break
111
  cap.release()
112
+ if out:
113
+ out.release()
114
  cv2.destroyAllWindows()
115
 
116
  # ----------------- Detect from Multiple Images -----------------
117
+ def detect_from_images(image_paths, model, confidence=CONFIDENCE_THRESHOLD, return_path=False):
118
+ result_paths = []
119
  for image_path in image_paths:
120
  frame = cv2.imread(image_path)
121
  results = model.predict(source=frame, conf=confidence, verbose=False)
 
138
  cv2.putText(annotated_frame, "⚠ No Helmet!", (hx1, hy1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
139
  alert_triggered = True
140
  log_alert(f"Alert: No helmet at ({hx1}, {hy1}) in image {image_path}")
141
+ output_path = f"output/pred_{os.path.basename(image_path)}"
142
+ os.makedirs("output", exist_ok=True)
143
+ cv2.imwrite(output_path, annotated_frame)
144
+ result_paths.append(output_path)
145
  if alert_triggered:
146
  speak_alert("Alert! Person without helmet detected", ALERT_LANGUAGE)
 
 
147
  cv2.destroyAllWindows()
148
+ if return_path:
149
+ return result_paths if len(result_paths) > 1 else result_paths[0]
150
 
151
  # ----------------- Snapshot -----------------
152
  def save_snapshot(frame, path="snapshot.jpg"):