Update anti_spoofing.py
Browse files- anti_spoofing.py +28 -186
anti_spoofing.py
CHANGED
@@ -1,50 +1,15 @@
|
|
|
|
1 |
import cv2
|
2 |
import dlib
|
3 |
import numpy as np
|
4 |
-
import os
|
5 |
-
import time
|
6 |
-
import mediapipe as mp
|
7 |
from skimage import feature
|
8 |
|
|
|
9 |
class AntiSpoofingSystem:
|
10 |
def __init__(self):
|
11 |
self.detector = dlib.get_frontal_face_detector()
|
12 |
-
self.anti_spoofing_completed = False
|
13 |
-
self.blink_count = 0
|
14 |
-
self.image_captured = False
|
15 |
-
self.captured_image = None
|
16 |
self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
|
17 |
-
|
18 |
-
self.mp_hands = mp.solutions.hands
|
19 |
-
self.hands = self.mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.7)
|
20 |
-
|
21 |
-
self.cap = cv2.VideoCapture(0)
|
22 |
-
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
|
23 |
-
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
|
24 |
-
|
25 |
-
self.save_directory = "Person"
|
26 |
-
if not os.path.exists(self.save_directory):
|
27 |
-
os.makedirs(self.save_directory)
|
28 |
-
|
29 |
-
self.net_smartphone = cv2.dnn.readNet('yolov4.weights', 'Pretrained_yolov4 (1).cfg')
|
30 |
-
with open('PreTrained_coco.names', 'r') as f:
|
31 |
-
self.classes_smartphone = f.read().strip().split('\n')
|
32 |
-
|
33 |
self.EAR_THRESHOLD = 0.25
|
34 |
-
self.BLINK_CONSEC_FRAMES = 4
|
35 |
-
|
36 |
-
self.left_eye_state = False
|
37 |
-
self.right_eye_state = False
|
38 |
-
self.left_blink_counter = 0
|
39 |
-
self.right_blink_counter = 0
|
40 |
-
|
41 |
-
self.smartphone_detected = False
|
42 |
-
self.smartphone_detection_frame_interval = 30
|
43 |
-
self.frame_count = 0
|
44 |
-
|
45 |
-
# New attributes for student data
|
46 |
-
self.student_id = None
|
47 |
-
self.student_name = None
|
48 |
|
49 |
def calculate_ear(self, eye):
|
50 |
A = np.linalg.norm(eye[1] - eye[5])
|
@@ -60,165 +25,42 @@ class AntiSpoofingSystem:
|
|
60 |
lbp_hist /= (lbp_hist.sum() + 1e-5)
|
61 |
return np.sum(lbp_hist[:10]) > 0.3
|
62 |
|
63 |
-
def
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
def detect_smartphone(self, frame):
|
68 |
-
if self.frame_count % self.smartphone_detection_frame_interval == 0:
|
69 |
-
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
|
70 |
-
self.net_smartphone.setInput(blob)
|
71 |
-
output_layers_names = self.net_smartphone.getUnconnectedOutLayersNames()
|
72 |
-
detections = self.net_smartphone.forward(output_layers_names)
|
73 |
-
|
74 |
-
for detection in detections:
|
75 |
-
for obj in detection:
|
76 |
-
scores = obj[5:]
|
77 |
-
class_id = np.argmax(scores)
|
78 |
-
confidence = scores[class_id]
|
79 |
-
if confidence > 0.5 and self.classes_smartphone[class_id] == 'cell phone':
|
80 |
-
center_x = int(obj[0] * frame.shape[1])
|
81 |
-
center_y = int(obj[1] * frame.shape[0])
|
82 |
-
width = int(obj[2] * frame.shape[1])
|
83 |
-
height = int(obj[3] * frame.shape[0])
|
84 |
-
left = int(center_x - width / 2)
|
85 |
-
top = int(center_y - height / 2)
|
86 |
-
|
87 |
-
cv2.rectangle(frame, (left, top), (left + width, top + height), (0, 0, 255), 2)
|
88 |
-
cv2.putText(frame, 'Smartphone Detected', (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
89 |
-
|
90 |
-
self.smartphone_detected = True
|
91 |
-
self.left_blink_counter = 0
|
92 |
-
self.right_blink_counter = 0
|
93 |
-
return
|
94 |
-
|
95 |
-
self.frame_count += 1
|
96 |
-
self.smartphone_detected = False
|
97 |
-
|
98 |
-
def access_verified_image(self):
|
99 |
-
ret, frame = self.cap.read()
|
100 |
-
if not ret:
|
101 |
-
return None
|
102 |
-
|
103 |
-
# Perform anti-spoofing checks
|
104 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
105 |
faces = self.detector(gray)
|
106 |
-
|
107 |
-
#
|
108 |
if len(faces) == 0:
|
109 |
-
return
|
110 |
-
|
111 |
-
#
|
112 |
face = faces[0]
|
113 |
landmarks = self.predictor(gray, face)
|
114 |
-
|
115 |
-
# Check for blink detection (assuming you have this method correctly implemented)
|
116 |
leftEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(36, 42)])
|
117 |
rightEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(42, 48)])
|
|
|
118 |
ear_left = self.calculate_ear(leftEye)
|
119 |
ear_right = self.calculate_ear(rightEye)
|
120 |
-
if not self.detect_blink(ear_left, ear_right):
|
121 |
-
return None
|
122 |
-
|
123 |
-
# Check for hand gesture (assuming you have this method correctly implemented)
|
124 |
-
if not self.detect_hand_gesture(frame):
|
125 |
-
return None
|
126 |
-
|
127 |
-
# Check if a smartphone is detected
|
128 |
-
self.detect_smartphone(frame)
|
129 |
-
if self.smartphone_detected:
|
130 |
-
return None
|
131 |
-
|
132 |
-
# Check texture for liveness (assuming you have this method correctly implemented)
|
133 |
-
(x, y, w, h) = (face.left(), face.top(), face.width(), face.height())
|
134 |
-
expanded_region = frame[max(y - h // 2, 0):min(y + 3 * h // 2, frame.shape[0]),
|
135 |
-
max(x - w // 2, 0):min(x + 3 * w // 2, frame.shape[1])]
|
136 |
-
if not self.analyze_texture(expanded_region):
|
137 |
-
return None
|
138 |
-
|
139 |
-
return frame
|
140 |
-
|
141 |
-
def detect_blink(self, left_ear, right_ear):
|
142 |
-
if self.smartphone_detected:
|
143 |
-
self.left_eye_state = False
|
144 |
-
self.right_eye_state = False
|
145 |
-
self.left_blink_counter = 0
|
146 |
-
self.right_blink_counter = 0
|
147 |
-
return False
|
148 |
-
|
149 |
-
if left_ear < self.EAR_THRESHOLD:
|
150 |
-
if not self.left_eye_state:
|
151 |
-
self.left_eye_state = True
|
152 |
-
else:
|
153 |
-
if self.left_eye_state:
|
154 |
-
self.left_eye_state = False
|
155 |
-
self.left_blink_counter += 1
|
156 |
-
|
157 |
-
if right_ear < self.EAR_THRESHOLD:
|
158 |
-
if not self.right_eye_state:
|
159 |
-
self.right_eye_state = True
|
160 |
-
else:
|
161 |
-
if self.right_eye_state:
|
162 |
-
self.right_eye_state = False
|
163 |
-
self.right_blink_counter += 1
|
164 |
-
|
165 |
-
if self.left_blink_counter > 0 and self.right_blink_counter > 0:
|
166 |
-
self.left_blink_counter = 0
|
167 |
-
self.right_blink_counter = 0
|
168 |
-
return True
|
169 |
-
else:
|
170 |
-
return False
|
171 |
-
|
172 |
-
def run(self):
|
173 |
-
ret, frame = self.cap.read()
|
174 |
-
if not ret:
|
175 |
-
return None
|
176 |
-
|
177 |
-
# Detect smartphone in the frame
|
178 |
-
self.detect_smartphone(frame)
|
179 |
-
|
180 |
-
if self.smartphone_detected:
|
181 |
-
cv2.putText(frame, "Mobile phone detected, can't record attendance", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
182 |
-
else:
|
183 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
184 |
-
faces = self.detector(gray)
|
185 |
-
|
186 |
-
for face in faces:
|
187 |
-
landmarks = self.predictor(gray, face)
|
188 |
-
leftEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(36, 42)])
|
189 |
-
rightEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(42, 48)])
|
190 |
-
|
191 |
-
ear_left = self.calculate_ear(leftEye)
|
192 |
-
ear_right = self.calculate_ear(rightEye)
|
193 |
-
|
194 |
-
if self.detect_blink(ear_left, ear_right):
|
195 |
-
self.blink_count += 1
|
196 |
-
cv2.putText(frame, f"Blink Count: {self.blink_count}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
self.save_image(frame)
|
202 |
-
self.blink_count = 0
|
203 |
-
self.image_captured = True
|
204 |
|
205 |
-
|
|
|
206 |
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
image_name = f"captured_{timestamp}.png"
|
211 |
-
cv2.imwrite(os.path.join(self.save_directory, image_name), frame)
|
212 |
-
self.captured_image = frame
|
213 |
-
print(f"Image captured and saved as {image_name}")
|
214 |
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
221 |
|
222 |
-
|
223 |
-
|
224 |
-
anti_spoofing_system.run()
|
|
|
1 |
+
import gradio as gr
|
2 |
import cv2
|
3 |
import dlib
|
4 |
import numpy as np
|
|
|
|
|
|
|
5 |
from skimage import feature
|
6 |
|
7 |
+
# Initialize your AntiSpoofingSystem class as previously defined
|
8 |
class AntiSpoofingSystem:
|
9 |
def __init__(self):
|
10 |
self.detector = dlib.get_frontal_face_detector()
|
|
|
|
|
|
|
|
|
11 |
self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
self.EAR_THRESHOLD = 0.25
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def calculate_ear(self, eye):
|
15 |
A = np.linalg.norm(eye[1] - eye[5])
|
|
|
25 |
lbp_hist /= (lbp_hist.sum() + 1e-5)
|
26 |
return np.sum(lbp_hist[:10]) > 0.3
|
27 |
|
28 |
+
def process_image(self, image):
|
29 |
+
# Convert the image to grayscale and detect faces
|
30 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
faces = self.detector(gray)
|
32 |
+
|
33 |
+
# If no face is detected, return a message
|
34 |
if len(faces) == 0:
|
35 |
+
return "No face detected. Please try again."
|
36 |
+
|
37 |
+
# Process the first detected face
|
38 |
face = faces[0]
|
39 |
landmarks = self.predictor(gray, face)
|
|
|
|
|
40 |
leftEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(36, 42)])
|
41 |
rightEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(42, 48)])
|
42 |
+
|
43 |
ear_left = self.calculate_ear(leftEye)
|
44 |
ear_right = self.calculate_ear(rightEye)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Determine if a blink is detected
|
47 |
+
blink_detected = (ear_left < self.EAR_THRESHOLD and ear_right < self.EAR_THRESHOLD)
|
48 |
+
return "Blink detected!" if blink_detected else "No blink detected."
|
|
|
|
|
|
|
49 |
|
50 |
+
# Define the Gradio interface
|
51 |
+
anti_spoofing_system = AntiSpoofingSystem()
|
52 |
|
53 |
+
def detect_blink(image):
|
54 |
+
result = anti_spoofing_system.process_image(image)
|
55 |
+
return result
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
iface = gr.Interface(
|
58 |
+
fn=detect_blink,
|
59 |
+
inputs=gr.Image(shape=(720, 1280)),
|
60 |
+
outputs="text",
|
61 |
+
title="Anti-Spoofing Detection System",
|
62 |
+
description="Upload an image with a face to detect if a blink is detected."
|
63 |
+
)
|
64 |
|
65 |
+
# Launch the Gradio interface
|
66 |
+
iface.launch()
|
|