-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
168 lines (127 loc) · 5.31 KB
/
Copy pathapp.py
File metadata and controls
168 lines (127 loc) · 5.31 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
from flask import Flask, render_template, flash, redirect, url_for, request, session, jsonify
from flask_pymongo import PyMongo
from flask_bcrypt import Bcrypt
import numpy as np
import cv2
import base64
import checker
import tracker
import tensorflow as tf
import matplotlib.pyplot as plt
import face_recognition
import io, os
from PIL import Image
from PIL import Image
from io import BytesIO
import mediapipe as mp
import faceDetector
import ssl
app = Flask(__name__, static_url_path='/static')
# Create an SSL context
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
# Load the SSL certificate and key into the context
context.load_cert_chain('yourapp.crt', 'yourapp.key')
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/mydb' # MongoDB connection URL
mongo = PyMongo(app)
bcrypt = Bcrypt(app)
@app.route('/')
def first():
return render_template('index.html') # <script src="../static/scripts.js"></script>
@app.route('/signup_form', methods=['POST','GET'])
def signup_form():
return render_template('signup.html')
@app.route('/signup', methods=['GET','POST'])
def signup():
data=request.form
#print("the data is ",data)
if data['password1']==data['password2']:
existing_user = mongo.db.users.find_one({'username': data['userName']})
if existing_user:
return render_template('signup.html', status="This username already exists")
elif 'image' in request.files:
image=request.files['image']
hashed_password = bcrypt.generate_password_hash(data['password1']).decode('utf-8') #hash password into base64
user_data = {
'username': data['userName'],
'password': hashed_password,
'image': image.read()
}
mongo.db.users.insert_one(user_data)
return render_template('signup.html', status="Registration sucessfull")
else:
return render_template('signup.html', status="Upload your image ")
else :
return render_template('signup.html', status="Passwords dosen't match")
return render_template('signup.html')
@app.route('/login_form', methods=['GET','POST'])
def login_form():
return render_template('login.html')
@app.route('/login', methods=['POST','GET'])
def login():
data=request.form
username=data['userName']
user = mongo.db.users.find_one({'username': username})
if user and bcrypt.check_password_hash(user['password'], data['password']):
session['username']=username
return render_template('capture.html')
else:
return render_template('login.html', status="Wrong Password")
@app.route('/verify', methods=['POST', 'GET'])
def Verification():
if 'photo' in request.files:
image2=request.files['photo']
image_data = image2.read()
file_path = 'webImage.png'
with open(file_path, 'wb') as file:
file.write(image_data)
faces=faceDetector.detect_faces(file_path)
if faces==1:
print("only one face ")
if 'username' in session:
username = session['username']
user = mongo.db.users.find_one({'username': username})
binaryImage = user['image']
stored_image = np.frombuffer(binaryImage, np.uint8)
stored_image = cv2.imdecode(stored_image, cv2.IMREAD_COLOR) #known face encoding image1 in numpy arrey
stored_face_encoding_db= face_recognition.face_encodings(stored_image)[0]
unknown_face_image = face_recognition.load_image_file("webImage.png")
try:
unknown_face_encoding = face_recognition.face_encodings(unknown_face_image)[0]
except IndexError or ssl.SSLEOFError:
return jsonify({'message':'please align your face properly'}), 404
results = face_recognition.compare_faces([stored_face_encoding_db], unknown_face_encoding)
if results[0]:
print("Person 1's face matches the unknown face.")
return jsonify({'user':username, 'status':200}), 200
else:
return jsonify({'message':'Face does not match, Try again'}), 401
print("this is working")
else:
print("error in session")
return "Bad Request"
elif faces==0:
print(faces, "no face")
return jsonify({'message':'No face Found'}), 401
else:
print(faces, "Multiple faces")
return jsonify({'message':'Multiple faces Found'}), 401
else:
print("failed to uploading the image")
return jsonify({'message':'Some error in uploading the image, please make sure that your authorizing camera properly '}), 401
@app.route('/dashboard')
def dash():
if 'username' in session:
username=session['username']
return render_template('dashboard.html', user=username)
else:
return "Wrong Method"
@app.route('/logout', methods=['GET', 'POST'])
def logout():
if 'username' in session:
session.pop('username', None)
return render_template('index.html'), 200
else:
return "Login First"
if __name__ == '__main__':
app.run(host='0.0.0.0', ssl_context=context)