ClassFlowCNNGeneral.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. #include "ClassFlowCNNGeneral.h"
  2. #include <math.h>
  3. #include <iomanip>
  4. #include <new>
  5. #include <sys/types.h>
  6. #include <sstream> // std::stringstream
  7. #include "CTfLiteClass.h"
  8. #include "ClassLogFile.h"
  9. #include "esp_log.h"
  10. #include "../../include/defines.h"
  11. #include "Helper.h"
  12. static const char* TAG = "CNN";
  13. //#ifdef CONFIG_HEAP_TRACING_STANDALONE
  14. #ifdef HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT
  15. #include <esp_heap_trace.h>
  16. #define NUM_RECORDS 300
  17. static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM
  18. #endif
  19. ClassFlowCNNGeneral::ClassFlowCNNGeneral(ClassFlowAlignment *_flowalign, t_CNNType _cnntype) : ClassFlowImage(NULL, TAG) {
  20. string cnnmodelfile = "";
  21. modelxsize = 1;
  22. modelysize = 1;
  23. CNNGoodThreshold = 0.0;
  24. ListFlowControll = NULL;
  25. previousElement = NULL;
  26. SaveAllFiles = false;
  27. disabled = false;
  28. isLogImageSelect = false;
  29. CNNType = AutoDetect;
  30. CNNType = _cnntype;
  31. flowpostalignment = _flowalign;
  32. imagesRetention = 5;
  33. }
  34. ClassFlowCNNGeneral::~ClassFlowCNNGeneral()
  35. {
  36. for (general* sequence : GENERAL) {
  37. if (sequence == NULL) continue;
  38. for (roi* region : sequence->ROI) {
  39. if (region == NULL) continue;
  40. delete region->image;
  41. region->image = NULL;
  42. delete region->image_org;
  43. region->image_org = NULL;
  44. delete region;
  45. }
  46. sequence->ROI.clear();
  47. delete sequence;
  48. }
  49. GENERAL.clear();
  50. }
  51. string ClassFlowCNNGeneral::getReadout(int _analog = 0, bool _extendedResolution, int prev, float _before_narrow_Analog, float AnalogToDigitTransitionStart, bool ContinuousLastDigit) {
  52. string result = "";
  53. if (GENERAL[_analog]->ROI.size() == 0) {
  54. return result;
  55. }
  56. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "getReadout _analog=" + std::to_string(_analog) + ", _extendedResolution=" + std::to_string(_extendedResolution) + ", prev=" + std::to_string(prev));
  57. if (CNNType == Analogue || CNNType == Analogue100) {
  58. float number = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  59. int result_after_decimal_point = ((int) floor(number * 10) + 10) % 10;
  60. prev = PointerEvalAnalogNew(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev);
  61. // LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "getReadout(analog) number=" + std::to_string(number) + ", result_after_decimal_point=" + std::to_string(result_after_decimal_point) + ", prev=" + std::to_string(prev));
  62. result = std::to_string(prev);
  63. if (_extendedResolution) {
  64. result = result + std::to_string(result_after_decimal_point);
  65. }
  66. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i) {
  67. prev = PointerEvalAnalogNew(GENERAL[_analog]->ROI[i]->result_float, prev);
  68. result = std::to_string(prev) + result;
  69. }
  70. return result;
  71. }
  72. if (CNNType == Digit) {
  73. for (int i = 0; i < GENERAL[_analog]->ROI.size(); ++i) {
  74. if ((GENERAL[_analog]->ROI[i]->result_klasse >= 0) && (GENERAL[_analog]->ROI[i]->result_klasse < 10)) {
  75. result = result + std::to_string(GENERAL[_analog]->ROI[i]->result_klasse);
  76. }
  77. else {
  78. result = result + "N";
  79. }
  80. }
  81. return result;
  82. }
  83. if ((CNNType == DoubleHyprid10) || (CNNType == Digit100)) {
  84. float number = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  85. // NaN?
  86. if ((number >= 0) && (number < 10)) {
  87. // is only set if it is the first digit (no analogue before!)
  88. if (_extendedResolution) {
  89. int result_after_decimal_point = ((int) floor(number * 10)) % 10;
  90. int result_before_decimal_point = ((int) floor(number)) % 10;
  91. result = std::to_string(result_before_decimal_point) + std::to_string(result_after_decimal_point);
  92. prev = result_before_decimal_point;
  93. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "getReadout(dig100-ext) result_before_decimal_point=" + std::to_string(result_before_decimal_point) + ", result_after_decimal_point=" + std::to_string(result_after_decimal_point) + ", prev=" + std::to_string(prev));
  94. }
  95. else {
  96. if (_before_narrow_Analog >= 0) {
  97. prev = PointerEvalHybridNew(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, _before_narrow_Analog, prev, true, AnalogToDigitTransitionStart, ContinuousLastDigit);
  98. }
  99. else {
  100. prev = PointerEvalHybridNew(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev, prev);
  101. }
  102. // is necessary because a number greater than 9.994999 returns a 10! (for further details see check in PointerEvalHybridNew)
  103. if ((prev >= 0) && (prev < 10)) {
  104. result = std::to_string(prev);
  105. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "getReadout(dig100) prev=" + std::to_string(prev));
  106. }
  107. else {
  108. result = "N";
  109. }
  110. }
  111. }
  112. else {
  113. result = "N";
  114. if (_extendedResolution && (CNNType != Digit)) {
  115. result = "NN";
  116. }
  117. }
  118. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i) {
  119. if ((GENERAL[_analog]->ROI[i]->result_float >= 0) && (GENERAL[_analog]->ROI[i]->result_float < 10)) {
  120. prev = PointerEvalHybridNew(GENERAL[_analog]->ROI[i]->result_float, GENERAL[_analog]->ROI[i+1]->result_float, prev);
  121. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "getReadout#PointerEvalHybridNew()= " + std::to_string(prev));
  122. result = std::to_string(prev) + result;
  123. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "getReadout#result= " + result);
  124. }
  125. else {
  126. prev = -1;
  127. result = "N" + result;
  128. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "getReadout(result_float<0 /'N') result_float=" + std::to_string(GENERAL[_analog]->ROI[i]->result_float));
  129. }
  130. }
  131. return result;
  132. }
  133. return result;
  134. }
  135. /**
  136. * @brief Determines the number of an ROI in connection with previous ROI results
  137. *
  138. * @param number: is the current ROI as float value from recognition
  139. * @param number_of_predecessors: is the last (lower) ROI as float from recognition
  140. * @param eval_predecessors: is the evaluated number. Sometimes a much lower value can change higer values
  141. * example: 9.8, 9.9, 0.1
  142. * 0.1 => 0 (eval_predecessors)
  143. * The 0 makes a 9.9 to 0 (eval_predecessors)
  144. * The 0 makes a 9.8 to 0
  145. * @param Analog_Predecessors false/true if the last ROI is an analog or digit ROI (default=false)
  146. * runs in special handling because analog is much less precise
  147. * @param digitAnalogTransitionStart start of the transitionlogic begins on number_of_predecessor (default=9.2)
  148. * @param ContinuousLastDigit the digit before the analog ROIs rolls continuously with the first pointer (default=false)
  149. *
  150. * @return int the determined number of the current ROI
  151. */
  152. int ClassFlowCNNGeneral::PointerEvalHybridNew(float number, float number_of_predecessors, int eval_predecessors, bool Analog_Predecessors, float digitAnalogTransitionStart, bool ContinuousLastDigit) {
  153. int result;
  154. int result_after_decimal_point = ((int) floor(number * 10)) % 10;
  155. int result_before_decimal_point = ((int) floor(number) + 10) % 10;
  156. if (eval_predecessors < 0) {
  157. // on first digit is no spezial logic for transition needed
  158. // we use the recognition as given. The result is the int value of the recognition
  159. // add precisition of 2 digits and round before trunc
  160. // a number greater than 9.994999 is returned as 10, this leads to an error during the decimal shift because the NUMBERS[j]->ReturnRawValue is one digit longer.
  161. // To avoid this, an additional test must be carried out, see "if ((CNNType == DoubleHyprid10) || (CNNType == Digit100))" check in getReadout()
  162. // Another alternative would be "result = (int) ((int) trunc(round((number+10 % 10)*1000))) / 1000;", which could, however, lead to other errors?
  163. result = (int) ((int) trunc(round((number+10 % 10)*100)) ) / 100;
  164. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalHybridNew - No predecessor - Result = " + std::to_string(result) +
  165. " number: " + std::to_string(number) + " number_of_predecessors = " + std::to_string(number_of_predecessors)+ " eval_predecessors = " + std::to_string(eval_predecessors) + " Digit_Uncertainty = " + std::to_string(Digit_Uncertainty));
  166. return result;
  167. }
  168. if (Analog_Predecessors) {
  169. result = PointerEvalAnalogToDigitNew(number, number_of_predecessors, eval_predecessors, digitAnalogTransitionStart, ContinuousLastDigit);
  170. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalHybridNew - Analog predecessor, evaluation over PointerEvalAnalogNew = " + std::to_string(result) +
  171. " number: " + std::to_string(number) + " number_of_predecessors = " + std::to_string(number_of_predecessors)+ " eval_predecessors = " + std::to_string(eval_predecessors) + " Digit_Uncertainty = " + std::to_string(Digit_Uncertainty));
  172. return result;
  173. }
  174. if ((number_of_predecessors >= Digit_Transition_Area_Predecessor ) && (number_of_predecessors <= (10.0 - Digit_Transition_Area_Predecessor))) {
  175. // no digit change, because predecessor is far enough away (0+/-DigitTransitionRangePredecessor) --> number is rounded
  176. // Band around the digit --> Round off, as digit reaches inaccuracy in the frame
  177. if ((result_after_decimal_point <= DigitBand) || (result_after_decimal_point >= (10-DigitBand))) {
  178. result = ((int) round(number) + 10) % 10;
  179. }
  180. else {
  181. result = ((int) trunc(number) + 10) % 10;
  182. }
  183. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalHybridNew - NO analogue predecessor, no change of digits, as pre-decimal point far enough away = " + std::to_string(result) +
  184. " number: " + std::to_string(number) + " number_of_predecessors = " + std::to_string(number_of_predecessors)+ " eval_predecessors = " + std::to_string(eval_predecessors) + " Digit_Uncertainty = " + std::to_string(Digit_Uncertainty));
  185. return result;
  186. }
  187. // Zero crossing at the predecessor has taken place (! evaluation via Prev_value and not number!) --> round up here (2.8 --> 3, but also 3.1 --> 3)
  188. if (eval_predecessors <= 1) {
  189. // We simply assume that the current digit after the zero crossing of the predecessor
  190. // has passed through at least half (x.5)
  191. if (result_after_decimal_point > 5) {
  192. // The current digit does not yet have a zero crossing, but the predecessor does..
  193. result = (result_before_decimal_point + 1) % 10;
  194. }
  195. else {
  196. // Act. digit and predecessor have zero crossing
  197. result = result_before_decimal_point % 10;
  198. }
  199. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalHybridNew - NO analogue predecessor, zero crossing has taken placen = " + std::to_string(result) +
  200. " number: " + std::to_string(number) + " number_of_predecessors = " + std::to_string(number_of_predecessors)+ " eval_predecessors = " + std::to_string(eval_predecessors) + " Digit_Uncertainty = " + std::to_string(Digit_Uncertainty));
  201. return result;
  202. }
  203. // remains only >= 9.x --> no zero crossing yet --> 2.8 --> 2,
  204. // and from 9.7(DigitTransitionRangeLead) 3.1 --> 2
  205. // everything >=x.4 can be considered as current number in transition. With 9.x predecessor the current
  206. // number can still be x.6 - x.7.
  207. // Preceding (else - branch) does not already happen from 9.
  208. if (Digit_Transition_Area_Forward>=number_of_predecessors || result_after_decimal_point >= 4) {
  209. // The current digit, like the previous digit, does not yet have a zero crossing.
  210. result = result_before_decimal_point % 10;
  211. }
  212. else {
  213. // current digit precedes the smaller digit (9.x). So already >=x.0 while the previous digit has not yet
  214. // has no zero crossing. Therefore, it is reduced by 1.
  215. result = (result_before_decimal_point - 1 + 10) % 10;
  216. }
  217. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalHybridNew - O analogue predecessor, >= 9.5 --> no zero crossing yet = " + std::to_string(result) +
  218. " number: " + std::to_string(number) + " number_of_predecessors = " + std::to_string(number_of_predecessors)+ " eval_predecessors = " + std::to_string(eval_predecessors) + " Digit_Uncertainty = " + std::to_string(Digit_Uncertainty) + " result_after_decimal_point = " + std::to_string(result_after_decimal_point));
  219. return result;
  220. }
  221. int ClassFlowCNNGeneral::PointerEvalAnalogToDigitNew(float number, float numeral_preceder, int eval_predecessors, float AnalogToDigitTransitionStart, bool ContinuousLastDigit) {
  222. int result;
  223. int result_after_decimal_point = ((int) floor(number * 10)) % 10;
  224. int result_before_decimal_point = ((int) floor(number) + 10) % 10;
  225. bool roundedUp = false;
  226. // Drum geared continuously to the first analog pointer: its position is digit + pointer/10
  227. // (e.g. 7.6 at pointer 6), so there is no step to detect. Remove the pointer's share and round;
  228. // this tolerates +/-0.5 of combined reading error and wraps 9 <-> 0 in both directions.
  229. // AnalogToDigitTransitionStart is not used in this mode.
  230. if (ContinuousLastDigit) {
  231. float pointer = numeral_preceder;
  232. // Near 0 the pointer reading can be on the wrong side (e.g. 9.8 although the next pointer shows
  233. // it has passed 0). The analog evaluation already resolved this (eval_predecessors), so move the
  234. // reading to that side: 9.8 evaluated as 0 -> -0.2, 0.1 evaluated as 9 -> 10.1.
  235. if (eval_predecessors >= 0) {
  236. int pointer_before_decimal_point = (int) floor(pointer);
  237. if (pointer_before_decimal_point - eval_predecessors >= 5) {
  238. pointer -= 10;
  239. }
  240. else if (eval_predecessors - pointer_before_decimal_point >= 5) {
  241. pointer += 10;
  242. }
  243. }
  244. result = ((int) round(number - pointer / 10.0) + 10) % 10;
  245. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogToDigitNew - Continuous last digit - Result = " + std::to_string(result) +
  246. " number: " + std::to_string(number) + " numeral_preceder = " + std::to_string(numeral_preceder) +
  247. " eval_predecessors = " + std::to_string(eval_predecessors));
  248. return result;
  249. }
  250. // Within the digit inequalities
  251. if ((result_after_decimal_point >= (10-Digit_Uncertainty * 10)) // Band around the digit --> Round off, as digit reaches inaccuracy in the frame
  252. || (eval_predecessors <= 4 && result_after_decimal_point>=6)) { // or digit runs after (analogue =0..4, digit >=6)
  253. result = (int) (round(number) + 10) % 10;
  254. roundedUp = true;
  255. // before/ after decimal point, because we adjust the number based on the uncertainty.
  256. result_after_decimal_point = ((int) floor(result * 10)) % 10;
  257. result_before_decimal_point = ((int) floor(result) + 10) % 10;
  258. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogToDigitNew - Digit Uncertainty - Result = " + std::to_string(result) +
  259. " number: " + std::to_string(number) + " numeral_preceder: " + std::to_string(numeral_preceder) +
  260. " erg before comma: " + std::to_string(result_before_decimal_point) +
  261. " erg after comma: " + std::to_string(result_after_decimal_point));
  262. }
  263. else {
  264. result = (int) ((int) trunc(number) + 10) % 10;
  265. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogToDigitNew - NO digit Uncertainty - Result = " + std::to_string(result) +
  266. " number: " + std::to_string(number) + " numeral_preceder = " + std::to_string(numeral_preceder));
  267. }
  268. // No zero crossing has taken place.
  269. // Only eval_predecessors used because numeral_preceder could be wrong here.
  270. // numeral_preceder<=0.1 & eval_predecessors=9 corresponds to analogue was reset because of previous analogue that are not yet at 0.
  271. if ((eval_predecessors>=6 && (numeral_preceder>AnalogToDigitTransitionStart || numeral_preceder<=0.2) && roundedUp)) {
  272. result = ((result_before_decimal_point+10) - 1) % 10;
  273. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogToDigitNew - Nulldurchgang noch nicht stattgefunden = " + std::to_string(result) +
  274. " number: " + std::to_string(number) +
  275. " numeral_preceder = " + std::to_string(numeral_preceder) +
  276. " eerg after comma = " + std::to_string(result_after_decimal_point));
  277. }
  278. return result;
  279. }
  280. int ClassFlowCNNGeneral::PointerEvalAnalogNew(float number, int numeral_preceder) {
  281. float number_min, number_max;
  282. int result;
  283. if (numeral_preceder == -1) {
  284. result = (int) floor(number);
  285. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogNew - No predecessor - Result = " + std::to_string(result) +
  286. " number: " + std::to_string(number) + " numeral_preceder = " + std::to_string(numeral_preceder) + " Analog_error = " + std::to_string(Analog_error));
  287. return result;
  288. }
  289. number_min = number - Analog_error / 10.0;
  290. number_max = number + Analog_error / 10.0;
  291. if ((int) floor(number_max) - (int) floor(number_min) != 0) {
  292. if (numeral_preceder <= Analog_error) {
  293. result = ((int) floor(number_max) + 10) % 10;
  294. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogNew - number ambiguous, correction upwards - result = " + std::to_string(result) +
  295. " number: " + std::to_string(number) + " numeral_preceder = " + std::to_string(numeral_preceder) + " Analog_error = " + std::to_string(Analog_error));
  296. return result;
  297. }
  298. if (numeral_preceder >= 10 - Analog_error) {
  299. result = ((int) floor(number_min) + 10) % 10;
  300. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogNew - number ambiguous, downward correction - result = " + std::to_string(result) +
  301. " number: " + std::to_string(number) + " numeral_preceder = " + std::to_string(numeral_preceder) + " Analog_error = " + std::to_string(Analog_error));
  302. return result;
  303. }
  304. }
  305. result = ((int) floor(number) + 10) % 10;
  306. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "PointerEvalAnalogNew - number unambiguous, no correction necessary - result = " + std::to_string(result) +
  307. " number: " + std::to_string(number) + " numeral_preceder = " + std::to_string(numeral_preceder) + " Analog_error = " + std::to_string(Analog_error));
  308. return result;
  309. }
  310. bool ClassFlowCNNGeneral::ReadParameter(FILE* pfile, string& aktparamgraph) {
  311. std::vector<string> splitted;
  312. aktparamgraph = trim(aktparamgraph);
  313. if (aktparamgraph.size() == 0) {
  314. if (!this->GetNextParagraph(pfile, aktparamgraph)) {
  315. return false;
  316. }
  317. }
  318. if ((toUpper(aktparamgraph) != "[ANALOG]") && (toUpper(aktparamgraph) != ";[ANALOG]")
  319. && (toUpper(aktparamgraph) != "[DIGIT]") && (toUpper(aktparamgraph) != ";[DIGIT]")
  320. && (toUpper(aktparamgraph) != "[DIGITS]") && (toUpper(aktparamgraph) != ";[DIGITS]")) {
  321. // Paragraph passt nicht
  322. return false;
  323. }
  324. if (aktparamgraph[0] == ';') {
  325. disabled = true;
  326. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  327. ESP_LOGD(TAG, "[Analog/Digit] is disabled!");
  328. return true;
  329. }
  330. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) {
  331. splitted = ZerlegeZeile(aktparamgraph);
  332. if ((toUpper(splitted[0]) == "ROIIMAGESLOCATION") && (splitted.size() > 1)) {
  333. this->imagesLocation = "/sdcard" + splitted[1];
  334. this->isLogImage = true;
  335. }
  336. if ((toUpper(splitted[0]) == "LOGIMAGESELECT") && (splitted.size() > 1)) {
  337. LogImageSelect = splitted[1];
  338. isLogImageSelect = true;
  339. }
  340. if ((toUpper(splitted[0]) == "ROIIMAGESRETENTION") && (splitted.size() > 1)) {
  341. if (isStringNumeric(splitted[1])) {
  342. this->imagesRetention = std::stoi(splitted[1]);
  343. }
  344. }
  345. if ((toUpper(splitted[0]) == "MODEL") && (splitted.size() > 1)) {
  346. this->cnnmodelfile = splitted[1];
  347. }
  348. if ((toUpper(splitted[0]) == "CNNGOODTHRESHOLD") && (splitted.size() > 1)) {
  349. if (isStringNumeric(splitted[1])) {
  350. CNNGoodThreshold = std::stof(splitted[1]);
  351. }
  352. }
  353. if (splitted.size() >= 5) {
  354. general* _analog = GetGENERAL(splitted[0], true);
  355. if (_analog == NULL || _analog->ROI.empty()) {
  356. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to create ROI " + splitted[0]);
  357. disabled = true;
  358. return false;
  359. }
  360. roi* neuroi = _analog->ROI[_analog->ROI.size()-1];
  361. neuroi->posx = std::stoi(splitted[1]);
  362. neuroi->posy = std::stoi(splitted[2]);
  363. neuroi->deltax = std::stoi(splitted[3]);
  364. neuroi->deltay = std::stoi(splitted[4]);
  365. neuroi->CCW = false;
  366. if (splitted.size() >= 6) {
  367. neuroi->CCW = toUpper(splitted[5]) == "TRUE";
  368. }
  369. neuroi->result_float = -1;
  370. neuroi->image = NULL;
  371. neuroi->image_org = NULL;
  372. }
  373. if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1)) {
  374. SaveAllFiles = alphanumericToBoolean(splitted[1]);
  375. }
  376. }
  377. if (!getNetworkParameter()) {
  378. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "An error occured on setting up the Network -> Disabling it!");
  379. disabled = true; // An error occured, disable this CNN!
  380. return false;
  381. }
  382. for (int _ana = 0; _ana < GENERAL.size(); ++_ana) {
  383. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i) {
  384. GENERAL[_ana]->ROI[i]->image = new (std::nothrow) CImageBasis("ROI " + GENERAL[_ana]->ROI[i]->name,
  385. modelxsize, modelysize, modelchannel);
  386. GENERAL[_ana]->ROI[i]->image_org = new (std::nothrow) CImageBasis("ROI " + GENERAL[_ana]->ROI[i]->name + " original",
  387. GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, 3);
  388. if (GENERAL[_ana]->ROI[i]->image == NULL || GENERAL[_ana]->ROI[i]->image_org == NULL ||
  389. !GENERAL[_ana]->ROI[i]->image->ImageOkay() || !GENERAL[_ana]->ROI[i]->image_org->ImageOkay()) {
  390. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate ROI image buffers for " + GENERAL[_ana]->ROI[i]->name);
  391. delete GENERAL[_ana]->ROI[i]->image;
  392. GENERAL[_ana]->ROI[i]->image = NULL;
  393. delete GENERAL[_ana]->ROI[i]->image_org;
  394. GENERAL[_ana]->ROI[i]->image_org = NULL;
  395. disabled = true;
  396. return false;
  397. }
  398. }
  399. }
  400. return true;
  401. }
  402. general* ClassFlowCNNGeneral::FindGENERAL(string _name_number) {
  403. for (int i = 0; i < GENERAL.size(); ++i) {
  404. if (GENERAL[i]->name == _name_number) {
  405. return GENERAL[i];
  406. }
  407. }
  408. return NULL;
  409. }
  410. general* ClassFlowCNNGeneral::GetGENERAL(string _name, bool _create = true) {
  411. string _analog, _roi;
  412. int _pospunkt = _name.find_first_of(".");
  413. if (_pospunkt > -1) {
  414. _analog = _name.substr(0, _pospunkt);
  415. _roi = _name.substr(_pospunkt+1, _name.length() - _pospunkt - 1);
  416. }
  417. else {
  418. _analog = "default";
  419. _roi = _name;
  420. }
  421. general *_ret = NULL;
  422. for (int i = 0; i < GENERAL.size(); ++i) {
  423. if (GENERAL[i]->name == _analog) {
  424. _ret = GENERAL[i];
  425. }
  426. }
  427. // not found and should not be created
  428. if (!_create) {
  429. return _ret;
  430. }
  431. if (_ret == NULL) {
  432. _ret = new (std::nothrow) general;
  433. if (_ret == NULL) {
  434. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate GENERAL sequence");
  435. return NULL;
  436. }
  437. _ret->name = _analog;
  438. GENERAL.push_back(_ret);
  439. }
  440. roi* neuroi = new (std::nothrow) roi;
  441. if (neuroi == NULL) {
  442. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate ROI descriptor");
  443. return NULL;
  444. }
  445. neuroi->image = NULL;
  446. neuroi->image_org = NULL;
  447. neuroi->result_float = -1;
  448. neuroi->result_klasse = -1;
  449. neuroi->isReject = true;
  450. neuroi->CCW = false;
  451. neuroi->name = _roi;
  452. _ret->ROI.push_back(neuroi);
  453. ESP_LOGD(TAG, "GetGENERAL - GENERAL %s - roi %s - CCW: %d", _analog.c_str(), _roi.c_str(), neuroi->CCW);
  454. return _ret;
  455. }
  456. string ClassFlowCNNGeneral::getHTMLSingleStep(string host) {
  457. string result, zw;
  458. std::vector<HTMLInfo*> htmlinfo;
  459. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  460. result = result + "Analog Pointers: <p> ";
  461. htmlinfo = GetHTMLInfo();
  462. for (int i = 0; i < htmlinfo.size(); ++i) {
  463. std::stringstream stream;
  464. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  465. zw = stream.str();
  466. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  467. delete htmlinfo[i];
  468. }
  469. htmlinfo.clear();
  470. return result;
  471. }
  472. bool ClassFlowCNNGeneral::doFlow(string time) {
  473. #ifdef HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT
  474. //register a buffer to record the memory trace
  475. ESP_ERROR_CHECK( heap_trace_init_standalone(trace_record, NUM_RECORDS) );
  476. // start tracing
  477. ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
  478. #endif
  479. if (disabled) {
  480. return true;
  481. }
  482. if (!doAlignAndCut(time)){
  483. return false;
  484. }
  485. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "doFlow after alignment");
  486. doNeuralNetwork(time);
  487. RemoveOldLogs();
  488. #ifdef HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT
  489. ESP_ERROR_CHECK( heap_trace_stop() );
  490. heap_trace_dump();
  491. #endif
  492. return true;
  493. }
  494. bool ClassFlowCNNGeneral::doAlignAndCut(string time) {
  495. if (disabled) {
  496. return true;
  497. }
  498. if (flowpostalignment == NULL) {
  499. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Alignment flow is unavailable");
  500. return false;
  501. }
  502. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  503. if (caic == NULL) {
  504. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Aligned source image is unavailable");
  505. return false;
  506. }
  507. for (int _ana = 0; _ana < GENERAL.size(); ++_ana) {
  508. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i) {
  509. roi *region = GENERAL[_ana]->ROI[i];
  510. if (region == NULL || region->image_org == NULL || region->image == NULL) {
  511. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ROI image buffers are unavailable");
  512. return false;
  513. }
  514. ESP_LOGD(TAG, "General %d - Align&Cut", i);
  515. if (!caic->CutAndSave(region->posx, region->posy, region->deltax, region->deltay, region->image_org)) {
  516. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ROI crop failed");
  517. return false;
  518. }
  519. if (SaveAllFiles) {
  520. if (GENERAL[_ana]->name == "default") {
  521. region->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + region->name + ".jpg"));
  522. } else {
  523. region->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + region->name + ".jpg"));
  524. }
  525. }
  526. if (!region->image_org->Resize(modelxsize, modelysize, region->image)) {
  527. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ROI resize failed");
  528. return false;
  529. }
  530. if (SaveAllFiles) {
  531. if (GENERAL[_ana]->name == "default") {
  532. region->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + region->name + ".jpg"));
  533. } else {
  534. region->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + region->name + ".jpg"));
  535. }
  536. }
  537. }
  538. }
  539. return true;
  540. }
  541. void ClassFlowCNNGeneral::DrawROI(CImageBasis *_zw) {
  542. if (_zw == NULL || !_zw->ImageOkay()) return;
  543. {
  544. if (CNNType == Analogue || CNNType == Analogue100) {
  545. int r = 0;
  546. int g = 255;
  547. int b = 0;
  548. for (int _ana = 0; _ana < GENERAL.size(); ++_ana) {
  549. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i) {
  550. _zw->drawRect(GENERAL[_ana]->ROI[i]->posx, GENERAL[_ana]->ROI[i]->posy, GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, r, g, b, 1);
  551. _zw->drawEllipse( (int) (GENERAL[_ana]->ROI[i]->posx + GENERAL[_ana]->ROI[i]->deltax/2), (int) (GENERAL[_ana]->ROI[i]->posy + GENERAL[_ana]->ROI[i]->deltay/2), (int) (GENERAL[_ana]->ROI[i]->deltax/2), (int) (GENERAL[_ana]->ROI[i]->deltay/2), r, g, b, 2);
  552. _zw->drawLine((int) (GENERAL[_ana]->ROI[i]->posx + GENERAL[_ana]->ROI[i]->deltax/2), (int) GENERAL[_ana]->ROI[i]->posy, (int) (GENERAL[_ana]->ROI[i]->posx + GENERAL[_ana]->ROI[i]->deltax/2), (int) (GENERAL[_ana]->ROI[i]->posy + GENERAL[_ana]->ROI[i]->deltay), r, g, b, 2);
  553. _zw->drawLine((int) GENERAL[_ana]->ROI[i]->posx, (int) (GENERAL[_ana]->ROI[i]->posy + GENERAL[_ana]->ROI[i]->deltay/2), (int) GENERAL[_ana]->ROI[i]->posx + GENERAL[_ana]->ROI[i]->deltax, (int) (GENERAL[_ana]->ROI[i]->posy + GENERAL[_ana]->ROI[i]->deltay/2), r, g, b, 2);
  554. }
  555. }
  556. }
  557. else {
  558. for (int _dig = 0; _dig < GENERAL.size(); ++_dig) {
  559. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i) {
  560. _zw->drawRect(GENERAL[_dig]->ROI[i]->posx, GENERAL[_dig]->ROI[i]->posy, GENERAL[_dig]->ROI[i]->deltax, GENERAL[_dig]->ROI[i]->deltay, 0, 0, (255 - _dig*100), 2);
  561. }
  562. }
  563. }
  564. }
  565. }
  566. bool ClassFlowCNNGeneral::getNetworkParameter() {
  567. if (disabled) {
  568. return true;
  569. }
  570. CTfLiteClass *tflite = new (std::nothrow) CTfLiteClass;
  571. if (tflite == NULL) {
  572. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate CTfLiteClass object");
  573. LogFile.WriteHeapInfo("CTfLiteClass object allocation");
  574. return false;
  575. }
  576. string zwcnn = "/sdcard" + cnnmodelfile;
  577. zwcnn = FormatFileName(zwcnn);
  578. ESP_LOGD(TAG, "%s", zwcnn.c_str());
  579. if (!tflite->LoadModel(zwcnn)) {
  580. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't load tflite model " + cnnmodelfile + " -> Init aborted!");
  581. LogFile.WriteHeapInfo("getNetworkParameter-LoadModel");
  582. delete tflite;
  583. return false;
  584. }
  585. if (!tflite->MakeAllocate()) {
  586. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate tflite model -> Init aborted!");
  587. LogFile.WriteHeapInfo("getNetworkParameter-MakeAllocate");
  588. delete tflite;
  589. return false;
  590. }
  591. if (CNNType == AutoDetect) {
  592. tflite->GetInputDimension(false);
  593. modelxsize = tflite->ReadInputDimenstion(0);
  594. modelysize = tflite->ReadInputDimenstion(1);
  595. modelchannel = tflite->ReadInputDimenstion(2);
  596. int _anzoutputdimensions = tflite->GetAnzOutPut();
  597. switch (_anzoutputdimensions) {
  598. case 2:
  599. CNNType = Analogue;
  600. ESP_LOGD(TAG, "TFlite-Type set to Analogue");
  601. break;
  602. case 10:
  603. CNNType = DoubleHyprid10;
  604. ESP_LOGD(TAG, "TFlite-Type set to DoubleHyprid10");
  605. break;
  606. case 11:
  607. CNNType = Digit;
  608. ESP_LOGD(TAG, "TFlite-Type set to Digit");
  609. break;
  610. /* case 20:
  611. CNNType = DigitHyprid10;
  612. ESP_LOGD(TAG, "TFlite-Type set to DigitHyprid10");
  613. break;
  614. */
  615. // case 22:
  616. // CNNType = DigitHyprid;
  617. // ESP_LOGD(TAG, "TFlite-Type set to DigitHyprid");
  618. // break;
  619. case 100:
  620. if (modelxsize==32 && modelysize == 32) {
  621. CNNType = Analogue100;
  622. ESP_LOGD(TAG, "TFlite-Type set to Analogue100");
  623. }
  624. else {
  625. CNNType = Digit100;
  626. ESP_LOGD(TAG, "TFlite-Type set to Digit");
  627. }
  628. break;
  629. default:
  630. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "tflite does not fit the firmware (outout_dimension=" + std::to_string(_anzoutputdimensions) + ")");
  631. }
  632. }
  633. delete tflite;
  634. return true;
  635. }
  636. bool ClassFlowCNNGeneral::doNeuralNetwork(string time) {
  637. if (disabled) {
  638. return true;
  639. }
  640. string logPath = CreateLogFolder(time);
  641. CTfLiteClass *tflite = new (std::nothrow) CTfLiteClass;
  642. if (tflite == NULL) {
  643. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate CTfLiteClass object");
  644. LogFile.WriteHeapInfo("CTfLiteClass object allocation");
  645. return false;
  646. }
  647. string zwcnn = "/sdcard" + cnnmodelfile;
  648. zwcnn = FormatFileName(zwcnn);
  649. ESP_LOGD(TAG, "%s", zwcnn.c_str());
  650. if (!tflite->LoadModel(zwcnn)) {
  651. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't load tflite model " + cnnmodelfile + " -> Exec aborted this round!");
  652. LogFile.WriteHeapInfo("doNeuralNetwork-LoadModel");
  653. delete tflite;
  654. return false;
  655. }
  656. if (!tflite->MakeAllocate()) {
  657. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate tfilte model -> Exec aborted this round!");
  658. LogFile.WriteHeapInfo("doNeuralNetwork-MakeAllocate");
  659. delete tflite;
  660. return false;
  661. }
  662. // For each NUMBER
  663. for (int n = 0; n < GENERAL.size(); ++n) {
  664. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Processing Number '" + GENERAL[n]->name + "'");
  665. // For each ROI
  666. for (int roi = 0; roi < GENERAL[n]->ROI.size(); ++roi) {
  667. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "ROI #" + std::to_string(roi) + " - TfLite");
  668. //ESP_LOGD(TAG, "General %d - TfLite", i);
  669. switch (CNNType) {
  670. case Analogue:
  671. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Analogue");
  672. {
  673. float f1 = 0, f2 = 0;
  674. if (!tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image)) {
  675. GENERAL[n]->ROI[roi]->isReject = true;
  676. GENERAL[n]->ROI[roi]->result_float = -1;
  677. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CNN input image could not be copied into Tensor Arena");
  678. continue;
  679. }
  680. tflite->Invoke();
  681. if (!tflite->LastInvokeSucceeded()) {
  682. GENERAL[n]->ROI[roi]->isReject = true;
  683. GENERAL[n]->ROI[roi]->result_float = -1;
  684. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "TensorFlow Lite inference failed");
  685. continue;
  686. }
  687. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "After Invoke");
  688. f1 = tflite->GetOutputValue(0);
  689. f2 = tflite->GetOutputValue(1);
  690. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  691. if(GENERAL[n]->ROI[roi]->CCW) {
  692. GENERAL[n]->ROI[roi]->result_float = 10 - (result * 10);
  693. }
  694. else {
  695. GENERAL[n]->ROI[roi]->result_float = result * 10;
  696. }
  697. ESP_LOGD(TAG, "General result (Analog)%i - CCW: %d - %f", roi, GENERAL[n]->ROI[roi]->CCW, GENERAL[n]->ROI[roi]->result_float);
  698. if (isLogImage) {
  699. LogImage(logPath, GENERAL[n]->ROI[roi]->name, &GENERAL[n]->ROI[roi]->result_float, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  700. }
  701. } break;
  702. case Digit:
  703. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Digit");
  704. {
  705. GENERAL[n]->ROI[roi]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[n]->ROI[roi]->image);
  706. if (GENERAL[n]->ROI[roi]->result_klasse < 0) {
  707. GENERAL[n]->ROI[roi]->isReject = true;
  708. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Digit inference failed for ROI " + GENERAL[n]->ROI[roi]->name);
  709. continue;
  710. }
  711. ESP_LOGD(TAG, "General result (Digit)%i: %d", roi, GENERAL[n]->ROI[roi]->result_klasse);
  712. if (isLogImage) {
  713. string _imagename = GENERAL[n]->name + "_" + GENERAL[n]->ROI[roi]->name;
  714. if (isLogImageSelect) {
  715. if (LogImageSelect.find(GENERAL[n]->ROI[roi]->name) != std::string::npos) {
  716. LogImage(logPath, _imagename, NULL, &GENERAL[n]->ROI[roi]->result_klasse, time, GENERAL[n]->ROI[roi]->image_org);
  717. }
  718. }
  719. else {
  720. LogImage(logPath, _imagename, NULL, &GENERAL[n]->ROI[roi]->result_klasse, time, GENERAL[n]->ROI[roi]->image_org);
  721. }
  722. }
  723. } break;
  724. case DoubleHyprid10:
  725. {
  726. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: DoubleHyprid10");
  727. int _num, _numplus, _numminus;
  728. float _val, _valplus, _valminus;
  729. float _fit;
  730. float _result_save_file;
  731. if (!tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image)) {
  732. GENERAL[n]->ROI[roi]->isReject = true;
  733. GENERAL[n]->ROI[roi]->result_float = -1;
  734. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CNN input image could not be copied into Tensor Arena");
  735. continue;
  736. }
  737. tflite->Invoke();
  738. if (!tflite->LastInvokeSucceeded()) {
  739. GENERAL[n]->ROI[roi]->isReject = true;
  740. GENERAL[n]->ROI[roi]->result_float = -1;
  741. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "TensorFlow Lite inference failed");
  742. continue;
  743. }
  744. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "After Invoke");
  745. _num = tflite->GetOutClassification(0, 9);
  746. _numplus = (_num + 1) % 10;
  747. _numminus = (_num - 1 + 10) % 10;
  748. _val = tflite->GetOutputValue(_num);
  749. _valplus = tflite->GetOutputValue(_numplus);
  750. _valminus = tflite->GetOutputValue(_numminus);
  751. float result = _num;
  752. if (_valplus > _valminus) {
  753. result = result + _valplus / (_valplus + _val);
  754. _fit = _val + _valplus;
  755. }
  756. else {
  757. result = result - _valminus / (_val + _valminus);
  758. _fit = _val + _valminus;
  759. }
  760. if (result >= 10) {
  761. result = result - 10;
  762. }
  763. if (result < 0) {
  764. result = result + 10;
  765. }
  766. string zw = "_num (p, m): " + to_string(_num) + " " + to_string(_numplus) + " " + to_string(_numminus);
  767. zw = zw + " _val (p, m): " + to_string(_val) + " " + to_string(_valplus) + " " + to_string(_valminus);
  768. zw = zw + " result: " + to_string(result) + " _fit: " + to_string(_fit);
  769. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw);
  770. _result_save_file = result;
  771. if (_fit < CNNGoodThreshold) {
  772. GENERAL[n]->ROI[roi]->isReject = true;
  773. result = -1;
  774. _result_save_file+= 100; // In case fit is not sufficient, the result should still be saved with "-10x.y".
  775. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + ", Threshold: " + to_string(CNNGoodThreshold) + ")";
  776. LogFile.WriteToFile(ESP_LOG_WARN, TAG, zw);
  777. }
  778. else {
  779. GENERAL[n]->ROI[roi]->isReject = false;
  780. }
  781. GENERAL[n]->ROI[roi]->result_float = result;
  782. ESP_LOGD(TAG, "Result General(Analog)%i: %f", roi, GENERAL[n]->ROI[roi]->result_float);
  783. if (isLogImage) {
  784. string _imagename = GENERAL[n]->name + "_" + GENERAL[n]->ROI[roi]->name;
  785. if (isLogImageSelect) {
  786. if (LogImageSelect.find(GENERAL[n]->ROI[roi]->name) != std::string::npos) {
  787. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  788. }
  789. }
  790. else {
  791. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  792. }
  793. }
  794. } break;
  795. case Digit100:
  796. case Analogue100:
  797. {
  798. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Digit100 or Analogue100");
  799. int _num;
  800. float _result_save_file;
  801. if (!tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image)) {
  802. GENERAL[n]->ROI[roi]->isReject = true;
  803. GENERAL[n]->ROI[roi]->result_float = -1;
  804. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CNN input image could not be copied into Tensor Arena");
  805. continue;
  806. }
  807. tflite->Invoke();
  808. if (!tflite->LastInvokeSucceeded()) {
  809. GENERAL[n]->ROI[roi]->isReject = true;
  810. GENERAL[n]->ROI[roi]->result_float = -1;
  811. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "TensorFlow Lite inference failed");
  812. continue;
  813. }
  814. _num = tflite->GetOutClassification();
  815. if(GENERAL[n]->ROI[roi]->CCW) {
  816. GENERAL[n]->ROI[roi]->result_float = 10 - ((float)_num / 10.0);
  817. }
  818. else {
  819. GENERAL[n]->ROI[roi]->result_float = (float)_num / 10.0;
  820. }
  821. _result_save_file = GENERAL[n]->ROI[roi]->result_float;
  822. GENERAL[n]->ROI[roi]->isReject = false;
  823. ESP_LOGD(TAG, "Result General(Analog)%i - CCW: %d - %f", roi, GENERAL[n]->ROI[roi]->CCW, GENERAL[n]->ROI[roi]->result_float);
  824. if (isLogImage) {
  825. string _imagename = GENERAL[n]->name + "_" + GENERAL[n]->ROI[roi]->name;
  826. if (isLogImageSelect) {
  827. if (LogImageSelect.find(GENERAL[n]->ROI[roi]->name) != std::string::npos) {
  828. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  829. }
  830. }
  831. else {
  832. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  833. }
  834. }
  835. } break;
  836. default:
  837. break;
  838. }
  839. }
  840. }
  841. delete tflite;
  842. return true;
  843. }
  844. bool ClassFlowCNNGeneral::isExtendedResolution(int _number) {
  845. if (CNNType == Digit) {
  846. return false;
  847. }
  848. return true;
  849. }
  850. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo() {
  851. std::vector<HTMLInfo*> result;
  852. for (int _ana = 0; _ana < GENERAL.size(); ++_ana) {
  853. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i) {
  854. ESP_LOGD(TAG, "Image: %d", (int) GENERAL[_ana]->ROI[i]->image);
  855. if (GENERAL[_ana]->ROI[i]->image) {
  856. if (GENERAL[_ana]->name == "default") {
  857. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  858. }
  859. else {
  860. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  861. }
  862. }
  863. HTMLInfo *zw = new (std::nothrow) HTMLInfo;
  864. if (zw == NULL) {
  865. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate ROI HTML metadata");
  866. continue;
  867. }
  868. if (GENERAL[_ana]->name == "default") {
  869. zw->filename = GENERAL[_ana]->ROI[i]->name + ".jpg";
  870. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  871. }
  872. else {
  873. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  874. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  875. }
  876. if (CNNType == Digit) {
  877. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  878. }
  879. else {
  880. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  881. }
  882. zw->image = GENERAL[_ana]->ROI[i]->image;
  883. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  884. result.push_back(zw);
  885. }
  886. }
  887. return result;
  888. }
  889. int ClassFlowCNNGeneral::getNumberGENERAL() {
  890. return GENERAL.size();
  891. }
  892. string ClassFlowCNNGeneral::getNameGENERAL(int _analog) {
  893. if (_analog < GENERAL.size()) {
  894. return GENERAL[_analog]->name;
  895. }
  896. return "GENERAL DOES NOT EXIST";
  897. }
  898. general* ClassFlowCNNGeneral::GetGENERAL(int _analog) {
  899. if (_analog < GENERAL.size()) {
  900. return GENERAL[_analog];
  901. }
  902. return NULL;
  903. }
  904. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers) {
  905. for (int _dig = 0; _dig < GENERAL.size(); _dig++) {
  906. std::string _name = GENERAL[_dig]->name;
  907. bool found = false;
  908. for (int i = 0; i < (*_name_numbers).size(); ++i) {
  909. if ((*_name_numbers)[i] == _name) {
  910. found = true;
  911. }
  912. }
  913. if (!found) {
  914. (*_name_numbers).push_back(_name);
  915. }
  916. }
  917. }
  918. string ClassFlowCNNGeneral::getReadoutRawString(int _analog)
  919. {
  920. string rt = "";
  921. if (_analog >= GENERAL.size() || GENERAL[_analog]==NULL || GENERAL[_analog]->ROI.size() == 0) {
  922. return rt;
  923. }
  924. for (int i = 0; i < GENERAL[_analog]->ROI.size(); ++i) {
  925. if (CNNType == Analogue || CNNType == Analogue100) {
  926. rt = rt + "," + RundeOutput(GENERAL[_analog]->ROI[i]->result_float, 1);
  927. }
  928. if (CNNType == Digit) {
  929. if (GENERAL[_analog]->ROI[i]->result_klasse >= 10) {
  930. rt = rt + ",N";
  931. }
  932. else {
  933. rt = rt + "," + RundeOutput(GENERAL[_analog]->ROI[i]->result_klasse, 0);
  934. }
  935. }
  936. if ((CNNType == DoubleHyprid10) || (CNNType == Digit100)) {
  937. rt = rt + "," + RundeOutput(GENERAL[_analog]->ROI[i]->result_float, 1);
  938. }
  939. }
  940. return rt;
  941. }