-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
232 lines (165 loc) · 7.57 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import io
import time
from datetime import datetime
import numpy as np
from flask import Flask, render_template, request, jsonify, Response
import warnings
import cv2
import json
import requests
import base64
import os
from PIL import Image
from face_detection.FaceDetection import FaceDetector
# setting up face detector
detector = FaceDetector('ssd')
warnings.filterwarnings('ignore')
DATA_FOLDER = os.path.join("static", "data")
# global counter variable for image capture and class id
CAPTURE_IMAGE = False
CLASS_ID = None
def delete_current_captured_saved_image():
folder_path = 'static/sv_im/'
# using listdir() method to list the files of the folder
test = os.listdir(folder_path)
# taking a loop to remove all the images
# using ".png" extension to remove only png images
for images in test:
if images.endswith(".png"):
os.remove(os.path.join(folder_path, images))
def web_streaming():
global CAPTURE_IMAGE, CLASS_ID
vid = cv2.VideoCapture(0)
vid.set(cv2.CAP_PROP_FPS, 30)
vid.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
if os.path.exists("templates/profiles.json"):
os.remove("templates/profiles.json")
delete_current_captured_saved_image()
while True:
# Capture the video
ret, frame = vid.read()
if frame is None:
print("Error, Check if camera is connected!")
break
frame = cv2.flip(frame, 1)
output = detector.check_and_detect(frame)
# flip the image
if CAPTURE_IMAGE:
if (output['detectable']) and (output['faceonly'].all() is not None):
request_data = {'id': CLASS_ID,
'face': output['faceonly'], 'box': output['box']}
cv2.imwrite('static/sv_im/box.png', output['box'])
resized_cropped_face = cv2.resize(request_data['face'], (400, 400))
cv2.imwrite('static/sv_im/cropped-frame.png', resized_cropped_face)
cv2.imwrite('static/sv_im/cropped.png', request_data['face'])
else:
print('Face Not Found or Lightening Issues...')
CAPTURE_IMAGE = False
frame = output['frame']
# Display the resulting frame on page
ret, jpeg = cv2.imencode('.jpg', frame)
img = jpeg.tobytes()
if os.path.exists('static/sv_im/cropped.png') and os.path.exists('static/sv_im/db_image.png'):
img_st_mtime = os.stat('static/sv_im/cropped.png').st_mtime
img_str_time = datetime.fromtimestamp(img_st_mtime).strftime("%H:%M:%S")
current_time = datetime.now().strftime("%H:%M:%S")
FMT = '%H:%M:%S'
tdelta = datetime.strptime(current_time, FMT) - datetime.strptime(img_str_time, FMT)
if int(str(tdelta).split(':')[1]) > 0 or int(str(tdelta).split(':')[-1]) > 5:
print('resetting...')
delete_current_captured_saved_image()
if os.path.exists("templates/profiles.json"):
os.remove("templates/profiles.json")
print('reset done.')
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + img + b'\r\n\r\n')
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = '0'
response.headers["Pragma"] = "no-cache"
return response
@app.route('/', methods=['POST', 'GET'])
def index():
dummy_image = os.path.join(DATA_FOLDER, "dummy.png")
return render_template("index.html", dummy_image=dummy_image)
@app.route('/live_streaming', methods=['POST', 'GET'])
def live_streaming():
return Response(web_streaming(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/requests', methods=['POST', 'GET'])
def tasks():
if request.method == 'POST':
if request.form.get('click') == 'Capture':
print('Running tasks')
global CAPTURE_IMAGE, CLASS_ID
CAPTURE_IMAGE = True
CLASS_ID = request.form['uid'].strip().rstrip()
if len(CLASS_ID) == 0:
CLASS_ID = None
if os.path.exists('static/sv_im/cropped.png') and os.path.exists('static/sv_im/box.png'):
img_st_mtime = os.stat('static/sv_im/box.png').st_mtime
img_str_time = datetime.fromtimestamp(img_st_mtime).strftime("%H:%M:%S")
current_time = datetime.now().strftime("%H:%M:%S")
FMT = '%H:%M:%S'
tdelta = datetime.strptime(current_time, FMT) - datetime.strptime(img_str_time, FMT)
if int(str(tdelta).split(':')[1]) > 0 or int(str(tdelta).split(':')[-1]) > 10:
delete_current_captured_saved_image()
# send the data to the server
api = 'http://192.168.100.245:5000/fr'
image_file = 'static/sv_im/cropped.png'
time.sleep(0.5)
if os.path.exists(image_file):
with open(image_file, "rb") as f:
im_bytes = f.read()
im_b64 = base64.b64encode(im_bytes).decode("utf8")
headers = {'Content-type': 'application/json',
'Accept': 'text/plain'}
payload = json.dumps({"id": CLASS_ID, "face": im_b64})
response = requests.post(api, data=payload, headers=headers)
response = response.json()
if response['verified']:
db_img_bytes = base64.b64decode(response['db_image'].encode('utf-8'))
db_img = Image.open(io.BytesIO(db_img_bytes))
db_bgr_img_array = np.asarray(db_img)
db_rgb_img_array = cv2.cvtColor(db_bgr_img_array, cv2.COLOR_BGR2RGB)
db_rgb_img_array = cv2.resize(db_rgb_img_array, (400, 400))
cv2.imwrite('static/sv_im/db_image.png', db_rgb_img_array)
images_dict = {'db_img': 'static/sv_im/db_image.png',
'cr_img': 'static/sv_im/cropped-frame.png',
'id': response['id'].split("_")[0],
'name': response['id'].split("_")[1]}
with open('templates/profiles.json', 'w') as outfile:
# Writing from json file
outfile.write(json.dumps(images_dict))
print('Welcome Mr. ' + response['id'].split('_')[1])
try:
requests.get('http://192.168.100.197/gpio/1')
except:
print('Lock Not Connected')
# delete_current_captured_saved_image()
CAPTURE_IMAGE = False
CLASS_ID = None
else:
print('Current Image Not Found')
elif request.method == 'GET':
return render_template('index.html')
return render_template('index.html')
@app.route('/get_similarity_json', methods=['POST', 'GET'])
def get_similarity_json():
try:
# Opening JSON file
with open('templates/profiles.json', 'r') as openfile:
# Reading from json file
json_object = json.load(openfile)
return jsonify(json_object)
except:
abort(404)
@app.route('/form', methods=['POST', 'GET'])
def form():
return jsonify({'status': 'Done'})
if __name__ == '__main__':
app.run(debug=True)