-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
135 lines (112 loc) · 4.24 KB
/
Copy pathapp.py
File metadata and controls
135 lines (112 loc) · 4.24 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
from flask import Flask, render_template, request,redirect, url_for, flash, get_flashed_messages, session
import cv2
import numpy as np
import sqlite3
from keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
import tensorflow as tf
import time
app = Flask(__name__)
app.secret_key = 'ronansibappe'
# model = tf.keras.models.load_model('./model/CNN')
model = tf.keras.models.load_model('./model/CNN')
# kiến trúc của mô hình
# model.summary()
# Khởi tạo bộ phát hiện khuôn mặt
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Tạo thông tin người dùng
users = [
{
"id": 0,
"name": "khoa",
"password": "12345"
},
{
"id": 1,
"name": "Quang",
"password": "12345"
},
]
def get_user(username, password):
for user in users:
if user['name'] == username and user['password'] == password:
return user
return None
# // GET home
@app.route('/')
def index():
return render_template('index.html')
# //GET login
@app.route('/login', methods=['GET'])
def login():
messages = get_flashed_messages()
return render_template('login.html', messages=messages)
# //POST login
@app.route('/login', methods=['POST'])
def do_login():
username = request.form['username']
password = request.form['password']
user = get_user(username,password)
if user is not None:
return redirect(url_for('face_detection', user_id=user['id'], username=user['name']))
else:
flash('Đăng nhập thất bại!')
return redirect(url_for('login'))
@app.route('/register')
def register():
return render_template('registerForm.html')
# Route để nhận diện khuôn
@app.route('/face_detection')
def face_detection():
user_id = request.args.get('user_id')
username = request.args.get('username')
if user_id is None or username is None:
return redirect(url_for('login'))
else:
# Khởi tạo bộ phát hiện khuôn mặt
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Khởi tạo camera
cam = cv2.VideoCapture(0)
while (True):
# khởi tạo bộ đếm số lần giống với dự đoán
count = 0
# Đọc ảnh từ camera
ret, img = cam.read()
# Lật ảnh cho đỡ bị ngược
img = cv2.flip(img, 1)
# Chuyển ảnh về xám
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Phát hiện các khuôn mặt trong ảnh camera
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
# Lặp qua các khuôn mặt nhận được để hiện thông tin
for (x, y, w, h) in faces:
# Vẽ hình chữ nhật quanh mặt
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Chuyển đổi ảnh về định dạng phù hợp và thực hiện dự đoán
roi_color = img[y:y+h, x:x+w]
roi_color = cv2.resize(roi_color, (150, 150))
roi_color = cv2.cvtColor(roi_color, cv2.COLOR_BGR2RGB) # Chuyển đổi sang định dạng RGB
preds = model.predict(np.array([roi_color]))
person_id = np.argmax(preds)
print(preds)
print(person_id)
print(user_id)
if (str(user_id) == str(person_id)):
print("Đây là: {}".format(username))
cv2.putText(img, str(person_id)+"_Press_q_to_break", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
# xử lí xác thực hay không:
if (str(user_id) == str(person_id)):
if preds[0][person_id] == 1:
cam.release()
cv2.destroyAllWindows()
return 'Đăng nhập thành công!'
cv2.imshow('Face', img)
# Nếu nhấn q thì thoát
if cv2.waitKey(1) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
time.sleep(2000)
return 'không thành công!'
if __name__ == '__main__':
app.run(debug=True)