Upload 2 files
Browse files- anti_spoofing.py +232 -224
- app.py +61 -0
anti_spoofing.py
CHANGED
@@ -1,224 +1,232 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
self.
|
14 |
-
self.
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
self.mp_hands =
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
self.cap.
|
23 |
-
self.cap.set(cv2.
|
24 |
-
|
25 |
-
|
26 |
-
if
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
self.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
self.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
self.
|
44 |
-
|
45 |
-
|
46 |
-
self.
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
def
|
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 |
-
if not
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import all the libraries
|
2 |
+
import cv2
|
3 |
+
import dlib
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
import mediapipe as mp
|
8 |
+
from skimage import feature
|
9 |
+
|
10 |
+
# I'm setting up the face and hand detectors here.
|
11 |
+
class AntiSpoofingSystem:
|
12 |
+
def __init__(self):
|
13 |
+
self.detector = dlib.get_frontal_face_detector()
|
14 |
+
self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
|
15 |
+
|
16 |
+
# Here I initialize MediaPipe for hand gesture detection.
|
17 |
+
self.mp_hands = mp.solutions.hands
|
18 |
+
self.hands = self.mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.7)
|
19 |
+
|
20 |
+
|
21 |
+
# This code is for Webcam if you have Jetson kit change value from 0 to 1.
|
22 |
+
self.cap = cv2.VideoCapture(0)
|
23 |
+
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
|
24 |
+
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
|
25 |
+
|
26 |
+
# I create a directory to save the captured images if it doesn't exist.
|
27 |
+
self.save_directory = "Person"
|
28 |
+
if not os.path.exists(self.save_directory):
|
29 |
+
os.makedirs(self.save_directory)
|
30 |
+
|
31 |
+
|
32 |
+
# Iam loading the Pre-trained model to detect smartphones.
|
33 |
+
self.net_smartphone = cv2.dnn.readNet('yolov4.weights', 'PreTrained_yolov4.cfg')
|
34 |
+
with open('PreTrained_coco.names', 'r') as f:
|
35 |
+
self.classes_smartphone = f.read().strip().split('\n')
|
36 |
+
|
37 |
+
|
38 |
+
# Setting some thresholds for eye aspect ratio to detect blinks.
|
39 |
+
self.EAR_THRESHOLD = 0.2
|
40 |
+
self.BLINK_CONSEC_FRAMES = 4
|
41 |
+
|
42 |
+
# Initializing some variables to keep track of eye states and blink counts.
|
43 |
+
self.left_eye_state = False
|
44 |
+
self.right_eye_state = False
|
45 |
+
self.left_blink_counter = 0
|
46 |
+
self.right_blink_counter = 0
|
47 |
+
|
48 |
+
# Variables to manage smartphone detection.
|
49 |
+
self.smartphone_detected = False
|
50 |
+
self.smartphone_detection_frame_interval = 10
|
51 |
+
self.frame_count = 0
|
52 |
+
|
53 |
+
# New attributes for student data
|
54 |
+
self.student_id = None
|
55 |
+
self.student_name = None
|
56 |
+
|
57 |
+
|
58 |
+
# It is calculating the eye aspect ratio to detect blinks.
|
59 |
+
def calculate_ear(self, eye):
|
60 |
+
A = np.linalg.norm(eye[1] - eye[5])
|
61 |
+
B = np.linalg.norm(eye[2] - eye[4])
|
62 |
+
C = np.linalg.norm(eye[0] - eye[3])
|
63 |
+
return (A + B) / (2.0 * C)
|
64 |
+
|
65 |
+
|
66 |
+
# Analyzing the texture of the face to check for liveness.
|
67 |
+
def analyze_texture(self, face_region):
|
68 |
+
gray_face = cv2.cvtColor(face_region, cv2.COLOR_BGR2GRAY)
|
69 |
+
lbp = feature.local_binary_pattern(gray_face, P=8, R=1, method="uniform")
|
70 |
+
lbp_hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, 58), range=(0, 58))
|
71 |
+
lbp_hist = lbp_hist.astype("float")
|
72 |
+
lbp_hist /= (lbp_hist.sum() + 1e-5)
|
73 |
+
return np.sum(lbp_hist[:10]) > 0.3
|
74 |
+
|
75 |
+
# Detecting hand using MediaPipe.
|
76 |
+
def detect_hand_gesture(self, frame):
|
77 |
+
results = self.hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
78 |
+
return results.multi_hand_landmarks is not None
|
79 |
+
|
80 |
+
# Detecting smartphones in the frame to prevent System Bypass.
|
81 |
+
def detect_smartphone(self, frame):
|
82 |
+
if self.frame_count % self.smartphone_detection_frame_interval == 0:
|
83 |
+
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (224, 224), swapRB=True, crop=False)
|
84 |
+
self.net_smartphone.setInput(blob)
|
85 |
+
output_layers_names = self.net_smartphone.getUnconnectedOutLayersNames()
|
86 |
+
detections = self.net_smartphone.forward(output_layers_names)
|
87 |
+
|
88 |
+
for detection in detections:
|
89 |
+
for obj in detection:
|
90 |
+
scores = obj[5:]
|
91 |
+
class_id = np.argmax(scores)
|
92 |
+
confidence = scores[class_id]
|
93 |
+
if confidence > 0.3 and self.classes_smartphone[class_id] == 'cell phone':
|
94 |
+
center_x = int(obj[0] * frame.shape[1])
|
95 |
+
center_y = int(obj[1] * frame.shape[0])
|
96 |
+
width = int(obj[2] * frame.shape[1])
|
97 |
+
height = int(obj[3] * frame.shape[0])
|
98 |
+
left = int(center_x - width / 2)
|
99 |
+
top = int(center_y - height / 2)
|
100 |
+
|
101 |
+
cv2.rectangle(frame, (left, top), (left + width, top + height), (0, 0, 255), 2)
|
102 |
+
cv2.putText(frame, 'Smartphone Detected', (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
103 |
+
|
104 |
+
self.smartphone_detected = True
|
105 |
+
self.left_blink_counter = 0
|
106 |
+
self.right_blink_counter = 0
|
107 |
+
return
|
108 |
+
|
109 |
+
self.frame_count += 1
|
110 |
+
self.smartphone_detected = False
|
111 |
+
|
112 |
+
# Checking if the user blinked to confirm their presence.
|
113 |
+
def detect_blink(self, left_ear, right_ear):
|
114 |
+
if self.smartphone_detected:
|
115 |
+
self.left_eye_state = False
|
116 |
+
self.right_eye_state = False
|
117 |
+
self.left_blink_counter = 0
|
118 |
+
self.right_blink_counter = 0
|
119 |
+
return False
|
120 |
+
|
121 |
+
# Incrementing blink counter if a blink is detected.
|
122 |
+
if left_ear < self.EAR_THRESHOLD:
|
123 |
+
if not self.left_eye_state:
|
124 |
+
self.left_eye_state = True
|
125 |
+
else:
|
126 |
+
if self.left_eye_state:
|
127 |
+
self.left_eye_state = False
|
128 |
+
self.left_blink_counter += 1
|
129 |
+
|
130 |
+
if right_ear < self.EAR_THRESHOLD:
|
131 |
+
if not self.right_eye_state:
|
132 |
+
self.right_eye_state = True
|
133 |
+
else:
|
134 |
+
if self.right_eye_state:
|
135 |
+
self.right_eye_state = False
|
136 |
+
self.right_blink_counter += 1
|
137 |
+
|
138 |
+
|
139 |
+
# Resetting blink counters after a successful blink detection.
|
140 |
+
if self.left_blink_counter > 0 and self.right_blink_counter > 0:
|
141 |
+
self.left_blink_counter = 0
|
142 |
+
self.right_blink_counter = 0
|
143 |
+
return True
|
144 |
+
else:
|
145 |
+
return False
|
146 |
+
|
147 |
+
# Main loop to process the video feed.
|
148 |
+
def run(self, update_frame_callback=None):
|
149 |
+
blink_count = 0
|
150 |
+
hand_gesture_detected = False
|
151 |
+
image_captured = False
|
152 |
+
last_event_time = time.time()
|
153 |
+
event_timeout = 60
|
154 |
+
message_displayed = False
|
155 |
+
|
156 |
+
while True:
|
157 |
+
ret, frame = self.cap.read()
|
158 |
+
if not ret:
|
159 |
+
break
|
160 |
+
|
161 |
+
# Detecting smartphones in the frame.
|
162 |
+
self.detect_smartphone(frame)
|
163 |
+
|
164 |
+
# Displaying a warning if a smartphone is detected.
|
165 |
+
if self.smartphone_detected:
|
166 |
+
cv2.putText(frame, "Mobile phone detected, can't record attendance", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
167 |
+
blink_count = 0
|
168 |
+
|
169 |
+
# Processing each frame to detect faces, blinks, and hand gestures.
|
170 |
+
if not self.smartphone_detected:
|
171 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
172 |
+
faces = self.detector(gray)
|
173 |
+
|
174 |
+
for face in faces:
|
175 |
+
landmarks = self.predictor(gray, face)
|
176 |
+
leftEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(36, 42)])
|
177 |
+
rightEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(42, 48)])
|
178 |
+
|
179 |
+
ear_left = self.calculate_ear(leftEye)
|
180 |
+
ear_right = self.calculate_ear(rightEye)
|
181 |
+
|
182 |
+
if self.detect_blink(ear_left, ear_right):
|
183 |
+
blink_count += 1
|
184 |
+
|
185 |
+
# Prionting and Incrementing blink Count
|
186 |
+
cv2.putText(frame, f"Blink Count: {blink_count}", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
187 |
+
|
188 |
+
hand_gesture_detected = self.detect_hand_gesture(frame)
|
189 |
+
|
190 |
+
# Indicating when a hand gesture is detected.
|
191 |
+
if hand_gesture_detected:
|
192 |
+
cv2.putText(frame, "Hand Gesture Detected", (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
193 |
+
|
194 |
+
(x, y, w, h) = (face.left(), face.top(), face.width(), face.height())
|
195 |
+
expanded_region = frame[max(y - h // 2, 0):min(y + 3 * h // 2, frame.shape[0]),
|
196 |
+
max(x - w // 2, 0):min(x + 3 * w // 2, frame.shape[1])]
|
197 |
+
|
198 |
+
# Checking if the conditions are met to capture the image.
|
199 |
+
if blink_count >= 5 and hand_gesture_detected and self.analyze_texture(expanded_region) and not message_displayed:
|
200 |
+
cv2.putText(frame, "Please hold still for 2 seconds...", (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
201 |
+
cv2.imshow("Frame", frame)
|
202 |
+
cv2.waitKey(1)
|
203 |
+
time.sleep(2)
|
204 |
+
message_displayed = True
|
205 |
+
|
206 |
+
if message_displayed and not image_captured:
|
207 |
+
timestamp = int(time.time())
|
208 |
+
picture_name = f"{self.student_id}_{timestamp}.jpg"
|
209 |
+
cv2.imwrite(os.path.join(self.save_directory, picture_name), expanded_region)
|
210 |
+
image_captured = True
|
211 |
+
|
212 |
+
if update_frame_callback:
|
213 |
+
update_frame_callback(frame)
|
214 |
+
|
215 |
+
cv2.imshow("Frame", frame)
|
216 |
+
if image_captured or (time.time() - last_event_time > event_timeout and not hand_gesture_detected):
|
217 |
+
break
|
218 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
219 |
+
break
|
220 |
+
|
221 |
+
self.cap.release()
|
222 |
+
cv2.destroyAllWindows()
|
223 |
+
|
224 |
+
#If person if real and did all the required features then his attendance will be marked if not then it will print no person detected.
|
225 |
+
if image_captured:
|
226 |
+
print(f"Person detected. Face image captured and saved as {picture_name}.")
|
227 |
+
elif not hand_gesture_detected:
|
228 |
+
print("No real person detected")
|
229 |
+
|
230 |
+
if __name__ == "__main__":
|
231 |
+
anti_spoofing_system = AntiSpoofingSystem()
|
232 |
+
anti_spoofing_system.run()
|
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import tkinter as tk
|
3 |
+
from tkinter import messagebox
|
4 |
+
from PIL import Image, ImageTk
|
5 |
+
import threading
|
6 |
+
import cv2
|
7 |
+
from anti_spoofing import AntiSpoofingSystem
|
8 |
+
|
9 |
+
class AntiSpoofingGUI:
|
10 |
+
def __init__(self, anti_spoofing_system):
|
11 |
+
self.anti_spoofing_system = anti_spoofing_system
|
12 |
+
self.window = tk.Tk()
|
13 |
+
self.window.title("Anti-Spoofing System")
|
14 |
+
|
15 |
+
self.student_id_label = tk.Label(self.window, text="Student ID:")
|
16 |
+
self.student_id_label.pack()
|
17 |
+
self.student_id_entry = tk.Entry(self.window)
|
18 |
+
self.student_id_entry.pack()
|
19 |
+
|
20 |
+
self.student_name_label = tk.Label(self.window, text="Student Name:")
|
21 |
+
self.student_name_label.pack()
|
22 |
+
self.student_name_entry = tk.Entry(self.window)
|
23 |
+
self.student_name_entry.pack()
|
24 |
+
|
25 |
+
self.start_button = tk.Button(self.window, text="Start", command=self.start_anti_spoofing)
|
26 |
+
self.start_button.pack()
|
27 |
+
|
28 |
+
self.image_label = tk.Label(self.window)
|
29 |
+
self.image_label.pack()
|
30 |
+
|
31 |
+
# Create a PhotoImage object to use for the video feed
|
32 |
+
self.photo = ImageTk.PhotoImage("RGB", (640, 480))
|
33 |
+
|
34 |
+
def start_anti_spoofing(self):
|
35 |
+
self.student_id = self.student_id_entry.get()
|
36 |
+
self.student_name = self.student_name_entry.get()
|
37 |
+
|
38 |
+
if not self.student_id or not self.student_name:
|
39 |
+
messagebox.showwarning("Warning", "Please enter both Student ID and Name")
|
40 |
+
return
|
41 |
+
|
42 |
+
threading.Thread(target=self.run_anti_spoofing, daemon=True).start()
|
43 |
+
|
44 |
+
def run_anti_spoofing(self):
|
45 |
+
self.anti_spoofing_system.student_id = self.student_id
|
46 |
+
self.anti_spoofing_system.student_name = self.student_name
|
47 |
+
self.anti_spoofing_system.run(self.update_frame)
|
48 |
+
|
49 |
+
def update_frame(self, frame):
|
50 |
+
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
|
51 |
+
self.photo.paste(Image.fromarray(cv2image))
|
52 |
+
self.image_label.config(image=self.photo)
|
53 |
+
self.image_label.update_idletasks()
|
54 |
+
|
55 |
+
def run(self):
|
56 |
+
self.window.mainloop()
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
anti_spoofing_system = AntiSpoofingSystem()
|
60 |
+
gui = AntiSpoofingGUI(anti_spoofing_system)
|
61 |
+
gui.run()
|