Munaamullah commited on
Commit
2d013f4
·
1 Parent(s): 8bb0582

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -18
app.py CHANGED
@@ -1,27 +1,168 @@
1
  import gradio as gr
2
- from anti_spoofing import AntiSpoofingSystem
 
 
 
 
 
 
 
3
 
4
- # Instantiate your AntiSpoofingSystem outside of the function to avoid re-loading models
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  anti_spoofing_system = AntiSpoofingSystem()
6
 
 
 
 
 
7
  def process_frame(image):
8
- # Since we're not processing a video stream, we process the single image
9
- # Convert the image to grayscale, detect features, etc.
10
- # Then return results (for this example, just returning the input image and dummy text)
11
- processed_image, blink_count, hand_gesture_detected, smartphone_detected = anti_spoofing_system.process_single_frame(image)
12
- return processed_image, f"Blink Count: {blink_count}", f"Hand Gesture Detected: {hand_gesture_detected}", f"Smartphone Detected: {smartphone_detected}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  iface = gr.Interface(
15
- fn=process_frame,
16
- inputs=gr.inputs.Image(type="pil", label="Upload Image or Use Webcam"),
17
- outputs=[
18
- gr.outputs.Image(type="pil", label="Processed Image"),
19
- gr.outputs.Textbox(label="Blink Count"),
20
- gr.outputs.Textbox(label="Hand Gesture Detected"),
21
- gr.outputs.Textbox(label="Smartphone Detected")
22
- ],
23
- title="Anti-Spoofing System",
24
- description="Upload an image or capture from webcam to check for spoofing indicators."
25
  )
26
 
27
- iface.launch()
 
 
1
  import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import os
