ClassFlowCNNGeneral.cpp 44 KB

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