-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
93 lines (79 loc) · 2.97 KB
/
app.py
File metadata and controls
93 lines (79 loc) · 2.97 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
from flask import Flask,request, url_for, redirect, render_template
from datetime import datetime
import numpy as np
from werkzeug.utils import secure_filename
import face_recognition as fr
import cv2
import numpy as np
import os,shutil
import base64
import pandas as pd
app = Flask(__name__)
#Here i have read the data from gogle drive
def get_encoded_faces():
encoded={}
for dirpath,dname,fname in os.walk("static/images/"):
for f in fname:
if f.endswith(".jpeg") or f.endswith(".jpg"):
face=fr.load_image_file("static/images/"+ f)
encoding=fr.face_encodings(face)[0]
encoded[f.split(".")[0]]=encoding
return (encoded)
# Here i am recognising the face of test data by providing certain data
def classify_face(im):
faces = get_encoded_faces()
faces_encoded = list(faces.values())
known_faces_names = list(faces.keys())
img = cv2.imread(im)
face_locations = fr.face_locations(img)
unknown_face_encodings = fr.face_encodings(img, face_locations)
face_names = []
for face_encoding in unknown_face_encodings:
name = "Unknown"
matches = fr.compare_faces(faces_encoded, face_encoding)
face_distances = fr.face_distance(faces_encoded, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_faces_names[best_match_index]
face_names.append(name)
for (top, right, bottom, left), name in zip(face_locations, face_names):
cv2.rectangle(img, (left - 20, top - 20), (right + 20, bottom + 20), (255, 0, 0), 2)
cv2.rectangle(img, (left - 20, bottom - 15), (right + 20, bottom + 20), (255, 0, 0), cv2.FILLED)
cv2.putText(img, name, (left - 20, bottom + 15), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255), 1)
while (True):
#cv2_imshow(img)
return (face_names)
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
if request.method=="GET":
return "no picture"
elif request.method == "POST":
features = [(x) for x in request.form.values()]
print(features)
for i in features:
print(i)
print(i[23:])
imgstring=i[23:]
imgdata = base64.b64decode(imgstring)
from datetime import datetime
import os.path
directory = './studentimgs/'
now = datetime.now()
f_name = now.strftime("%d%m%Y %H%M%S")
filename = f_name + '.png'
filepath = os.path.join(directory, filename)
# print(f_name)
with open(filepath, 'wb') as f:
f.write(imgdata)
f.close()
print("Done")
print(classify_face(filepath))
output=classify_face(filepath)
return render_template('index.html',
pred='Hii{} Attendence recorded'.format(output))
if __name__ == '__main__':
app.run(debug=True)