-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_script.py
More file actions
130 lines (109 loc) · 4.76 KB
/
run_script.py
File metadata and controls
130 lines (109 loc) · 4.76 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
from flask import Flask, render_template
import cv2
from align_custom import AlignCustom
from face_feature import FaceFeature
from mtcnn_detect import MTCNNDetect
from tf_graph import FaceRecGraph
import argparse
import sys
import json
import time
import numpy as np
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/input')
def create_manual_data():
FRGraph = FaceRecGraph();
MTCNNGraph = FaceRecGraph();
aligner = AlignCustom();
extract_feature = FaceFeature(FRGraph)
face_detect = MTCNNDetect(MTCNNGraph, scale_factor=2); #scale_factor, rescales image for faster detection
vs = cv2.VideoCapture(0); #get input from webcam
# print("Please input new user ID:")
new_name = request.form['input_name']; #ez python input()
f = open('./facerec_128D.txt','r');
data_set = json.loads(f.read());
person_imgs = {"Left" : [], "Right": [], "Center": []};
person_features = {"Left" : [], "Right": [], "Center": []};
print("Please start turning slowly. Press 'q' to save and add this new user to the dataset");
while True:
_, frame = vs.read();
rects, landmarks = face_detect.detect_face(frame, 80); # min face size is set to 80x80
for (i, rect) in enumerate(rects):
aligned_frame, pos = aligner.align(160,frame,landmarks[:,i]);
if len(aligned_frame) == 160 and len(aligned_frame[0]) == 160:
person_imgs[pos].append(aligned_frame)
cv2.imshow("Captured face", aligned_frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
for pos in person_imgs: #there r some exceptions here, but I'll just leave it as this to keep it simple
person_features[pos] = [np.mean(extract_feature.get_features(person_imgs[pos]),axis=0).tolist()]
data_set[new_name] = person_features;
f = open('./facerec_128D.txt', 'w');
f.write(json.dumps(data_set))
return render_template('home.html')
@app.route('/verify')
def camera_recog():
FRGraph = FaceRecGraph();
MTCNNGraph = FaceRecGraph();
aligner = AlignCustom();
extract_feature = FaceFeature(FRGraph)
face_detect = MTCNNDetect(MTCNNGraph, scale_factor=2); #scale_factor, rescales image for faster detection
print("[INFO] camera sensor warming up...")
vs = cv2.VideoCapture(0); #get input from webcam
detect_time = time.time()
while True:
_,frame = vs.read();
#u can certainly add a roi here but for the sake of a demo i'll just leave it as simple as this
rects, landmarks = face_detect.detect_face(frame,80);#min face size is set to 80x80
aligns = []
positions = []
for (i, rect) in enumerate(rects):
aligned_face, face_pos = aligner.align(160,frame,landmarks[:,i])
if len(aligned_face) == 160 and len(aligned_face[0]) == 160:
aligns.append(aligned_face)
positions.append(face_pos)
else:
print("Align face failed") #log
if(len(aligns) > 0):
features_arr = extract_feature.get_features(aligns)
recog_data = findPeople(features_arr,positions)
for (i,rect) in enumerate(rects):
cv2.rectangle(frame,(rect[0],rect[1]),(rect[2],rect[3]),(255,0,0)) #draw bounding box for the face
cv2.putText(frame,recog_data[i][0]+" - "+str(recog_data[i][1])+"%",(rect[0],rect[1]),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),1,cv2.LINE_AA)
cv2.imshow("Frame",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
return render_template('home.html')
def findPeople(features_arr, positions, thres = 0.6, percent_thres = 70):
'''
:param features_arr: a list of 128d Features of all faces on screen
:param positions: a list of face position types of all faces on screen
:param thres: distance threshold
:return: person name and percentage
'''
TIMEOUT = 10 #10 seconds
f = open('./facerec_128D.txt','r')
data_set = json.loads(f.read());
returnRes = [];
for (i,features_128D) in enumerate(features_arr):
result = "Unknown";
smallest = sys.maxsize
for person in data_set.keys():
person_data = data_set[person][positions[i]];
for data in person_data:
distance = np.sqrt(np.sum(np.square(data-features_128D)))
if(distance < smallest):
smallest = distance;
result = person;
percentage = min(100, 100 * thres / smallest)
if percentage <= percent_thres :
result = "Unknown"
returnRes.append((result,percentage))
return returnRes
if __name__ == '__main__':
app.run()