-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
162 lines (129 loc) · 5.27 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
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
from flask import *
import requests
from flask import redirect, url_for
import cv2
import numpy as np
import face_recognition
import os
# from main.py import register_feed()
app = Flask(__name__)
#################################START######################################################
#Login page code
def gen_frames():
path = 'UserImage'
images = []
classNames = []
myList = os.listdir(path)
# print(myList)
for cls in myList:
curImg = cv2.imread(f'{path}/{cls}')
images.append(curImg)
classNames.append(os.path.splitext(cls)[0])
# print(classNames)
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
encodeListKnown = findEncodings(images)
# print('Encoding Complete')
cap = cv2.VideoCapture(0)
# def gen_frames():
while True:
success, img = cap.read()
if not success:
break
else:
imageSmall = cv2.resize(img,(0,0),None,0.25,0.25)
imageSmall = cv2.cvtColor(imageSmall,cv2.COLOR_BGR2RGB)
facesCurrFrames = face_recognition.face_locations(imageSmall)
encodeCurrFrame = face_recognition.face_encodings(imageSmall,facesCurrFrames)
for encodeFace, faceLocation in zip(encodeCurrFrame,facesCurrFrames):
matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
facedistance = face_recognition.face_distance(encodeListKnown,encodeFace)
#print(facedistance)
matchIndex = np.argmin(facedistance)
if matches[matchIndex]:
y1,x2,y2,x1 = faceLocation
y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.rectangle(img,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
cv2.putText(img,"profile Matched",(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
case:ProfileMatched
else:
y1,x2,y2,x1 = faceLocation
y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.rectangle(img,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
cv2.putText(img,"profile Not Matched",(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
case:ProfileNotMatched
ret, buffer = cv2.imencode('.jpg', img)
img = buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + img +
b'\r\n')
#####################################END######################################
#################################START######################################################
#Register page code
def register_feed():
import os
import cv2
path = '/UserImage'
cam = cv2.VideoCapture(0)
# name=input("Name: ")
# cv2.namedWindow("test")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
else:
# cv2.imshow("test", frame)
img_saved = cv2.imwrite(f"UserImage\image{img_counter}.jpeg", frame)
if img_saved:
imageSmall = cv2.resize(frame,(0,0),None,0.25,0.25)
imageSmall = cv2.cvtColor(imageSmall,cv2.COLOR_BGR2RGB)
facesCurrFrames = face_recognition.face_locations(imageSmall)
encodeCurrFrame = face_recognition.face_encodings(imageSmall,facesCurrFrames)
for encodeFace, faceLocation in zip(encodeCurrFrame,facesCurrFrames):
y1,x2,y2,x1 = faceLocation
y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
cv2.putText(frame,"Your image saved",(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
img_counter += 1
cam.release()
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame +
b'\r\n')
# cam.release()
# cv2.destroyAllWindows()
#####################################END######################################
@app.route('/')
def index():
return render_template('index.html')
@app.route('/index.html')
def home():
return render_template('index.html')
@app.route('/login.html')
def login():
return render_template('login.html')
@app.route('/login_cam')
def login_cam():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/register_cam')
def register_cam():
return Response(register_feed(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/register.html')
def register():
return render_template('register.html')
@app.route('/result.html',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
return render_template("result.html",result = result)
if __name__ == '__main__':
app.run(debug=True)