-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathServer.py
124 lines (92 loc) · 4.27 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
119
120
121
122
123
124
############################################################################################
#
# Project: Peter Moss Acute Myeloid & Lymphoblastic Leukemia AI Research Project
# Repository: ALL Detection System 2019
# Project: Facial Authentication Server
#
# Author: Adam Milton-Barker (AdamMiltonBarker.com)
# Contributors:
# Title: Server Class
# Description: Server for the ALL Detection System 2019 Classifier.
# License: MIT License
# Last Modified: 2020-07-21
#
############################################################################################
import cv2, json, jsonpickle, os, sys, time
import numpy as np
from mvnc import mvncapi as mvnc
from datetime import datetime
from skimage.transform import resize
from Classes.Helpers import Helpers
from Classes.Movidius import Movidius
from flask import Flask, request, Response
class Server():
""" ALL Detection System 2019 Server Class
Server for the ALL Detection System 2019 Classifier.
"""
def __init__(self):
""" Initializes the Server Class. """
self.Helpers = Helpers("Server")
self.confs = self.Helpers.confs
self.Movidius = Movidius()
self.Movidius.checkNCS()
self.Movidius.loadInception()
self.Helpers.logger.info(
"Server class initialization complete.")
app = Flask(__name__)
Server = Server()
@app.route('/Inference', methods=['POST'])
def Inference():
if len(request.files) != 0:
img = np.fromstring(request.files['file'].read(), np.uint8)
else:
img = np.fromstring(request.data, np.uint8)
img = cv2.imdecode(img, cv2.IMREAD_UNCHANGED).astype(np.float32)
dx, dy, dz = img.shape
delta = float(abs(dy-dx))
if dx > dy:
img = img[int(0.5*delta):dx-int(0.5*delta), 0:dy]
else:
img = img[0:dx, int(0.5*delta):dy-int(0.5*delta)]
img = cv2.resize(img, (Server.Movidius.reqsize,
Server.Movidius.reqsize))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in range(3):
img[:, :, i] = (img[:, :, i] - Server.Movidius.mean) * \
Server.Movidius.std
detectionStart, detectionStart = Server.Helpers.timerStart()
Server.Helpers.logger.info("API detection started.")
Server.Movidius.ncsGraph.LoadTensor(img.astype(np.float16), 'user object')
output, userobj = Server.Movidius.ncsGraph.GetResult()
detectionClockEnd, difference, detectionEnd = Server.Helpers.timerEnd(
detectionStart)
Server.Helpers.logger.info("API detection ended taking " + str(difference))
top_inds = output.argsort()[::-1][:5]
if output[top_inds[0]] >= Server.confs["Classifier"]["InceptionThreshold"] and Server.Movidius.classes[top_inds[0]] == "1":
classification = "AML Positive"
message = "ALL detected with a confidence of " + \
str(output[top_inds[0]]) + " in " + str(difference)
elif output[top_inds[0]] >= Server.confs["Classifier"]["InceptionThreshold"] and Server.Movidius.classes[top_inds[0]] == "0":
classification = "AML Negative"
message = "ALL not detected with a confidence of " + \
str(output[top_inds[0]]) + " in " + str(difference)
elif output[top_inds[0]] <= Server.confs["Classifier"]["InceptionThreshold"] and Server.Movidius.classes[top_inds[0]] == "1":
classification = "AML Positive"
message = "ALL detected with a LOW confidence of " + \
str(output[top_inds[0]]) + " in " + str(difference)
elif output[top_inds[0]] <= Server.confs["Classifier"]["InceptionThreshold"] and Server.Movidius.classes[top_inds[0]] == "0":
classification = "AML Negative"
message = "ALL not detected with a LOW confidence of " + \
str(output[top_inds[0]]) + " in " + str(difference)
Server.Helpers.logger.info(message)
ServerResponse = jsonpickle.encode({
'Response': 'OK',
'Classification': classification,
'aClassification': Server.Movidius.classes[top_inds[0]],
'Confidence': str(output[top_inds[0]]),
'Message': message
})
return Response(response=ServerResponse, status=200, mimetype="application/json")
if __name__ == "__main__":
app.run(host=Server.confs["Classifier"]["IP"],
port=Server.confs["Classifier"]["Port"])