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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])
|
51 |
+
B = np.linalg.norm(eye[2] - eye[4])
|
52 |
+
C = np.linalg.norm(eye[0] - eye[3])
|
53 |
+
return (A + B) / (2.0 * C)
|
54 |
+
|
55 |
+
def analyze_texture(self, face_region):
|
56 |
+
gray_face = cv2.cvtColor(face_region, cv2.COLOR_BGR2GRAY)
|
57 |
+
lbp = feature.local_binary_pattern(gray_face, P=8, R=1, method="uniform")
|
58 |
+
lbp_hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, 58), range=(0, 58))
|
59 |
+
lbp_hist = lbp_hist.astype("float")
|
60 |
+
lbp_hist /= (lbp_hist.sum() + 1e-5)
|
61 |
+
return np.sum(lbp_hist[:10]) > 0.3
|
62 |
+
|
63 |
+
def detect_hand_gesture(self, frame):
|
64 |
+
results = self.hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
65 |
+
return results.multi_hand_landmarks is not None
|
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 |
+
# Check if a face is detected
|
108 |
+
if len(faces) == 0:
|
109 |
+
return None
|
110 |
+
|
111 |
+
# Assume the first detected face is the subject
|
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 |
+
# Check if conditions for image capture are met
|
199 |
+
if self.blink_count >= 5 and not self.image_captured:
|
200 |
+
# Capture the image and reset blink count
|
201 |
+
self.save_image(frame)
|
202 |
+
self.blink_count = 0
|
203 |
+
self.image_captured = True
|
204 |
+
|
205 |
+
return frame
|
206 |
+
|
207 |
+
def save_image(self, frame):
|
208 |
+
# Implement logic to save the frame as an image
|
209 |
+
timestamp = int(time.time())
|
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 |
+
def get_captured_image(self):
|
216 |
+
# Return the captured image with preprocessing applied (if necessary)
|
217 |
+
captured_frame = self.captured_image
|
218 |
+
if captured_frame is not None:
|
219 |
+
return captured_frame
|
220 |
+
return None
|
221 |
+
|
222 |
+
if __name__ == "__main__":
|
223 |
+
anti_spoofing_system = AntiSpoofingSystem()
|
224 |
+
anti_spoofing_system.run()
|