-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
118 lines (75 loc) · 3.2 KB
/
server.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
#!/usr/bin/env python3
"""Hololens backend server implemented with Flask"""
import pickle
import hashlib
import datetime
import json
import configuration
import logger
import classifierFace
from flask import Flask
from flask import request
import cv2
# Instantiate and configure Flask server
server = Flask("Hololens backend server")
server.config['DEBUG'] = configuration.FLASK_DEBUG
# Instantiate face classifier
logger.log_event("Server: begin loading face classifier")
classifier_face = classifierFace.ClassifierFace()
logger.log_event("Server: finished loading face classifier")
# API endpoints
# Template endpoint, e.g. admin, reload models
@server.route("/api/admin/reload", methods=["POST"])
def route_admin_reload():
"""Template endpoint"""
if request.method == "POST":
json_dict = request.get_json()
command = int(json_dict['command'])
# Code
# TODO Implement functionality
# Response
response = {"message": "ok"}
return json.dumps(response)
else:
response = {"message": "bad request"}
return json.dumps(response)
# Detect faces
@server.route("/api/detect/faces", methods=["POST"])
def route_detect_faces():
"""Detect faces"""
logger.log_event("Server: received face detection request")
if request.method == "POST":
# Receive data and deserialize to image matrix (marked below with horizontal rules)
# NOTE: THIS DESERIALIZATION NEEDS TO MATCH THE CLIENT (SENDER'S) SERIALIZATION
# ---------------------------------------------------------------------
image_matrix_pickled = request.get_data()
# Unpickle from byte string
image_matrix = pickle.loads(image_matrix_pickled)
# ---------------------------------------------------------------------
# Write image to disk (optionally)
if configuration.DEBUG_SERVER_WRITE_IMAGE:
timestamp = '{0:%Y-%m-%d-%H-%M-%S}'.format(datetime.datetime.now())
image_filename = timestamp + ".jpg"
cv2.imwrite(configuration.DEBUG_SERVER_WRITE_IMAGE_DIR + "/" + image_filename, image_matrix)
logger.log_event("Server: image written to disk at " + configuration.DEBUG_SERVER_WRITE_IMAGE_DIR + "/" + image_filename)
# Compute MD5 hash and log it
hash_md5 = hashlib.md5()
hash_md5.update(image_matrix)
hash_image_matrix = hash_md5.hexdigest()
logger.log_event("Server: received image matrix MD5 hash is: " + hash_image_matrix)
# Detect faces and classify faces
bounding_boxes, predictions = classifier_face.classify(image_matrix)
logger.log_event("Server: finished face detection on image")
# Build response and send it
response = {"bounding_boxes": bounding_boxes, "predictions": predictions}
return json.dumps(response)
else:
logger.log_error("Server: received a bad requesst")
response = {"message": "bad request"}
return json.dumps(response)
def main():
"""Run Flask server application"""
logger.log_event("Server: starting Flask server")
server.run(host=configuration.FLASK_HOST, port=configuration.FLASK_PORT)
if __name__ == "__main__":
main()