-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun.py
More file actions
289 lines (204 loc) · 10.2 KB
/
run.py
File metadata and controls
289 lines (204 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# app.py
from flask import Flask, render_template, flash, redirect, url_for, request, session
from flask_pymongo import PyMongo
from flask_bcrypt import Bcrypt
import cv2
import mediapipe as mp
import numpy as np
import base64
import face_recognition
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/mydb' # MongoDB connection URL
mongo = PyMongo(app)
bcrypt = Bcrypt(app)
# Define the routes
@app.route('/')
def index():
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
# if 'user' in session:
# return redirect(url_for('index'))
# if request.method == 'POST':
# username = request.form.get('username')
# password = request.form.get('password')
# image = request.files['image']
# hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
# user_data = {
# 'username': username,
# 'password': hashed_password,
# 'image': image.read()
# }
# mongo.db.users.insert_one(user_data)
# flash('Your account has been created!', 'success')
# return redirect(url_for('login'))
# return render_template('register.html')
if 'user' in session:
return redirect(url_for('index'))
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
image = request.files['image']
# Check if the username already exists in the database
existing_user = mongo.db.users.find_one({'username': username})
if existing_user:
flash('Username already exists. Please choose a different username.', 'danger')
return redirect(url_for('register'))
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
user_data = {
'username': username,
'password': hashed_password,
'image': image.read()
}
mongo.db.users.insert_one(user_data)
flash('Your account has been created!', 'success')
return redirect(url_for('login'))
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if 'user' in session:
return redirect(url_for('index'))
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
user = mongo.db.users.find_one({'username': username})
if user and bcrypt.check_password_hash(user['password'], password):
session['user'] = user['username']
flash('Login successful', 'success')
return redirect(url_for('authenticate'))
flash('Login failed. Please check your username and password.', 'danger')
return render_template('login.html')
@app.route('/authenticate', methods=['GET', 'POST'])
def authenticate():
# if 'user' not in session:
# return redirect(url_for('login'))
# live_image_data = None # Initialize to None
# if request.method == 'POST':
# # Capture a live image from the webcam using OpenCV
# cap = cv2.VideoCapture(0)
# ret, frame = cap.read()
# cap.release()
# if not ret:
# flash('Failed to capture live image', 'danger')
# return redirect(url_for('authenticate'))
# # Retrieve the stored image from MongoDB for the authenticated user
# username = session['user']
# user = mongo.db.users.find_one({'username': username})
# if not user:
# flash('User not found', 'danger')
# return redirect(url_for('authenticate'))
# stored_image_binary = user['image']
# # Convert the binary image data to a NumPy array
# stored_image = np.frombuffer(stored_image_binary, np.uint8)
# stored_image = cv2.imdecode(stored_image, cv2.IMREAD_COLOR)
# # Initialize MediaPipe Face Detection
# mp_face_detection = mp.solutions.face_detection
# face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)
# # Detect faces in the live image and perform template matching
# with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
# live_image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# results = face_detection.process(live_image_rgb)
# if results.detections:
# for detection in results.detections:
# bboxC = detection.location_data.relative_bounding_box
# ih, iw, _ = frame.shape
# x, y, w, h = int(bboxC.xmin * iw), int(bboxC.ymin * ih), \
# int(bboxC.width * iw), int(bboxC.height * ih)
# # Extract the face region from the live image
# live_face = frame[y:y + h, x:x + w]
# # Perform image comparison (template matching)
# similarity = template_matching(stored_image, live_face)
# if similarity > 0.7: # Adjust the threshold as needed
# flash('Authentication successful', 'success')
# else:
# flash('Authentication failed', 'danger')
# # Draw a rectangle around the detected face
# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # Green rectangle
# # Convert the annotated live image to base64 format for displaying in HTML
# _, buffer = cv2.imencode('.jpg', frame)
# live_image_data = base64.b64encode(buffer).decode()
# return render_template('authenticate.html', live_image=f'data:image/jpeg;base64,{live_image_data}')
if 'user' not in session:
return redirect(url_for('login'))
live_image_data = None # Initialize to None
if request.method == 'POST':
# Set the desired resolution and frame rate
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) # Width
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) # Height
cap.set(cv2.CAP_PROP_FPS, 30) # Frame rate (adjust as needed)
ret, frame = cap.read()
cap.release()
if not ret:
flash('Failed to capture live image', 'danger')
return redirect(url_for('authenticate'))
# Retrieve the stored image from MongoDB for the authenticated user
username = session['user']
user = mongo.db.users.find_one({'username': username})
if not user:
flash('User not found', 'danger')
return redirect(url_for('authenticate'))
stored_image_binary = user['image']
# Convert the binary image data to a NumPy array
stored_image = np.frombuffer(stored_image_binary, np.uint8)
stored_image = cv2.imdecode(stored_image, cv2.IMREAD_COLOR) #known face encoding image1 in numpy arrey
# Initialize MediaPipe Face Detection
mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)
# Detect faces in the live image and perform template matching
with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
live_image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_detection.process(live_image_rgb)
if results.detections:
for detection in results.detections:
bboxC = detection.location_data.relative_bounding_box
ih, iw, _ = frame.shape
x, y, w, h = int(bboxC.xmin * iw), int(bboxC.ymin * ih), \
int(bboxC.width * iw), int(bboxC.height * ih)
# Extract the face region from the live image
live_face = frame[y:y + h, x:x + w]
np_live_image = np.array(live_image_rgb)
print(np_live_image)
# live_face_encoding= face_recognition.face_encodings(np_live_image)[0]
live_face_encodings = face_recognition.face_encodings(np_live_image)
if live_face_encodings:
live_face_encoding = live_face_encodings[0]
# Rest of your code for authentication
else:
flash('No face found in the live image', 'danger')
# Handle the case where no face is detected in the live image
stored_face_encoding_db= face_recognition.face_encodings(stored_image)[0] #stored face encoding ho rhi
# Perform image comparison (template matching)
similarity = face_recognition.compare_faces([live_face_encoding], stored_face_encoding_db) #yaha face comparison karna hai
if similarity[0]: # no adjustment
flash('Authentication successful', 'success')
else:
flash('Authentication failed', 'danger')
# Draw a rectangle around the detected face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # Green rectangle
# Convert the annotated live image to base64 format for displaying in HTML
_, buffer = cv2.imencode('.jpg', frame)
live_image_data = base64.b64encode(buffer).decode()
return render_template('authenticate.html', live_image=f'data:image/jpeg;base64,{live_image_data}', authStutus="Authentication sucessfull")
# Template Matching Function
def template_matching(template, target):
# Convert the target and template images to grayscale
target_gray = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
# Perform template matching
result = cv2.matchTemplate(target_gray, template_gray, cv2.TM_CCOEFF_NORMED)
# Find the location of the best match
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# You can adjust the threshold as needed (higher values are more strict)
threshold = 0.7
if max_val >= threshold:
return max_val
else:
return 0.0
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(debug=True)