ClassFlowCNNGeneral.cpp 49 KB

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