ClassFlowCNNGeneral.cpp 39 KB

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