ClassFlowCNNGeneral.cpp 38 KB

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