forked from SomdattaNag/Security-Screening-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
243 lines (196 loc) · 9.22 KB
/
main.py
File metadata and controls
243 lines (196 loc) · 9.22 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import cv2
import numpy as np
import os
import face_recognition
from playsound import playsound
from message import send_call, send_email, send_sms
import time
import sys
from gui.gui import guiwindow
import datetime
import pickle
# --- Pause/Resume global state ---
is_paused = False
pause_start_time = None
paused_names_time = {}
last_frame = None # To freeze video while paused
prev_time = 0
IMAGE_LOG_DIR = "image_logs"
CSV_LOG_DIR = "csv_logs"
#create log if it doesn't exist
for directory in [IMAGE_LOG_DIR, CSV_LOG_DIR]:
if not os.path.exists(directory):
os.makedirs(directory)
# Alarms
def threat_alarm():
playsound("alarms/threat.wav")
def safe_alarm():
playsound("alarms/safe.wav")
encodings_path = "encodings/face_encodings.pkl"
if os.path.exists(encodings_path):
with open(encodings_path, "rb") as f:
face_encode, face_name = pickle.load(f)
else:
raise FileNotFoundError("❌ Face encodings not found. Please run `save_encodings.py` first.")
print("🔧 Security Screening System - Full Face Recognition Mode")
print("📋 Status Messages Feature: ✅ Active")
print("🔍 Face Recognition: ✅ Using face_recognition library")
print("🎯 Identity Matching: ✅ Real confidence scores from face encodings")
try:
face_cap = cv2.VideoCapture(0)
if not face_cap.isOpened():
raise RuntimeError(" Error: Could not access the webcam. Please check if it's connected, or if it's being used by another application.")
except Exception as e:
print(f"Webcam access error: {e}")
sys.exit()
detection_time = {}
last_alarmed = {}
current_status = "System ready - Please position yourself in front of the camera"
status_color = '#00ff00' # Green for ready state
def log_event(event,name, confidence):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_entry = f"{timestamp}, {event}, {name}, {confidence}\n"
log_file = os.path.join(CSV_LOG_DIR, "security_log.csv")
with open(log_file, "a") as f:
f.write(log_entry)
def get_frame():
global prev_time, current_status, status_color, is_paused, pause_start_time, paused_names_time, last_frame
# Read frame from camera
ret, frame = face_cap.read()
if not ret:
current_status = " Camera error - Please check camera connection"
status_color = '#ff0000' # Red for error
print("❌ Camera read failed.")
return None
frame = cv2.flip(frame, 1)
curr_time = time.time()
fps = 1 / (curr_time - prev_time) if (curr_time - prev_time) > 0 else 0
prev_time = curr_time
cv2.putText(frame, f"FPS: {int(fps)}", (520,250), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 255), 3)
# ------------- Pause Handling -----------------
if is_paused:
current_status = "⏸️ Detection Paused. Click Resume to continue."
status_color = '#888888'
# Return last frame to freeze the GUI
if last_frame is not None:
return last_frame
else:
last_frame = frame.copy()
return last_frame
else:
last_frame = frame.copy()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
curr_names = []
# Update status based on face detection
if len(face_locations) == 0:
current_status = "👁️ Scanning for faces... Please position yourself in front of the camera"
status_color = '#ffff00' # Yellow for scanning
elif len(face_locations) > 1:
current_status = "⚠️ Multiple faces detected - Please ensure only one person is in frame"
status_color = '#ff8800' # Orange for warning
for face_encoding, face_location in zip(face_encodings, face_locations):
face_distances = face_recognition.face_distance(face_encode, face_encoding)
best_match_index = np.argmin(face_distances)
name = "No match"
if face_distances[best_match_index] < 0.4:
name = face_name[best_match_index]
confidence = (1 - face_distances[best_match_index]) * 100
confidence_text = f"{confidence:.2f}%"
# Update status for recognized face
current_status = f"✅ Match found: {name} (Confidence: {confidence:.1f}%)"
status_color = '#00ff00' # Green for match
else:
confidence_text = f"{(1 - face_distances[best_match_index]) * 100:.2f}%"
# Update status for unrecognized face
current_status = "❌ No match detected. You are safe to go."
status_color = '#ff0000' # Red for no match
curr_names.append(name)
top, right, bottom, left = [coord * 4 for coord in face_location]
color = (0, 255, 0) if name != "No match" else (0, 0, 255)
cv2.rectangle(frame, (left, top), (right, bottom), color, 2)
cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
cv2.putText(frame, confidence_text, (right, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
#added: a countdown timer for each detected face
if name in detection_time:
scan_time = curr_time - detection_time[name]
remaining_time = max(0, 10 - int(scan_time))
cv2.putText(frame, f"{remaining_time:.1f}s", (left, top + 20), cv2.FONT_HERSHEY_COMPLEX, 0.5, color, 2)
# Update status during countdown
if remaining_time > 0:
current_status = f"⏱️ Please stand still for {remaining_time:.0f} seconds - Processing..."
status_color = '#ffaa00' # Orange for processing
#starting timer
detected_now = set(curr_names)
for name in detected_now:
if name not in detection_time:
#first detection
detection_time[name] = curr_time
last_alarmed[name] = 0
for name in detected_now:
scan_time = curr_time - detection_time[name]
if scan_time >= 10 and (curr_time - last_alarmed.get(name, 0)) >= 30:
if name != "No match":
current_status = f"🚨 THREAT DETECTED: {name} - Security alert triggered!"
status_color = '#ff0000' # Red for threat
threat_alarm()
if confidence > 90:
send_call(name, confidence)
send_sms(name, confidence)
current_status = f"🚨 Very HIGH THREAT DETECTED: {name} - Security alert triggered! Call and SMS sent."
status_color = '#8B0000' #Crimson for very high alert
log_event("Very High Threat", name, confidence)
elif confidence >80:
send_email(name, frame, confidence)
send_sms(name, confidence)
current_status = f"🚨 HIGH THREAT DETECTED: {name} - Security alert triggered! Email and SMS sent."
status_color = '#ff0000' # Red for high threat
log_event("High Threat", name, confidence)
elif confidence >70:
send_email(name, frame, confidence)
current_status = f"🚨 MEDIUM THREAT DETECTED: {name} - Security alert triggered! Email sent."
status_color = '#ff8800' # Orange for medium threat
log_event("Medium Threat", name, confidence)
elif confidence > 60:
current_status = f"🚨 LOW THREAT DETECTED: {name} - Security alert triggered!"
status_color = '#ffaa00'
log_event("Low Threat", name, confidence)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{name}_{timestamp}.jpg"
filepath = os.path.join(IMAGE_LOG_DIR, filename)
cv2.imwrite(filepath, frame)
else:
current_status = "✅ SCAN COMPLETE: No match detected - You are safe to proceed"
status_color = '#00ff00' # Green for safe
safe_alarm()
last_alarmed[name] = curr_time
for name in list(detection_time.keys()):
if name not in detected_now:
del detection_time[name]
return frame
def get_status():
"""Return current status message and color for GUI"""
return current_status, status_color
# ------------- Add Pause/Resume Attribute Interface for GUI -----------
def set_pause_vars(
get_is_paused, set_is_paused,
get_pause_start_time, set_pause_start_time,
get_paused_names_time, set_paused_names_time,
):
global is_paused, pause_start_time, paused_names_time
# Not strictly needed with globals (optional if handling state in one file), but included for clarity & future-proofing
# Start GUI
video_app = guiwindow(get_frame_callback=get_frame, status_callback=get_status)
# Set pause state references for bidirectional access
video_app.set_pause_vars(
lambda: is_paused,
lambda v: globals().__setitem__('is_paused', v),
lambda: pause_start_time,
lambda v: globals().__setitem__('pause_start_time', v),
lambda: paused_names_time,
lambda v: globals().__setitem__('paused_names_time', v),
lambda: detection_time
)
video_app.run()