Switch to unified view

a b/Projects/NCS1/Classifier.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:         Classifier Class
10
# Description:   Classifier for the ALL Detection System 2019.
11
# License:       MIT License
12
# Last Modified: 2020-07-21
13
#
14
############################################################################################
15
16
import cv2, os, sys, time
17
18
import numpy as np
19
20
from mvnc import mvncapi as mvnc
21
22
from Classes.Helpers import Helpers
23
from Classes.Movidius import Movidius
24
25
26
class Classifier():
27
    """ ALL Detection System 2019 Classifier Class
28
29
    Classifier for the ALL Detection System 2019.
30
    """
31
32
    def __init__(self):
33
        """ Initializes the Classifier Class. """
34
35
        self.Helpers = Helpers("Classifier")
36
        self.confs = self.Helpers.confs
37
38
        self.Movidius = Movidius()
39
        self.Movidius.checkNCS()
40
        self.Movidius.loadInception()
41
42
        self.Helpers.logger.info("Classifier class initialization complete.")
43
44
45
Classifier = Classifier()
46
47
48
def main(argv):
49
50
    humanStart, clockStart = Classifier.Helpers.timerStart()
51
52
    Classifier.Helpers.logger.info(
53
        "ALL Detection System 2019 Classifier started.")
54
55
    files = 0
56
    correct = 0
57
    incorrect = 0
58
    low = 0
59
    lowCorrect = 0
60
    lowIncorrect = 0
61
62
    rootdir = Classifier.confs["Classifier"]["TestImagePath"]
63
64
    for testFile in os.listdir(rootdir):
65
        if os.path.splitext(testFile)[1] in Classifier.confs["Classifier"]["ValidIType"]:
66
67
            files += 1
68
            fileName = rootdir + "/" + testFile
69
70
            img = cv2.imread(fileName).astype(np.float32)
71
72
            Classifier.Helpers.logger.info(
73
                "Loaded test image " + fileName)
74
75
            dx, dy, dz = img.shape
76
            delta = float(abs(dy-dx))
77
78
            if dx > dy:
79
                img = img[int(0.5*delta):dx-int(0.5*delta), 0:dy]
80
            else:
81
                img = img[0:dx, int(0.5*delta):dy-int(0.5*delta)]
82
83
            img = cv2.resize(img, (Classifier.Movidius.reqsize,
84
                                   Classifier.Movidius.reqsize))
85
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
86
87
            for i in range(3):
88
                img[:, :, i] = (
89
                    img[:, :, i] - Classifier.Movidius.mean) * Classifier.Movidius.std
90
91
            detectionStart, detectionStart = Classifier.Helpers.timerStart()
92
93
            Classifier.Movidius.ncsGraph.LoadTensor(
94
                img.astype(np.float16), 'user object')
95
            output, userobj = Classifier.Movidius.ncsGraph.GetResult()
96
97
            detectionClockEnd, difference, detectionEnd = Classifier.Helpers.timerEnd(
98
                detectionStart)
99
100
            top_inds = output.argsort()[::-1][:5]
101
102
            if output[top_inds[0]] >= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "1":
103
                if "_1." in fileName:
104
                    correct += 1
105
                    Classifier.Helpers.logger.info(
106
                        "ALL correctly detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
107
                else:
108
                    incorrect += 1
109
                    Classifier.Helpers.logger.warning(
110
                        "ALL incorrectly detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
111
112
            elif output[top_inds[0]] >= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "0":
113
                if "_0." in fileName:
114
                    correct += 1
115
                    Classifier.Helpers.logger.info(
116
                        "ALL correctly not detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
117
                else:
118
                    incorrect += 1
119
                    Classifier.Helpers.logger.warning(
120
                        "ALL incorrectly not detected with confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
121
122
            elif output[top_inds[0]] <= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "1":
123
                if "_1." in fileName:
124
                    correct += 1
125
                    low += 1
126
                    lowCorrect += 1
127
                    Classifier.Helpers.logger.info(
128
                        "ALL correctly detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
129
                else:
130
                    incorrect += 1
131
                    low += 1
132
                    lowIncorrect += 1
133
                    Classifier.Helpers.logger.warning(
134
                        "ALL incorrectly detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
135
136
            elif output[top_inds[0]] <= Classifier.confs["Classifier"]["InceptionThreshold"] and Classifier.Movidius.classes[top_inds[0]] == "0":
137
                if "_0." in fileName:
138
                    correct += 1
139
                    low += 1
140
                    lowCorrect += 1
141
                    Classifier.Helpers.logger.info(
142
                        "ALL correctly not detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
143
                else:
144
                    low += 1
145
                    incorrect += 1
146
                    lowIncorrect += 1
147
                    Classifier.Helpers.logger.warning(
148
                        "ALL incorrectly not detected with LOW confidence of " + str(output[top_inds[0]]) + " in " + str(difference) + " seconds.")
149
150
    clockEnd, difference, humanEnd = Classifier.Helpers.timerEnd(clockStart)
151
    Classifier.Helpers.logger.info("Testing ended. " + str(correct) + " correct, " + str(incorrect) +
152
                                   " incorrect, " + str(low) + " low confidence: (" + str(lowCorrect) + " correct, " + str(lowIncorrect) + " incorrect)")
153
154
    Classifier.Movidius.ncsDevice.CloseDevice()
155
156
157
if __name__ == "__main__":
158
    main(sys.argv[1:])