5
+ import time
6
+ import dlib
7
+ import mediapipe as mp
8
+ from skimage import feature
9
+ # from your_cnn_model import YourCNNModel # Import your CNN model
10
 
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
+ self.mp_hands = mp.solutions.hands
16
+ self.hands = self.mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.7)
17
+ self.net_smartphone = cv2.dnn.readNet('yolov4.weights', 'yolov4.cfg')
18
+ with open('coco.names', 'r') as f:
19
+ self.classes_smartphone = f.read().strip().split('\n')
20
+ self.EAR_THRESHOLD = 0.25
21
+ self.BLINK_CONSEC_FRAMES = 4
22
+ self.left_eye_state = False
23
+ self.right_eye_state = False
24
+ self.left_blink_counter = 0
25
+ self.right_blink_counter = 0
26
+ self.smartphone_detected = False
27
+ self.smartphone_detection_frame_interval = 30
28
+ self.frame_count = 0
29
+
30
+ def calculate_ear(self, eye):
31
+ A = np.linalg.norm(eye[1] - eye[5])
32
+ B = np.linalg.norm(eye[2] - eye[4])
33
+ C = np.linalg.norm(eye[0] - eye[3])
34
+ return (A + B) / (2.0 * C)
35
+
36
+ def analyze_texture(self, face_region):
37
+ gray_face = cv2.cvtColor(face_region, cv2.COLOR_BGR2GRAY)
38
+ lbp = feature.local_binary_pattern(gray_face, P=8, R=1, method="uniform")
39
+ lbp_hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, 58), range=(0, 58))
40
+ lbp_hist = lbp_hist.astype("float")
41
+ lbp_hist /= (lbp_hist.sum() + 1e-5)
42
+ return np.sum(lbp_hist[:10]) > 0.3
43
+
44
+ def detect_hand_gesture(self, frame):
45
+ results = self.hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
46
+ return results.multi_hand_landmarks is not None
47
+
48
+ def detect_smartphone(self, frame):
49
+ if self.frame_count % self.smartphone_detection_frame_interval == 0:
50
+ blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
51
+ self.net_smartphone.setInput(blob)
52
+ output_layers_names = self.net_smartphone.getUnconnectedOutLayersNames()
53
+ detections = self.net_smartphone.forward(output_layers_names)
54
+ for detection in detections:
55
+ for obj in detection:
56
+ scores = obj[5:]
57
+ class_id = np.argmax(scores)
58
+ confidence = scores[class_id]
59
+ if confidence > 0.5 and self.classes_smartphone[class_id] == 'cell phone':
60
+ self.smartphone_detected = True
61
+ self.left_blink_counter = 0
62
+ self.right_blink_counter = 0
63
+ return
64
+ self.frame_count += 1
65
+ self.smartphone_detected = False
66
+
67
+ def detect_blink(self, left_ear, right_ear):
68
+ if self.smartphone_detected:
69
+ self.left_eye_state = False
70
+ self.right_eye_state = False
71
+ self.left_blink_counter = 0
72
+ self.right_blink_counter = 0
73
+ return False
74
+
75
+ if left_ear < self.EAR_THRESHOLD:
76
+ if not self.left_eye_state:
77
+ self.left_eye_state = True
78
+ else:
79
+ if self.left_eye_state:
80
+ self.left_eye_state = False
81
+ self.left_blink_counter += 1
82
+
83
+ if right_ear < self.EAR_THRESHOLD:
84
+ if not self.right_eye_state:
85
+ self.right_eye_state = True
86
+ else:
87
+ if self.right_eye_state:
88
+ self.right_eye_state = False
89
+ self.right_blink_counter += 1
90
+
91
+ return self.left_blink_counter > 0 and self.right_blink_counter > 0
92
+
93
+ def run(self, input_image):
94
+ frame = input_image
95
+ blink_count = 0
96
+ hand_gesture_detected = False
97
+ real_person_detected = False
98
+ cropped_face = None
99
+
100
+ self.detect_smartphone(frame)
101
+
102
+ if not self.smartphone_detected:
103
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
104
+ faces = self.detector(gray)
105
+
106
+ for face in faces:
107
+ landmarks = self.predictor(gray, face)
108
+ leftEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(36, 42)])
109
+ rightEye = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(42, 48)])
110
+
111
+ ear_left = self.calculate_ear(leftEye)
112
+ ear_right = self.calculate_ear(rightEye)
113
+
114
+ if self.detect_blink(ear_left, ear_right):
115
+ blink_count += 1
116
+
117
+ hand_gesture_detected = self.detect_hand_gesture(frame)
118
+
119
+ (x, y, w, h) = (face.left(), face.top(), face.width(), face.height())
120
+ cropped_face = frame[max(y - h // 2, 0):min(y + 3 * h // 2, frame.shape[0]),
121
+ max(x - w // 2, 0):min(x + 3 * w // 2, frame.shape[1])]
122
+
123
+ if blink_count >= 5 and hand_gesture_detected and self.analyze_texture(cropped_face):
124
+ real_person_detected = True
125
+ break
126
+
127
+ return real_person_detected, cropped_face
128
+
129
+ # Initialize the anti-spoofing system
130
  anti_spoofing_system = AntiSpoofingSystem()
131
 
132
+ # Load your CNN model (this is a placeholder for your actual model loading code)
133
+ # cnn_model = YourCNNModel()
134
+ # cnn_model.load_weights('path_to_your_h5_file.h5')
135
+
136
  def process_frame(image):
137
+ real_person_detected, cropped_face = anti_spoofing_system.run(image)
138
+
139
+ if not real_person_detected:
140
+ return image, "No real person detected or spoofing attempt."
141
+
142
+ # Placeholder for actual CNN model prediction
143
+ person_id, confidence = "PersonID", 0.99 # Replace with your CNN model logic
144
+
145
+ result_text = f"Person identified: {person_id} with confidence: {confidence}" if person_id else "Person not recognized. Registration required."
146
+ cv2.putText(image, result_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
147
+ return image
148
+
149
+ def video_stream():
150
+ cap = cv2.VideoCapture(0)
151
+ while True:
152
+ ret, frame = cap.read()
153
+ if not ret:
154
+ break
155
+ processed_frame = process_frame(frame)
156
+ yield processed_frame
157
 
158
  iface = gr.Interface(
159
+ fn=video_stream,
160
+ inputs=None,
161
+ outputs=gr.outputs.Video(label="Output Video"),
162
+ live=True,
163
+ title="Live Face Recognition and Verification System",
164
+ description="Live detection and verification of persons from a camera feed."
 
 
 
 
165
  )
166
 
167
+ if __name__ == "__main__":
168
+ iface.launch()