|
| 1 | +/** |
| 2 | + * @file MBNet_1000.cpp |
| 3 | + * @brief Detect object type |
| 4 | + |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +#include "MBNet_1000.h" |
| 9 | +#include "names.h" |
| 10 | +#include "stdlib.h" |
| 11 | + |
| 12 | + |
| 13 | +MBNet_1000::MBNet_1000(KPUClass& kpu, Sipeed_ST7789& lcd, Sipeed_OV2640& camera) |
| 14 | +:_kpu(kpu),_lcd(lcd), _camera(camera), |
| 15 | +_count(0), _result(nullptr) |
| 16 | +{ |
| 17 | + _names = mbnet_label_name; |
| 18 | + memset(_statistics, 0, sizeof(_statistics)); |
| 19 | +} |
| 20 | + |
| 21 | +MBNet_1000::~MBNet_1000() |
| 22 | +{ |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +int MBNet_1000::begin(const char* kmodel_name) |
| 28 | +{ |
| 29 | + File myFile; |
| 30 | + if(!_camera.begin()) |
| 31 | + return -1; |
| 32 | + if(!_lcd.begin(15000000, COLOR_RED)) |
| 33 | + return -2; |
| 34 | + _camera.run(true); |
| 35 | + |
| 36 | + if (!SD.begin()) |
| 37 | + return -3; |
| 38 | + |
| 39 | + myFile = SD.open(kmodel_name); |
| 40 | + if (!myFile) |
| 41 | + return -4; |
| 42 | + uint32_t fSize = myFile.size(); |
| 43 | + _lcd.setTextSize(2); |
| 44 | + _lcd.setTextColor(COLOR_WHITE); |
| 45 | + _lcd.setCursor(100,100); |
| 46 | + _lcd.print("Loading ... "); |
| 47 | + long ret = myFile.read(_model, fSize); |
| 48 | + myFile.close(); |
| 49 | + if(ret != fSize) |
| 50 | + return -5; |
| 51 | + |
| 52 | + if(_kpu.begin(_model) != 0) |
| 53 | + return -6; |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +int MBNet_1000::detect() |
| 58 | +{ |
| 59 | + uint8_t* img; |
| 60 | + uint8_t* img888; |
| 61 | + |
| 62 | + img = _camera.snapshot(); |
| 63 | + if(img == nullptr || img==0) |
| 64 | + return -1; |
| 65 | + img888 = _camera.getRGB888(); |
| 66 | + if(_kpu.forward(img888) != 0) |
| 67 | + { |
| 68 | + return -2; |
| 69 | + } |
| 70 | + while( !_kpu.isForwardOk() ); |
| 71 | + if( _kpu.getResult((uint8_t**)&_result, &_count) != 0) |
| 72 | + { |
| 73 | + return -3; |
| 74 | + } |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +void MBNet_1000::show() |
| 79 | +{ |
| 80 | + float prob; |
| 81 | + const char* name; |
| 82 | + uint8_t i, j; |
| 83 | + uint16_t* img; |
| 84 | + |
| 85 | + _count /= sizeof(float); |
| 86 | + label_init(); |
| 87 | + label_sort(); |
| 88 | + |
| 89 | + for(j=0; j<STATISTICS_NUM; ++j) |
| 90 | + _statistics[j].updated = false; |
| 91 | + for ( i = 0; i < 5; i++) |
| 92 | + { |
| 93 | + label_get(i, &prob, &name); |
| 94 | + for(j=0; j<STATISTICS_NUM; ++j) |
| 95 | + { |
| 96 | + if(_statistics[j].name == NULL) |
| 97 | + { |
| 98 | + _statistics[j].name = name; |
| 99 | + _statistics[j].sum = prob; |
| 100 | + _statistics[j].updated = true; |
| 101 | + break; |
| 102 | + } |
| 103 | + else if( _statistics[j].name == name ) |
| 104 | + { |
| 105 | + _statistics[j].sum += prob; |
| 106 | + _statistics[j].updated = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + } |
| 112 | + } |
| 113 | + if( j == STATISTICS_NUM) |
| 114 | + { |
| 115 | + float min = _statistics[0].sum; |
| 116 | + j = 0; |
| 117 | + for(i=1; i<STATISTICS_NUM; ++i) |
| 118 | + { |
| 119 | + if(_statistics[i].name) |
| 120 | + { |
| 121 | + if(_statistics[i].sum <= min) |
| 122 | + { |
| 123 | + min = _statistics[i].sum; |
| 124 | + j = i; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + _statistics[j].name = name; |
| 129 | + _statistics[j].sum = prob; |
| 130 | + _statistics[j].updated = true; |
| 131 | + } |
| 132 | + } |
| 133 | + float max = _statistics[0].sum; |
| 134 | + float second = 0; |
| 135 | + uint8_t index1=0, index2 = 0; |
| 136 | + for(i=0; i<STATISTICS_NUM; ++i) |
| 137 | + { |
| 138 | + if(_statistics[i].name) |
| 139 | + { |
| 140 | + if(_statistics[i].sum > max) |
| 141 | + { |
| 142 | + max = _statistics[i].sum; |
| 143 | + index1 = i; |
| 144 | + } |
| 145 | + else if(_statistics[i].sum > second && _statistics[i].sum<max) |
| 146 | + { |
| 147 | + index2 = i; |
| 148 | + } |
| 149 | + } |
| 150 | + if( !_statistics[i].updated ) |
| 151 | + { |
| 152 | + float tmp = _statistics[i].sum - _statistics[i].sum*2/5; |
| 153 | + if( tmp < 0) |
| 154 | + tmp = 0; |
| 155 | + _statistics[i].sum = tmp; |
| 156 | + } |
| 157 | + } |
| 158 | + img = _camera.getRGB565(); |
| 159 | + _lcd.fillRect(224,0, _lcd.width()-224, _lcd.height(), COLOR_RED); |
| 160 | + _lcd.drawImage(0, 0, _camera.width(), _camera.height(), img); |
| 161 | + _lcd.setTextSize(2); |
| 162 | + _lcd.setTextColor(COLOR_WHITE); |
| 163 | + _lcd.setCursor(0,0); |
| 164 | + _lcd.println(_statistics[index1].name); |
| 165 | + _lcd.println("-----"); |
| 166 | + _lcd.println(_statistics[index2].name); |
| 167 | + // _lcd.println("======="); |
| 168 | + // _lcd.println(_names[_index[0]]); |
| 169 | + // _lcd.println(_names[_index[1]]); |
| 170 | + // _lcd.println(_names[_index[2]]); |
| 171 | + // _lcd.println(_names[_index[3]]); |
| 172 | + // _lcd.println(_names[_index[4]]); |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +void MBNet_1000::label_init( ) |
| 177 | +{ |
| 178 | + int i; |
| 179 | + for(i = 0; i < _count; i++) |
| 180 | + _index[i] = i; |
| 181 | +} |
| 182 | + |
| 183 | +void MBNet_1000::label_sort(void) |
| 184 | +{ |
| 185 | + int i,j; |
| 186 | + float tmp_prob; |
| 187 | + uint16_t tmp_index; |
| 188 | + for(j=0; j<_count; j++) |
| 189 | + for(i=0; i<_count-1-j; i++) |
| 190 | + if(_result[i]<_result[i+1]) |
| 191 | + { |
| 192 | + tmp_prob=_result[i]; |
| 193 | + _result[i]=_result[i+1]; |
| 194 | + _result[i+1]=tmp_prob; |
| 195 | + |
| 196 | + tmp_index=_index[i]; |
| 197 | + _index[i]=_index[i+1]; |
| 198 | + _index[i+1]=tmp_index; |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +void MBNet_1000::label_get(uint16_t index, float* prob, const char** name) |
| 204 | +{ |
| 205 | + *prob = _result[index]; |
| 206 | + *name = _names[_index[index]]; |
| 207 | +} |
| 208 | + |
0 commit comments