|
a |
|
b/Projects/NCS1/Client.py |
|
|
1 |
############################################################################################ |
|
|
2 |
# |
|
|
3 |
# Project: Peter Moss Acute Myeloid & Lymphoblastic Leukemia AI Research Project |
|
|
4 |
# Repository: ALL Detection System 2019 |
|
|
5 |
# Project: Facial Authentication Server |
|
|
6 |
# |
|
|
7 |
# Author: Adam Milton-Barker (AdamMiltonBarker.com) |
|
|
8 |
# Contributors: |
|
|
9 |
# Title: Client Class |
|
|
10 |
# Description: Sends single or multiple images to the ALL Detection System 2019 Classifier. |
|
|
11 |
# License: MIT License |
|
|
12 |
# Last Modified: 2020-07-21 |
|
|
13 |
# |
|
|
14 |
############################################################################################ |
|
|
15 |
|
|
|
16 |
import cv2, json, os, requests, sys, time |
|
|
17 |
|
|
|
18 |
from Classes.Helpers import Helpers |
|
|
19 |
|
|
|
20 |
|
|
|
21 |
class Client(): |
|
|
22 |
""" ALL Detection System 2019 Client Class |
|
|
23 |
|
|
|
24 |
Sends single or multiple images to the ALL Detection System 2019 Classifier. |
|
|
25 |
""" |
|
|
26 |
|
|
|
27 |
def __init__(self): |
|
|
28 |
""" Initializes the Client Class. """ |
|
|
29 |
|
|
|
30 |
self.Helpers = Helpers("Client") |
|
|
31 |
self.confs = self.Helpers.confs |
|
|
32 |
|
|
|
33 |
self.addr = "http://"+self.confs["Classifier"]["IP"] + \ |
|
|
34 |
':'+str(self.confs["Classifier"]["Port"]) + '/Inference' |
|
|
35 |
self.headers = {'content-type': 'image/jpeg'} |
|
|
36 |
|
|
|
37 |
self.Helpers.logger.info("Classifier class initialization complete.") |
|
|
38 |
|
|
|
39 |
def send(self, imagePath): |
|
|
40 |
""" Sends image to the inference API endpoint. """ |
|
|
41 |
|
|
|
42 |
_, img_encoded = cv2.imencode('.png', cv2.imread(imagePath)) |
|
|
43 |
response = requests.post( |
|
|
44 |
self.addr, data=img_encoded.tostring(), headers=self.headers) |
|
|
45 |
response = json.loads(response.text) |
|
|
46 |
|
|
|
47 |
self.Helpers.logger.info(imagePath + ": " + response["Message"]) |
|
|
48 |
|
|
|
49 |
def test(self): |
|
|
50 |
""" Loops through all images in the testing directory and sends |
|
|
51 |
them to the inference API endpoint. """ |
|
|
52 |
|
|
|
53 |
testingDir = self.confs["Classifier"]["NetworkPath"] + \ |
|
|
54 |
self.confs["Classifier"]["TestImagePath"] + "/" |
|
|
55 |
|
|
|
56 |
for test in os.listdir(testingDir): |
|
|
57 |
if os.path.splitext(test)[1] in self.confs["Classifier"]["ValidIType"]: |
|
|
58 |
self.send(testingDir+test) |
|
|
59 |
time.sleep(7) |
|
|
60 |
|
|
|
61 |
|
|
|
62 |
if __name__ == "__main__": |
|
|
63 |
|
|
|
64 |
Client = Client() |
|
|
65 |
|
|
|
66 |
if sys.argv[1] == "Send": |
|
|
67 |
|
|
|
68 |
Client.send(sys.argv[2]) |
|
|
69 |
|
|
|
70 |
elif sys.argv[1] == "Test": |
|
|
71 |
|
|
|
72 |
Client.test() |