ClassFlowCNNGeneral.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  499. for (int _ana = 0; _ana < GENERAL.size(); ++_ana) {
  500. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i) {
  501. ESP_LOGD(TAG, "General %d - Align&Cut", i);
  502. caic->CutAndSave(GENERAL[_ana]->ROI[i]->posx, GENERAL[_ana]->ROI[i]->posy, GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, GENERAL[_ana]->ROI[i]->image_org);
  503. if (SaveAllFiles) {
  504. if (GENERAL[_ana]->name == "default") {
  505. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  506. }
  507. else {
  508. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  509. }
  510. }
  511. GENERAL[_ana]->ROI[i]->image_org->Resize(modelxsize, modelysize, GENERAL[_ana]->ROI[i]->image);
  512. if (SaveAllFiles) {
  513. if (GENERAL[_ana]->name == "default") {
  514. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  515. }
  516. else {
  517. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  518. }
  519. }
  520. }
  521. }
  522. return true;
  523. }
  524. void ClassFlowCNNGeneral::DrawROI(CImageBasis *_zw) {
  525. if (_zw->ImageOkay()) {
  526. if (CNNType == Analogue || CNNType == Analogue100) {
  527. int r = 0;
  528. int g = 255;
  529. int b = 0;
  530. for (int _ana = 0; _ana < GENERAL.size(); ++_ana) {
  531. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i) {
  532. _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);
  533. _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);
  534. _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);
  535. _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);
  536. }
  537. }
  538. }
  539. else {
  540. for (int _dig = 0; _dig < GENERAL.size(); ++_dig) {
  541. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i) {
  542. _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);
  543. }
  544. }
  545. }
  546. }
  547. }
  548. bool ClassFlowCNNGeneral::getNetworkParameter() {
  549. if (disabled) {
  550. return true;
  551. }
  552. CTfLiteClass *tflite = new (std::nothrow) CTfLiteClass;
  553. if (tflite == NULL) {
  554. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate CTfLiteClass object");
  555. LogFile.WriteHeapInfo("CTfLiteClass object allocation");
  556. return false;
  557. }
  558. string zwcnn = "/sdcard" + cnnmodelfile;
  559. zwcnn = FormatFileName(zwcnn);
  560. ESP_LOGD(TAG, "%s", zwcnn.c_str());
  561. if (!tflite->LoadModel(zwcnn)) {
  562. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't load tflite model " + cnnmodelfile + " -> Init aborted!");
  563. LogFile.WriteHeapInfo("getNetworkParameter-LoadModel");
  564. delete tflite;
  565. return false;
  566. }
  567. if (!tflite->MakeAllocate()) {
  568. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate tflite model -> Init aborted!");
  569. LogFile.WriteHeapInfo("getNetworkParameter-MakeAllocate");
  570. delete tflite;
  571. return false;
  572. }
  573. if (CNNType == AutoDetect) {
  574. tflite->GetInputDimension(false);
  575. modelxsize = tflite->ReadInputDimenstion(0);
  576. modelysize = tflite->ReadInputDimenstion(1);
  577. modelchannel = tflite->ReadInputDimenstion(2);
  578. int _anzoutputdimensions = tflite->GetAnzOutPut();
  579. switch (_anzoutputdimensions) {
  580. case 2:
  581. CNNType = Analogue;
  582. ESP_LOGD(TAG, "TFlite-Type set to Analogue");
  583. break;
  584. case 10:
  585. CNNType = DoubleHyprid10;
  586. ESP_LOGD(TAG, "TFlite-Type set to DoubleHyprid10");
  587. break;
  588. case 11:
  589. CNNType = Digit;
  590. ESP_LOGD(TAG, "TFlite-Type set to Digit");
  591. break;
  592. /* case 20:
  593. CNNType = DigitHyprid10;
  594. ESP_LOGD(TAG, "TFlite-Type set to DigitHyprid10");
  595. break;
  596. */
  597. // case 22:
  598. // CNNType = DigitHyprid;
  599. // ESP_LOGD(TAG, "TFlite-Type set to DigitHyprid");
  600. // break;
  601. case 100:
  602. if (modelxsize==32 && modelysize == 32) {
  603. CNNType = Analogue100;
  604. ESP_LOGD(TAG, "TFlite-Type set to Analogue100");
  605. }
  606. else {
  607. CNNType = Digit100;
  608. ESP_LOGD(TAG, "TFlite-Type set to Digit");
  609. }
  610. break;
  611. default:
  612. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "tflite does not fit the firmware (outout_dimension=" + std::to_string(_anzoutputdimensions) + ")");
  613. }
  614. }
  615. delete tflite;
  616. return true;
  617. }
  618. bool ClassFlowCNNGeneral::doNeuralNetwork(string time) {
  619. if (disabled) {
  620. return true;
  621. }
  622. string logPath = CreateLogFolder(time);
  623. CTfLiteClass *tflite = new (std::nothrow) CTfLiteClass;
  624. if (tflite == NULL) {
  625. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate CTfLiteClass object");
  626. LogFile.WriteHeapInfo("CTfLiteClass object allocation");
  627. return false;
  628. }
  629. string zwcnn = "/sdcard" + cnnmodelfile;
  630. zwcnn = FormatFileName(zwcnn);
  631. ESP_LOGD(TAG, "%s", zwcnn.c_str());
  632. if (!tflite->LoadModel(zwcnn)) {
  633. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't load tflite model " + cnnmodelfile + " -> Exec aborted this round!");
  634. LogFile.WriteHeapInfo("doNeuralNetwork-LoadModel");
  635. delete tflite;
  636. return false;
  637. }
  638. if (!tflite->MakeAllocate()) {
  639. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate tfilte model -> Exec aborted this round!");
  640. LogFile.WriteHeapInfo("doNeuralNetwork-MakeAllocate");
  641. delete tflite;
  642. return false;
  643. }
  644. // For each NUMBER
  645. for (int n = 0; n < GENERAL.size(); ++n) {
  646. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Processing Number '" + GENERAL[n]->name + "'");
  647. // For each ROI
  648. for (int roi = 0; roi < GENERAL[n]->ROI.size(); ++roi) {
  649. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "ROI #" + std::to_string(roi) + " - TfLite");
  650. //ESP_LOGD(TAG, "General %d - TfLite", i);
  651. switch (CNNType) {
  652. case Analogue:
  653. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Analogue");
  654. {
  655. float f1 = 0, f2 = 0;
  656. if (!tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image)) {
  657. GENERAL[n]->ROI[roi]->isReject = true;
  658. GENERAL[n]->ROI[roi]->result_float = -1;
  659. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CNN input image could not be copied into Tensor Arena");
  660. continue;
  661. }
  662. tflite->Invoke();
  663. if (!tflite->LastInvokeSucceeded()) {
  664. GENERAL[n]->ROI[roi]->isReject = true;
  665. GENERAL[n]->ROI[roi]->result_float = -1;
  666. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "TensorFlow Lite inference failed");
  667. continue;
  668. }
  669. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "After Invoke");
  670. f1 = tflite->GetOutputValue(0);
  671. f2 = tflite->GetOutputValue(1);
  672. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  673. if(GENERAL[n]->ROI[roi]->CCW) {
  674. GENERAL[n]->ROI[roi]->result_float = 10 - (result * 10);
  675. }
  676. else {
  677. GENERAL[n]->ROI[roi]->result_float = result * 10;
  678. }
  679. ESP_LOGD(TAG, "General result (Analog)%i - CCW: %d - %f", roi, GENERAL[n]->ROI[roi]->CCW, GENERAL[n]->ROI[roi]->result_float);
  680. if (isLogImage) {
  681. LogImage(logPath, GENERAL[n]->ROI[roi]->name, &GENERAL[n]->ROI[roi]->result_float, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  682. }
  683. } break;
  684. case Digit:
  685. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Digit");
  686. {
  687. GENERAL[n]->ROI[roi]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[n]->ROI[roi]->image);
  688. if (GENERAL[n]->ROI[roi]->result_klasse < 0) {
  689. GENERAL[n]->ROI[roi]->isReject = true;
  690. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Digit inference failed for ROI " + GENERAL[n]->ROI[roi]->name);
  691. continue;
  692. }
  693. ESP_LOGD(TAG, "General result (Digit)%i: %d", roi, GENERAL[n]->ROI[roi]->result_klasse);
  694. if (isLogImage) {
  695. string _imagename = GENERAL[n]->name + "_" + GENERAL[n]->ROI[roi]->name;
  696. if (isLogImageSelect) {
  697. if (LogImageSelect.find(GENERAL[n]->ROI[roi]->name) != std::string::npos) {
  698. LogImage(logPath, _imagename, NULL, &GENERAL[n]->ROI[roi]->result_klasse, time, GENERAL[n]->ROI[roi]->image_org);
  699. }
  700. }
  701. else {
  702. LogImage(logPath, _imagename, NULL, &GENERAL[n]->ROI[roi]->result_klasse, time, GENERAL[n]->ROI[roi]->image_org);
  703. }
  704. }
  705. } break;
  706. case DoubleHyprid10:
  707. {
  708. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: DoubleHyprid10");
  709. int _num, _numplus, _numminus;
  710. float _val, _valplus, _valminus;
  711. float _fit;
  712. float _result_save_file;
  713. if (!tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image)) {
  714. GENERAL[n]->ROI[roi]->isReject = true;
  715. GENERAL[n]->ROI[roi]->result_float = -1;
  716. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CNN input image could not be copied into Tensor Arena");
  717. continue;
  718. }
  719. tflite->Invoke();
  720. if (!tflite->LastInvokeSucceeded()) {
  721. GENERAL[n]->ROI[roi]->isReject = true;
  722. GENERAL[n]->ROI[roi]->result_float = -1;
  723. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "TensorFlow Lite inference failed");
  724. continue;
  725. }
  726. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "After Invoke");
  727. _num = tflite->GetOutClassification(0, 9);
  728. _numplus = (_num + 1) % 10;
  729. _numminus = (_num - 1 + 10) % 10;
  730. _val = tflite->GetOutputValue(_num);
  731. _valplus = tflite->GetOutputValue(_numplus);
  732. _valminus = tflite->GetOutputValue(_numminus);
  733. float result = _num;
  734. if (_valplus > _valminus) {
  735. result = result + _valplus / (_valplus + _val);
  736. _fit = _val + _valplus;
  737. }
  738. else {
  739. result = result - _valminus / (_val + _valminus);
  740. _fit = _val + _valminus;
  741. }
  742. if (result >= 10) {
  743. result = result - 10;
  744. }
  745. if (result < 0) {
  746. result = result + 10;
  747. }
  748. string zw = "_num (p, m): " + to_string(_num) + " " + to_string(_numplus) + " " + to_string(_numminus);
  749. zw = zw + " _val (p, m): " + to_string(_val) + " " + to_string(_valplus) + " " + to_string(_valminus);
  750. zw = zw + " result: " + to_string(result) + " _fit: " + to_string(_fit);
  751. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw);
  752. _result_save_file = result;
  753. if (_fit < CNNGoodThreshold) {
  754. GENERAL[n]->ROI[roi]->isReject = true;
  755. result = -1;
  756. _result_save_file+= 100; // In case fit is not sufficient, the result should still be saved with "-10x.y".
  757. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + ", Threshold: " + to_string(CNNGoodThreshold) + ")";
  758. LogFile.WriteToFile(ESP_LOG_WARN, TAG, zw);
  759. }
  760. else {
  761. GENERAL[n]->ROI[roi]->isReject = false;
  762. }
  763. GENERAL[n]->ROI[roi]->result_float = result;
  764. ESP_LOGD(TAG, "Result General(Analog)%i: %f", roi, GENERAL[n]->ROI[roi]->result_float);
  765. if (isLogImage) {
  766. string _imagename = GENERAL[n]->name + "_" + GENERAL[n]->ROI[roi]->name;
  767. if (isLogImageSelect) {
  768. if (LogImageSelect.find(GENERAL[n]->ROI[roi]->name) != std::string::npos) {
  769. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  770. }
  771. }
  772. else {
  773. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  774. }
  775. }
  776. } break;
  777. case Digit100:
  778. case Analogue100:
  779. {
  780. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Digit100 or Analogue100");
  781. int _num;
  782. float _result_save_file;
  783. if (!tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image)) {
  784. GENERAL[n]->ROI[roi]->isReject = true;
  785. GENERAL[n]->ROI[roi]->result_float = -1;
  786. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CNN input image could not be copied into Tensor Arena");
  787. continue;
  788. }
  789. tflite->Invoke();
  790. if (!tflite->LastInvokeSucceeded()) {
  791. GENERAL[n]->ROI[roi]->isReject = true;
  792. GENERAL[n]->ROI[roi]->result_float = -1;
  793. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "TensorFlow Lite inference failed");
  794. continue;
  795. }
  796. _num = tflite->GetOutClassification();
  797. if(GENERAL[n]->ROI[roi]->CCW) {
  798. GENERAL[n]->ROI[roi]->result_float = 10 - ((float)_num / 10.0);
  799. }
  800. else {
  801. GENERAL[n]->ROI[roi]->result_float = (float)_num / 10.0;
  802. }
  803. _result_save_file = GENERAL[n]->ROI[roi]->result_float;
  804. GENERAL[n]->ROI[roi]->isReject = false;
  805. ESP_LOGD(TAG, "Result General(Analog)%i - CCW: %d - %f", roi, GENERAL[n]->ROI[roi]->CCW, GENERAL[n]->ROI[roi]->result_float);
  806. if (isLogImage) {
  807. string _imagename = GENERAL[n]->name + "_" + GENERAL[n]->ROI[roi]->name;
  808. if (isLogImageSelect) {
  809. if (LogImageSelect.find(GENERAL[n]->ROI[roi]->name) != std::string::npos) {
  810. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  811. }
  812. }
  813. else {
  814. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[n]->ROI[roi]->image_org);
  815. }
  816. }
  817. } break;
  818. default:
  819. break;
  820. }
  821. }
  822. }
  823. delete tflite;
  824. return true;
  825. }
  826. bool ClassFlowCNNGeneral::isExtendedResolution(int _number) {
  827. if (CNNType == Digit) {
  828. return false;
  829. }
  830. return true;
  831. }
  832. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo() {
  833. std::vector<HTMLInfo*> result;
  834. for (int _ana = 0; _ana < GENERAL.size(); ++_ana) {
  835. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i) {
  836. ESP_LOGD(TAG, "Image: %d", (int) GENERAL[_ana]->ROI[i]->image);
  837. if (GENERAL[_ana]->ROI[i]->image) {
  838. if (GENERAL[_ana]->name == "default") {
  839. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  840. }
  841. else {
  842. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  843. }
  844. }
  845. HTMLInfo *zw = new HTMLInfo;
  846. if (GENERAL[_ana]->name == "default") {
  847. zw->filename = GENERAL[_ana]->ROI[i]->name + ".jpg";
  848. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  849. }
  850. else {
  851. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  852. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  853. }
  854. if (CNNType == Digit) {
  855. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  856. }
  857. else {
  858. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  859. }
  860. zw->image = GENERAL[_ana]->ROI[i]->image;
  861. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  862. result.push_back(zw);
  863. }
  864. }
  865. return result;
  866. }
  867. int ClassFlowCNNGeneral::getNumberGENERAL() {
  868. return GENERAL.size();
  869. }
  870. string ClassFlowCNNGeneral::getNameGENERAL(int _analog) {
  871. if (_analog < GENERAL.size()) {
  872. return GENERAL[_analog]->name;
  873. }
  874. return "GENERAL DOES NOT EXIST";
  875. }
  876. general* ClassFlowCNNGeneral::GetGENERAL(int _analog) {
  877. if (_analog < GENERAL.size()) {
  878. return GENERAL[_analog];
  879. }
  880. return NULL;
  881. }
  882. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers) {
  883. for (int _dig = 0; _dig < GENERAL.size(); _dig++) {
  884. std::string _name = GENERAL[_dig]->name;
  885. bool found = false;
  886. for (int i = 0; i < (*_name_numbers).size(); ++i) {
  887. if ((*_name_numbers)[i] == _name) {
  888. found = true;
  889. }
  890. }
  891. if (!found) {
  892. (*_name_numbers).push_back(_name);
  893. }
  894. }
  895. }
  896. string ClassFlowCNNGeneral::getReadoutRawString(int _analog)
  897. {
  898. string rt = "";
  899. if (_analog >= GENERAL.size() || GENERAL[_analog]==NULL || GENERAL[_analog]->ROI.size() == 0) {
  900. return rt;
  901. }
  902. for (int i = 0; i < GENERAL[_analog]->ROI.size(); ++i) {
  903. if (CNNType == Analogue || CNNType == Analogue100) {
  904. rt = rt + "," + RundeOutput(GENERAL[_analog]->ROI[i]->result_float, 1);
  905. }
  906. if (CNNType == Digit) {
  907. if (GENERAL[_analog]->ROI[i]->result_klasse >= 10) {
  908. rt = rt + ",N";
  909. }
  910. else {
  911. rt = rt + "," + RundeOutput(GENERAL[_analog]->ROI[i]->result_klasse, 0);
  912. }
  913. }
  914. if ((CNNType == DoubleHyprid10) || (CNNType == Digit100)) {
  915. rt = rt + "," + RundeOutput(GENERAL[_analog]->ROI[i]->result_float, 1);
  916. }
  917. }
  918. return rt;
  919. }