Update anti_spoofing.py
Browse files- anti_spoofing.py +53 -26
anti_spoofing.py
CHANGED
@@ -5,7 +5,6 @@ import os
|
|
5 |
import time
|
6 |
import mediapipe as mp
|
7 |
from skimage import feature
|
8 |
-
import gradio as gr
|
9 |
|
10 |
class AntiSpoofingSystem:
|
11 |
def __init__(self):
|
@@ -96,6 +95,49 @@ class AntiSpoofingSystem:
|
|
96 |
self.frame_count += 1
|
97 |
self.smartphone_detected = False
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
def detect_blink(self, left_ear, right_ear):
|
100 |
if self.smartphone_detected:
|
101 |
self.left_eye_state = False
|
@@ -163,35 +205,20 @@ class AntiSpoofingSystem:
|
|
163 |
return frame
|
164 |
|
165 |
def save_image(self, frame):
|
|
|
166 |
timestamp = int(time.time())
|
167 |
image_name = f"captured_{timestamp}.png"
|
168 |
cv2.imwrite(os.path.join(self.save_directory, image_name), frame)
|
169 |
self.captured_image = frame
|
170 |
print(f"Image captured and saved as {image_name}")
|
171 |
|
172 |
-
def
|
173 |
-
#
|
174 |
-
|
175 |
-
if
|
176 |
-
return
|
177 |
-
|
178 |
-
|
179 |
-
_, buffer = cv2.imencode('.png', frame)
|
180 |
-
return "Anti-spoofing check completed", buffer.tobytes()
|
181 |
-
|
182 |
-
# Initialize the anti-spoofing system
|
183 |
-
anti_spoofing_system = AntiSpoofingSystem()
|
184 |
-
|
185 |
-
# Define the Gradio interface
|
186 |
-
interface = gr.Interface(
|
187 |
-
fn=anti_spoofing_system.run_gradio,
|
188 |
-
inputs=[],
|
189 |
-
outputs=["text", "image"],
|
190 |
-
live=True,
|
191 |
-
title="Anti-Spoofing System",
|
192 |
-
description="This application performs an anti-spoofing check."
|
193 |
-
)
|
194 |
-
|
195 |
-
# Launch the interface
|
196 |
if __name__ == "__main__":
|
197 |
-
|
|
|
|
5 |
import time
|
6 |
import mediapipe as mp
|
7 |
from skimage import feature
|
|
|
8 |
|
9 |
class AntiSpoofingSystem:
|
10 |
def __init__(self):
|
|
|
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
|
|
|
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()
|