-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathClassifier.py
158 lines (119 loc) · 6.19 KB
/
Classifier.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
############################################################################################
#
# 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: Classifier Class
# Description: Classifier for the ALL Detection System 2019.
# License: MIT License
# Last Modified: 2020-07-21
#
############################################################################################
import cv2, os, sys, time
import numpy as np
from mvnc import mvncapi as mvnc
from Classes.Helpers import Helpers
from Classes.Movidius import Movidius
class Classifier():
""" ALL Detection System 2019 Classifier Class
Classifier for the ALL Detection System 2019.
"""
def __init__(self):
""" Initializes the Classifier Class. """
self.Helpers = Helpers("Classifier")
self.confs = self.Helpers.confs
self.Movidius = Movidius()
self.Movidius.checkNCS()
self.Movidius.loadInception()
self.Helpers.logger.info("Classifier class initialization complete.")
Classifier = Classifier()
def main(argv):
humanStart, clockStart = Classifier.Helpers.timerStart()
Classifier.Helpers.logger.info(
"ALL Detection System 2019 Classifier started.")
files = 0
correct = 0
incorrect = 0
low = 0
lowCorrect = 0
lowIncorrect = 0
rootdir = Classifier.confs["Classifier"]["TestImagePath"]
for testFile in os.listdir(rootdir):
if os.path.splitext(testFile)[1] in Classifier.confs["Classifier"]["ValidIType"]:
files += 1
fileName = rootdir + "/" + testFile
img = cv2.imread(fileName).astype(np.float32)
Classifier.Helpers.logger.info(
"Loaded test image " + fileName)
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, (Classifier.Movidius.reqsize,
Classifier.Movidius.reqsize))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in range(3):
img[:, :, i] = (
img[:, :, i] - Classifier.Movidius.mean) * Classifier.Movidius.std
detectionStart, detectionStart = Classifier.Helpers.timerStart()
Classifier.Movidius.ncsGraph.LoadTensor(
img.astype(np.float16), 'user object')
output, userobj = Classifier.Movidius.ncsGraph.GetResult()
detectionClockEnd, difference, detectionEnd = Classifier.Helpers.timerEnd(
detectionStart)
top_inds = output.argsort()[::-1][:5]
if output[top_inds[0]] >= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "1":
if "_1." in fileName:
correct += 1
Classifier.Helpers.logger.info(
"ALL correctly detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
else:
incorrect += 1
Classifier.Helpers.logger.warning(
"ALL incorrectly detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
elif output[top_inds[0]] >= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "0":
if "_0." in fileName:
correct += 1
Classifier.Helpers.logger.info(
"ALL correctly not detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
else:
incorrect += 1
Classifier.Helpers.logger.warning(
"ALL incorrectly not detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
elif output[top_inds[0]] <= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "1":
if "_1." in fileName:
correct += 1
low += 1
lowCorrect += 1
Classifier.Helpers.logger.info(
"ALL correctly detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
else:
incorrect += 1
low += 1
lowIncorrect += 1
Classifier.Helpers.logger.warning(
"ALL incorrectly detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
elif output[top_inds[0]] <= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "0":
if "_0." in fileName:
correct += 1
low += 1
lowCorrect += 1
Classifier.Helpers.logger.info(
"ALL correctly not detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
else:
low += 1
incorrect += 1
lowIncorrect += 1
Classifier.Helpers.logger.warning(
"ALL incorrectly not detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
clockEnd, difference, humanEnd = Classifier.Helpers.timerEnd(clockStart)
Classifier.Helpers.logger.info("Testing ended. " + str(correct) + " correct, " + str(incorrect) +
" incorrect, " + str(low) + " low confidence: (" + str(lowCorrect) + " correct, " + str(lowIncorrect) + " incorrect)")
Classifier.Movidius.ncsDevice.CloseDevice()
if __name__ == "__main__":
main(sys.argv[1:])