-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (63 loc) · 2.77 KB
/
app.py
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
from flask import Flask, request, jsonify,render_template
import face_recognition
import os
from datetime import datetime
app = Flask(__name__)
# Placeholder for attendance records
attendance_records = {}
def load_known_faces(directory):
known_faces = []
known_names = []
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".png"):
path = os.path.join(directory, filename)
image = face_recognition.load_image_file(path)
encoding = face_recognition.face_encodings(image)[0]
known_faces.append(encoding)
known_names.append(os.path.splitext(filename)[0])
return known_faces, known_names
def recognize_face(unknown_image, known_faces, known_namaes):
unknown_encoding = face_recognition.face_encodings(unknown_image)
if len(unknown_encoding) > 0:
face_distances = face_recognition.face_distance(known_faces, unknown_encoding[0])
min_distance_index = face_distances.argmin()
if face_distances[min_distance_index] < 0.6: # You can adjust the threshold as needed
return known_names[min_distance_index]
else:
return "Unknown"
else:
return "No face found in the provided image"
# Provide the path to the directory containing known faces images
known_faces_dir = "known"
# Load known faces and names
known_faces, known_names = load_known_faces(known_faces_dir)
@app.route('/recognize', methods=['POST'])
def recognize():
print(request.files)
# Check for the empty string key in request.files
if '' in request.files:
file = request.files['']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file:
try:
unknown_image = face_recognition.load_image_file(file)
result = recognize_face(unknown_image, known_faces, known_names)
# Update attendance records
update_attendance(result)
return jsonify({"result": result})
except Exception as e:
return jsonify({"error": str(e)}), 500
else:
return jsonify({"error": "No file provided"}), 400
def update_attendance(name):
if name != "Unknown":
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if name not in attendance_records:
attendance_records[name] = []
attendance_records[name].append(timestamp)
@app.route('/view_records', methods=['GET'])
def view_records():
return render_template('attendance_records.html', records=attendance_records)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)