-
Notifications
You must be signed in to change notification settings - Fork 6
/
custom.cpp
172 lines (148 loc) · 5.99 KB
/
custom.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Edge Impulse Linux SDK
* Copyright (c) 2021 EdgeImpulse Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <sstream>
#include "edge-impulse-sdk/classifier/ei_run_classifier.h"
#include "inc/bitmap_helper.h"
std::string trim(const std::string& str) {
size_t first = str.find_first_not_of(' ');
if (std::string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
std::string read_file(const char *filename) {
FILE *f = (FILE*)fopen(filename, "r");
if (!f) {
printf("Cannot open file %s\n", filename);
return "";
}
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
std::string ss;
ss.resize(size);
rewind(f);
fread(&ss[0], 1, size, f);
fclose(f);
return ss;
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("Requires one parameter (a comma-separated list of raw features, or a file pointing at raw features)\n");
return 1;
}
std::string input = argv[1];
if (!strchr(argv[1], ' ') && strchr(argv[1], '.')) { // looks like a filename
input = read_file(argv[1]);
}
std::istringstream ss(input);
std::string token;
std::vector<float> raw_features;
while (std::getline(ss, token, ',')) {
raw_features.push_back(std::stof(trim(token)));
}
if (raw_features.size() != EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE) {
printf("The size of your 'features' array is not correct. Expected %d items, but had %lu\n",
EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, raw_features.size());
return 1;
}
ei_impulse_result_t result;
signal_t signal;
numpy::signal_from_buffer(&raw_features[0], raw_features.size(), &signal);
EI_IMPULSE_ERROR res = run_classifier(&signal, &result, false);
// print the predictions
printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n",
result.timing.dsp, result.timing.classification, result.timing.anomaly);
#if EI_CLASSIFIER_OBJECT_DETECTION == 1
printf("#Object detection results:\n");
bool bb_found = result.bounding_boxes[0].value > 0;
for (size_t ix = 0; ix < result.bounding_boxes_count; ix++) {
auto bb = result.bounding_boxes[ix];
if (bb.value == 0) {
continue;
}
printf("%s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height);
}
if (!bb_found) {
printf(" No objects found\n");
}
#elif (EI_CLASSIFIER_LABEL_COUNT == 1) && (!EI_CLASSIFIER_HAS_ANOMALY) // regression
printf("#Regression results:\n");
printf(" %s: ", result.classification[0].label);
printf("%.5f", result.classification[0].value);
printf("\n");
#elif EI_CLASSIFIER_LABEL_COUNT > 1 // if there is only one label, this is an anomaly only
printf("#Classification results:\n");
for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
printf(" %s: ", result.classification[ix].label);
printf("%.5f", result.classification[ix].value);
printf("\n");
}
#endif
#if EI_CLASSIFIER_HAS_ANOMALY == 3 // visual AD
printf("#Visual anomaly grid results:\n");
for (uint32_t i = 0; i < result.visual_ad_count; i++) {
ei_impulse_result_bounding_box_t bb = result.visual_ad_grid_cells[i];
if (bb.value == 0) {
continue;
}
printf("%s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height);
}
printf("Visual anomaly values: Mean %.3f Max %.3f\n", result.visual_ad_result.mean_value, result.visual_ad_result.max_value);
#elif (EI_CLASSIFIER_HAS_ANOMALY > 0) // except for visual AD
printf("Anomaly prediction: %.3f\n", result.anomaly);
#endif
// Add a debug.bmp file for object detection / visual AD results
#if (EI_CLASSIFIER_OBJECT_DETECTION == 1) || (EI_CLASSIFIER_HAS_ANOMALY == 3)
#if (EI_CLASSIFIER_OBJECT_DETECTION == 1)
for (size_t ix = 0; ix < result.bounding_boxes_count; ix++) {
auto bb = result.bounding_boxes[ix];
if (bb.value == 0) {
continue;
}
for (size_t x = bb.x; x < bb.x + bb.width; x++) {
for (size_t y = bb.y; y < bb.y + bb.height; y++) {
raw_features[(y * EI_CLASSIFIER_INPUT_WIDTH) + x] = (float)0x00ff00;
}
}
}
#endif
#if (EI_CLASSIFIER_HAS_ANOMALY == 3)
for (size_t ix = 0; ix < result.visual_ad_count; ix++) {
auto bb = result.visual_ad_grid_cells[ix];
if (bb.value == 0) {
continue;
}
for (size_t x = bb.x; x < bb.x + bb.width; x++) {
for (size_t y = bb.y; y < bb.y + bb.height; y++) {
raw_features[(y * EI_CLASSIFIER_INPUT_WIDTH) + x] = (float)0xff0000;
}
}
}
#endif
create_bitmap_file("debug.bmp", raw_features.data(), EI_CLASSIFIER_INPUT_WIDTH, EI_CLASSIFIER_INPUT_HEIGHT);
#endif
}