-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServer.py
More file actions
100 lines (75 loc) · 3.28 KB
/
Copy pathServer.py
File metadata and controls
100 lines (75 loc) · 3.28 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
############################################################################################
#
# Project: Peter Moss Acute Myeloid & Lymphoblastic Leukemia AI Research Project
# Repository: ALL Detection System 2020
# Project: AllDS2020 CNN
#
# Author: Adam Milton-Barker (AdamMiltonBarker.com)
# Contributors:
# Title: Server helper class
# Description: Server functions for the Acute Lymphoblastic Leukemia Tensorflow CNN 2020.
# License: MIT License
# Last Modified: 2020-07-23
#
############################################################################################
import jsonpickle
from flask import Flask, request, Response
from Classes.Helpers import Helpers
class Server():
""" Server helper class
Server functions for the Acute Lymphoblastic Leukemia Tensorflow CNN 2020.
"""
def __init__(self, model):
""" Initializes the class. """
self.Helpers = Helpers("Server", False)
self.model = model
def start(self):
""" Starts the server. """
app = Flask(__name__)
@app.route('/Inference', methods=['POST'])
def Inference():
""" Responds to standard HTTP request. """
message = ""
classification = self.model.http_classify(request)
if classification == 1:
message = "Acute Lymphoblastic Leukemia detected!"
diagnosis = "Positive"
elif classification == 0:
message = "Acute Lymphoblastic Leukemia not detected!"
diagnosis = "Negative"
resp = jsonpickle.encode({
'Response': 'OK',
'Message': message,
'Diagnosis': diagnosis
})
return Response(response=resp, status=200, mimetype="application/json")
@app.route('/VRInference', methods=['POST'])
def VRInference(id):
""" Responds to requests from Oculus Rift. """
t_drive = self.Helpers.confs["cnn"]["data"]["test_data"]
if int(id)-1 > len(t_drive):
ServerResponse = jsonpickle.encode({
'Response': 'FAILED',
'Message': 'No testing data with provided ID'
})
i = int(id)-1
test_image = self.Helpers.confs["cnn"]["data"]["test"] + "/" + t_drive[i]
if not os.path.isfile(test_image):
ServerResponse = jsonpickle.encode({
'Response': 'FAILED',
'Message': 'No testing data with filename exists'
})
message = ""
classification = self.model.vr_http_classify(cv2.imread(test_image))
if classification == 1:
msg = "Positive"
elif classification == 0:
message = "Negative"
resp = jsonpickle.encode({
'Response': 'OK',
'Message': message,
'Classification': classification
})
return Response(response=resp, status=200, mimetype="application/json")
app.run(host = self.Helpers.confs["cnn"]["api"]["server"],
port = self.Helpers.confs["cnn"]["api"]["port"])