ClassFlowCNNGeneral.cpp 40 KB

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