-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
157 lines (135 loc) · 5.01 KB
/
main.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
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
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
import cv2
app = Flask(__name__, template_folder='video capture')
@app.route('/')
def login():
return render_template('main.html')
@app.route('/su')
def su():
return render_template('signup.html')
@app.route('/sign', methods=['POST', 'GET'])
def details():
if request.method == 'POST':
name = request.form['name'].lower()
email = request.form['email'].lower()
phone = request.form['phone']
address = request.form['address'].lower()
password = request.form['password']
bal = 500
import cv2
import numpy as np
conn = sqlite3.connect('./FaceBase.db')
cur = conn.cursor()
cur.execute("select id from users")
row = cur.fetchall()
if not row:
id = 1
else:
id = list(row[-1])[-1] + 1
print(id)
conn.execute('Insert into users(id, name, email, phone, balance, address, password) values(?,?,?,?,?,?,?)',
(id, name, email, phone, bal, address, password))
conn.commit()
conn.close()
faceDetect = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
# name=input('Enter the name:')
# insertOrUpdate(id,name)
sampleNum = 0
while (True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
sampleNum += 1
cv2.imwrite('./dataset/User.' + str(id) + '.' + str(sampleNum) + '.jpg', gray[y:y + h, x:x + w])
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.waitKey(100)
cv2.imshow('Face', img)
cv2.waitKey(1)
if (sampleNum > 50):
break
cam.release()
cv2.destroyAllWindows()
import os
import cv2
import numpy as np
from PIL import Image
recogniser = cv2.face.LBPHFaceRecognizer_create()
path = './dataset'
def getImagesWithID(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faces = []
IDs = []
for imagePath in imagePaths:
faceImg = Image.open(imagePath).convert('L')
faceNp = np.array(faceImg, 'uint8')
ID = int(os.path.split(imagePath)[-1].split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow("training", faceNp)
cv2.waitKey(10)
return IDs, faces
Ids, faces = getImagesWithID(path)
recogniser.train(faces, np.array(Ids))
recogniser.save('./recogniser/trainingData.yml')
cv2.destroyAllWindows()
return redirect(url_for('login'))
@app.route('/image')
def login_face():
faceDetect = cv2.CascadeClassifier(
'./haarcascade_frontalface_default.xml')
rec = cv2.face.LBPHFaceRecognizer_create()
rec.read(
'./recogniser/trainingData.yml')
import time
time.sleep(5)
img = cv2.imread('./login/input.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
print(faces)
x, y, w, h = faces[0]
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
id, conf = rec.predict(gray[y:y + h, x:x + w])
cv2.putText(img, str(id), (x, x), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
conn = sqlite3.connect('./FaceBase.db')
cur = conn.cursor()
import os
os.remove('./login/input.png')
cur.execute('select * from users where id=?', (str(id),))
row = cur.fetchall()
curh = conn.cursor()
curh.execute('select * from history where id=?', (str(id),))
record = curh.fetchall()
if row:
name = row[0][1]
print(row[0])
return render_template('profile.html', result=[row[0], record])
else:
account = 0
# return redirect(url_for('open'))
return render_template('login.html', result=account)
@app.route('/login', methods=['POST', 'GET'])
def login_get():
if request.method == 'POST':
name = request.form['email'].lower()
passw = request.form['password']
conn = sqlite3.connect('./FaceBase.db')
cur = conn.cursor()
cur.execute('select * from users where email=? and password=?', (name, passw))
row = cur.fetchall()
if not row:
# print('nahi hai')
account = 0
# return redirect(url_for('open'))
return render_template('main.html', result=account)
else:
user = row[0][1]
id = row[0][0]
curh = conn.cursor()
curh.execute('select * from history where id=?', (str(id),))
record = curh.fetchall()
return render_template('profile.html', result=[row[0], record])
if __name__ == '__main__':
app.run()