ClassFlowCNNGeneral.cpp 49 KB

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