ClassFlowCNNGeneral.cpp 46 KB

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