ClassFlowCNNGeneral.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. static const char* TAG = "flow_analog";
  9. bool debugdetailgeneral = false;
  10. ClassFlowCNNGeneral::ClassFlowCNNGeneral(ClassFlowAlignment *_flowalign, t_CNNType _cnntype) : ClassFlowImage(NULL, TAG)
  11. {
  12. string cnnmodelfile = "";
  13. modelxsize = 1;
  14. modelysize = 1;
  15. CNNGoodThreshold = 0.0;
  16. ListFlowControll = NULL;
  17. previousElement = NULL;
  18. SaveAllFiles = false;
  19. disabled = false;
  20. isLogImageSelect = false;
  21. CNNType = AutoDetect;
  22. CNNType = _cnntype;
  23. flowpostalignment = _flowalign;
  24. }
  25. string ClassFlowCNNGeneral::getReadout(int _analog = 0, bool _extendedResolution, int prev)
  26. {
  27. string result = "";
  28. if (GENERAL[_analog]->ROI.size() == 0)
  29. return result;
  30. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout _analog=" + std::to_string(_analog) + ", _extendedResolution=" + std::to_string(_extendedResolution) + ", prev=" + std::to_string(prev));
  31. if (CNNType == Analogue || CNNType == Analogue100)
  32. {
  33. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  34. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  35. prev = ZeigerEval(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev);
  36. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout(analog) zahl=" + std::to_string(zahl) + ", ergebnis_nachkomma=" + std::to_string(ergebnis_nachkomma) + ", prev=" + std::to_string(prev));
  37. result = std::to_string(prev);
  38. if (_extendedResolution && (CNNType != Digital))
  39. result = result + std::to_string(ergebnis_nachkomma);
  40. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  41. {
  42. prev = ZeigerEval(GENERAL[_analog]->ROI[i]->result_float, prev);
  43. result = std::to_string(prev) + result;
  44. }
  45. return result;
  46. }
  47. if (CNNType == Digital)
  48. {
  49. for (int i = 0; i < GENERAL[_analog]->ROI.size(); ++i)
  50. {
  51. if (GENERAL[_analog]->ROI[i]->result_klasse >= 10)
  52. result = result + "N";
  53. else
  54. result = result + std::to_string(GENERAL[_analog]->ROI[i]->result_klasse);
  55. }
  56. return result;
  57. }
  58. if ((CNNType == DoubleHyprid10) || (CNNType == Digital100))
  59. {
  60. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  61. if (zahl >= 0) // NaN?
  62. {
  63. if (_extendedResolution) // ist nur gesetzt, falls es die erste Ziffer ist (kein Analog vorher!)
  64. {
  65. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  66. int ergebnis_vorkomma = ((int) floor(zahl)) % 10;
  67. result = std::to_string(ergebnis_vorkomma) + std::to_string(ergebnis_nachkomma);
  68. prev = ergebnis_vorkomma;
  69. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout(dig100-ext) ergebnis_vorkomma=" + std::to_string(ergebnis_vorkomma) + ", ergebnis_nachkomma=" + std::to_string(ergebnis_nachkomma) + ", prev=" + std::to_string(prev));
  70. }
  71. else
  72. {
  73. // prev = ZeigerEval(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev);
  74. prev = ZeigerEvalHybrid(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev, prev);
  75. result = std::to_string(prev);
  76. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout(dig100) prev=" + std::to_string(prev));
  77. }
  78. }
  79. else
  80. {
  81. result = "N";
  82. if (_extendedResolution && (CNNType != Digital))
  83. result = "NN";
  84. }
  85. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  86. {
  87. if (GENERAL[_analog]->ROI[i]->result_float >= 0)
  88. {
  89. prev = ZeigerEvalHybrid(GENERAL[_analog]->ROI[i]->result_float, GENERAL[_analog]->ROI[i+1]->result_float, prev);
  90. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout#ZeigerEvalHybrid()= " + std::to_string(prev));
  91. result = std::to_string(prev) + result;
  92. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout#result= " + result);
  93. }
  94. else
  95. {
  96. prev = -1;
  97. result = "N" + result;
  98. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout(result_float<0 /'N') result_float=" + std::to_string(GENERAL[_analog]->ROI[i]->result_float));
  99. }
  100. }
  101. return result;
  102. }
  103. /*
  104. if (CNNType == Digital100)
  105. {
  106. int zif_akt = -1;
  107. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  108. if (zahl >= 0) // NaN?
  109. {
  110. if (_extendedResolution)
  111. {
  112. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  113. int ergebnis_vorkomma = ((int) floor(zahl)) % 10;
  114. result = std::to_string(ergebnis_vorkomma) + std::to_string(ergebnis_nachkomma);
  115. zif_akt = ergebnis_vorkomma;
  116. }
  117. else
  118. {
  119. zif_akt = ZeigerEvalHybrid(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, -1, -1);
  120. result = std::to_string(zif_akt);
  121. }
  122. }
  123. else
  124. {
  125. result = "N";
  126. if (_extendedResolution && (CNNType != Digital))
  127. result = "NN";
  128. }
  129. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  130. {
  131. if (GENERAL[_analog]->ROI[i]->result_float >= 0)
  132. {
  133. zif_akt = ZeigerEvalHybrid(GENERAL[_analog]->ROI[i]->result_float, GENERAL[_analog]->ROI[i+1]->result_float, zif_akt);
  134. result = std::to_string(zif_akt) + result;
  135. }
  136. else
  137. {
  138. zif_akt = -1;
  139. result = "N" + result;
  140. }
  141. }
  142. return result;
  143. }
  144. */
  145. return result;
  146. }
  147. int ClassFlowCNNGeneral::ZeigerEvalHybrid(float zahl, float zahl_vorgaenger, int eval_vorgaenger)
  148. {
  149. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::ZeigerEvalHybrid( " + std::to_string(zahl) + ", " + std::to_string(zahl_vorgaenger) + ", " + std::to_string(eval_vorgaenger) + ")");
  150. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  151. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  152. if (eval_vorgaenger < 0) // keine Vorzahl vorhanden !!! --> Runde die Zahl
  153. {
  154. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  155. return ((int) round(zahl) + 10) % 10;
  156. else
  157. return ((int) trunc(zahl) + 10) % 10;
  158. }
  159. // 9.0, da bei getReadout() prev als int übergeben wird (9 statt 9.5)
  160. // tritt bei der ersten ziffer von digit auf, wenn analog davor (2. Aufruf von getReadout)
  161. if ((zahl_vorgaenger >= 0.5 ) && (zahl_vorgaenger < 9.0))
  162. {
  163. // kein Ziffernwechsel, da Vorkomma weit genug weg ist (0+/-0.5) --> zahl wird gerundet
  164. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  165. return ((int) round(zahl) + 10) % 10;
  166. else
  167. return ((int) trunc(zahl) + 10) % 10;
  168. }
  169. else
  170. {
  171. if (eval_vorgaenger <= 1) // Nulldurchgang hat stattgefunden (!Bewertung über Prev_value und nicht Zahl!) --> hier aufrunden (2.8 --> 3, aber auch 3.1 --> 3)
  172. {
  173. if (ergebnis_nachkomma > 5)
  174. return (ergebnis_vorkomma + 1) % 10;
  175. else
  176. return ergebnis_vorkomma;
  177. }
  178. else // bleibt nur >= 9.5 --> noch kein Nulldurchgang --> 2.8 --> 2, und 3.1 --> 2
  179. {
  180. // hier auf 4 reduziert, da erst ab Vorgänder 9 anfängt umzustellen. Bei 9.5 Vorgänger kann die aktuelle
  181. // Zahl noch x.4 - x.5 sein.
  182. if (ergebnis_nachkomma >= 4)
  183. return ergebnis_vorkomma;
  184. else
  185. return (ergebnis_vorkomma - 1 + 10) % 10;
  186. }
  187. }
  188. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::ZeigerEvalHybrid(return -1) zahl=" + std::to_string(zahl)
  189. + ", zahl_vorgaenger=" + std::to_string(zahl_vorgaenger) + ", eval_vorgaenger=" + std::to_string(eval_vorgaenger));
  190. return -1;
  191. /*
  192. if (zahl_vorgaenger > 9.2) // Ziffernwechsel beginnt
  193. {
  194. if (eval_vorgaenger == 0) // Wechsel hat schon stattgefunden
  195. {
  196. return ((int) round(zahl) + 10) % 10; // Annahme, dass die neue Zahl schon in der Nähe des Ziels ist
  197. }
  198. else
  199. {
  200. if (zahl_vorgaenger <= 9.5) // Wechsel startet gerade, aber beginnt erst
  201. {
  202. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  203. return ((int) round(zahl) + 10) % 10;
  204. else
  205. return ((int) trunc(zahl) + 10) % 10;
  206. }
  207. else
  208. {
  209. return ((int) trunc(zahl) + 10) % 10; // Wechsel schon weiter fortgeschritten, d.h. über 2 als Nachkomma
  210. }
  211. }
  212. }
  213. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  214. return ((int) round(zahl) + 10) % 10;
  215. return ((int) trunc(zahl) + 10) % 10;
  216. */
  217. }
  218. int ClassFlowCNNGeneral::ZeigerEval(float zahl, int ziffer_vorgaenger)
  219. {
  220. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  221. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  222. int ergebnis;
  223. float ergebnis_rating;
  224. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::ZeigerEval erg_v=" + std::to_string(ergebnis_vorkomma) + ", erg_n=" + std::to_string(ergebnis_nachkomma) + ", ziff_v=" + std::to_string(ziffer_vorgaenger));
  225. if (ziffer_vorgaenger == -1)
  226. return ergebnis_vorkomma % 10;
  227. // Ist die aktuelle Stelle schon umgesprungen und die Vorstelle noch nicht?
  228. // Akt.: 2.1, Vorstelle = 0.9 => 1.9
  229. // Problem sind mehrere Rundungen
  230. // Bsp. zahl=4.5, Vorgänger= 9.6 (ziffer_vorgaenger=0)
  231. // Tritt nur auf bei Übergang von analog auf digit
  232. ergebnis_rating = ergebnis_nachkomma - ziffer_vorgaenger;
  233. if (ergebnis_nachkomma >= 5)
  234. ergebnis_rating-=5.1;
  235. else
  236. ergebnis_rating+=5;
  237. ergebnis = (int) round(zahl);
  238. if (ergebnis_rating < 0)
  239. ergebnis-=1;
  240. if (ergebnis == -1)
  241. ergebnis+=10;
  242. ergebnis = (ergebnis + 10) % 10;
  243. return ergebnis;
  244. }
  245. bool ClassFlowCNNGeneral::ReadParameter(FILE* pfile, string& aktparamgraph)
  246. {
  247. std::vector<string> zerlegt;
  248. aktparamgraph = trim(aktparamgraph);
  249. if (aktparamgraph.size() == 0)
  250. if (!this->GetNextParagraph(pfile, aktparamgraph))
  251. return false;
  252. if ((toUpper(aktparamgraph) != "[ANALOG]") && (toUpper(aktparamgraph) != ";[ANALOG]")
  253. && (toUpper(aktparamgraph) != "[DIGIT]") && (toUpper(aktparamgraph) != ";[DIGIT]")
  254. && (toUpper(aktparamgraph) != "[DIGITS]") && (toUpper(aktparamgraph) != ";[DIGITS]")
  255. ) // Paragraph passt nicht
  256. return false;
  257. if (aktparamgraph[0] == ';')
  258. {
  259. disabled = true;
  260. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  261. printf("[Analog/Digit] is disabled !!!\n");
  262. return true;
  263. }
  264. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  265. {
  266. zerlegt = this->ZerlegeZeile(aktparamgraph);
  267. if ((toUpper(zerlegt[0]) == "LOGIMAGELOCATION") && (zerlegt.size() > 1))
  268. {
  269. this->LogImageLocation = "/sdcard" + zerlegt[1];
  270. this->isLogImage = true;
  271. }
  272. if ((toUpper(zerlegt[0]) == "LOGIMAGESELECT") && (zerlegt.size() > 1))
  273. {
  274. LogImageSelect = zerlegt[1];
  275. isLogImageSelect = true;
  276. }
  277. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  278. {
  279. this->logfileRetentionInDays = std::stoi(zerlegt[1]);
  280. }
  281. // if ((toUpper(zerlegt[0]) == "MODELTYPE") && (zerlegt.size() > 1))
  282. // {
  283. // if (toUpper(zerlegt[1]) == "DIGITHYPRID")
  284. // CNNType = DigitalHyprid;
  285. // }
  286. if ((toUpper(zerlegt[0]) == "MODEL") && (zerlegt.size() > 1))
  287. {
  288. this->cnnmodelfile = zerlegt[1];
  289. }
  290. if ((toUpper(zerlegt[0]) == "CNNGOODTHRESHOLD") && (zerlegt.size() > 1))
  291. {
  292. CNNGoodThreshold = std::stof(zerlegt[1]);
  293. }
  294. if (zerlegt.size() >= 5)
  295. {
  296. general* _analog = GetGENERAL(zerlegt[0], true);
  297. roi* neuroi = _analog->ROI[_analog->ROI.size()-1];
  298. neuroi->posx = std::stoi(zerlegt[1]);
  299. neuroi->posy = std::stoi(zerlegt[2]);
  300. neuroi->deltax = std::stoi(zerlegt[3]);
  301. neuroi->deltay = std::stoi(zerlegt[4]);
  302. neuroi->result_float = -1;
  303. neuroi->image = NULL;
  304. neuroi->image_org = NULL;
  305. }
  306. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  307. {
  308. if (toUpper(zerlegt[1]) == "TRUE")
  309. SaveAllFiles = true;
  310. }
  311. }
  312. if (!getNetworkParameter())
  313. return false;
  314. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  315. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  316. {
  317. GENERAL[_ana]->ROI[i]->image = new CImageBasis(modelxsize, modelysize, modelchannel);
  318. GENERAL[_ana]->ROI[i]->image_org = new CImageBasis(GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, 3);
  319. }
  320. return true;
  321. }
  322. general* ClassFlowCNNGeneral::FindGENERAL(string _name_number)
  323. {
  324. for (int i = 0; i < GENERAL.size(); ++i)
  325. if (GENERAL[i]->name == _name_number)
  326. return GENERAL[i];
  327. return NULL;
  328. }
  329. general* ClassFlowCNNGeneral::GetGENERAL(string _name, bool _create = true)
  330. {
  331. string _analog, _roi;
  332. int _pospunkt = _name.find_first_of(".");
  333. if (_pospunkt > -1)
  334. {
  335. _analog = _name.substr(0, _pospunkt);
  336. _roi = _name.substr(_pospunkt+1, _name.length() - _pospunkt - 1);
  337. }
  338. else
  339. {
  340. _analog = "default";
  341. _roi = _name;
  342. }
  343. general *_ret = NULL;
  344. for (int i = 0; i < GENERAL.size(); ++i)
  345. if (GENERAL[i]->name == _analog)
  346. _ret = GENERAL[i];
  347. if (!_create) // nicht gefunden und soll auch nicht erzeugt werden
  348. return _ret;
  349. if (_ret == NULL)
  350. {
  351. _ret = new general;
  352. _ret->name = _analog;
  353. GENERAL.push_back(_ret);
  354. }
  355. roi* neuroi = new roi;
  356. neuroi->name = _roi;
  357. _ret->ROI.push_back(neuroi);
  358. printf("GetGENERAL - GENERAL %s - roi %s\n", _analog.c_str(), _roi.c_str());
  359. return _ret;
  360. }
  361. string ClassFlowCNNGeneral::getHTMLSingleStep(string host)
  362. {
  363. string result, zw;
  364. std::vector<HTMLInfo*> htmlinfo;
  365. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  366. result = result + "Analog Pointers: <p> ";
  367. htmlinfo = GetHTMLInfo();
  368. for (int i = 0; i < htmlinfo.size(); ++i)
  369. {
  370. std::stringstream stream;
  371. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  372. zw = stream.str();
  373. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  374. delete htmlinfo[i];
  375. }
  376. htmlinfo.clear();
  377. return result;
  378. }
  379. bool ClassFlowCNNGeneral::doFlow(string time)
  380. {
  381. if (disabled)
  382. return true;
  383. if (!doAlignAndCut(time)){
  384. return false;
  385. };
  386. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::doFlow nach Alignment");
  387. doNeuralNetwork(time);
  388. RemoveOldLogs();
  389. return true;
  390. }
  391. bool ClassFlowCNNGeneral::doAlignAndCut(string time)
  392. {
  393. if (disabled)
  394. return true;
  395. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  396. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  397. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  398. {
  399. printf("General %d - Align&Cut\n", i);
  400. 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);
  401. if (SaveAllFiles)
  402. {
  403. if (GENERAL[_ana]->name == "default")
  404. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  405. else
  406. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  407. }
  408. GENERAL[_ana]->ROI[i]->image_org->Resize(modelxsize, modelysize, GENERAL[_ana]->ROI[i]->image);
  409. if (SaveAllFiles)
  410. {
  411. if (GENERAL[_ana]->name == "default")
  412. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  413. else
  414. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  415. }
  416. }
  417. return true;
  418. }
  419. void ClassFlowCNNGeneral::DrawROI(CImageBasis *_zw)
  420. {
  421. if (CNNType == Analogue || CNNType == Analogue100)
  422. {
  423. int r = 0;
  424. int g = 255;
  425. int b = 0;
  426. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  427. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  428. {
  429. _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);
  430. _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);
  431. _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);
  432. _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);
  433. }
  434. }
  435. else
  436. {
  437. for (int _dig = 0; _dig < GENERAL.size(); ++_dig)
  438. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i)
  439. _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);
  440. }
  441. }
  442. bool ClassFlowCNNGeneral::getNetworkParameter()
  443. {
  444. if (disabled)
  445. return true;
  446. CTfLiteClass *tflite = new CTfLiteClass;
  447. string zwcnn = "/sdcard" + cnnmodelfile;
  448. zwcnn = FormatFileName(zwcnn);
  449. printf(zwcnn.c_str());printf("\n");
  450. if (!tflite->LoadModel(zwcnn)) {
  451. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  452. LogFile.WriteToFile("Cannot load model");
  453. delete tflite;
  454. return false;
  455. }
  456. tflite->MakeAllocate();
  457. if (CNNType == AutoDetect)
  458. {
  459. tflite->GetInputDimension(false);
  460. modelxsize = tflite->ReadInputDimenstion(0);
  461. modelysize = tflite->ReadInputDimenstion(1);
  462. modelchannel = tflite->ReadInputDimenstion(2);
  463. int _anzoutputdimensions = tflite->GetAnzOutPut();
  464. switch (_anzoutputdimensions)
  465. {
  466. case 2:
  467. CNNType = Analogue;
  468. printf("TFlite-Type set to Analogue\n");
  469. break;
  470. case 10:
  471. CNNType = DoubleHyprid10;
  472. printf("TFlite-Type set to DoubleHyprid10\n");
  473. break;
  474. case 11:
  475. CNNType = Digital;
  476. printf("TFlite-Type set to Digital\n");
  477. break;
  478. case 20:
  479. CNNType = DigitalHyprid10;
  480. printf("TFlite-Type set to DigitalHyprid10\n");
  481. break;
  482. // case 22:
  483. // CNNType = DigitalHyprid;
  484. // printf("TFlite-Type set to DigitalHyprid\n");
  485. // break;
  486. case 100:
  487. if (modelxsize==32 && modelysize == 32) {
  488. CNNType = Analogue100;
  489. printf("TFlite-Type set to Analogue100\n");
  490. } else {
  491. CNNType = Digital100;
  492. printf("TFlite-Type set to Digital\n");
  493. }
  494. break;
  495. default:
  496. LogFile.WriteToFile("ERROR ERROR ERROR - tflite passt nicht zur Firmware - ERROR ERROR ERROR (outout_dimension=" + std::to_string(_anzoutputdimensions) + ")");
  497. printf("ERROR ERROR ERROR - tflite passt nicht zur Firmware - ERROR ERROR ERROR\n");
  498. }
  499. }
  500. delete tflite;
  501. return true;
  502. }
  503. bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
  504. {
  505. if (disabled)
  506. return true;
  507. string logPath = CreateLogFolder(time);
  508. CTfLiteClass *tflite = new CTfLiteClass;
  509. string zwcnn = "/sdcard" + cnnmodelfile;
  510. zwcnn = FormatFileName(zwcnn);
  511. printf(zwcnn.c_str());printf("\n");
  512. if (!tflite->LoadModel(zwcnn)) {
  513. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  514. LogFile.WriteToFile("Cannot load model");
  515. delete tflite;
  516. return false;
  517. }
  518. tflite->MakeAllocate();
  519. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  520. {
  521. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  522. {
  523. printf("General %d - TfLite\n", i);
  524. switch (CNNType) {
  525. case Analogue:
  526. {
  527. float f1, f2;
  528. f1 = 0; f2 = 0;
  529. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  530. tflite->Invoke();
  531. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  532. f1 = tflite->GetOutputValue(0);
  533. f2 = tflite->GetOutputValue(1);
  534. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  535. GENERAL[_ana]->ROI[i]->result_float = result * 10;
  536. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  537. if (isLogImage)
  538. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  539. } break;
  540. case Digital:
  541. {
  542. GENERAL[_ana]->ROI[i]->result_klasse = 0;
  543. GENERAL[_ana]->ROI[i]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[_ana]->ROI[i]->image);
  544. printf("Result General(Digit)%i: %d\n", i, GENERAL[_ana]->ROI[i]->result_klasse);
  545. if (isLogImage)
  546. {
  547. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  548. if (isLogImageSelect)
  549. {
  550. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  551. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  552. }
  553. else
  554. {
  555. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  556. }
  557. }
  558. } break;
  559. /*
  560. case DigitalHyprid:
  561. {
  562. int _num, _nachkomma;
  563. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  564. tflite->Invoke();
  565. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  566. _num = tflite->GetOutClassification(0, 10);
  567. _nachkomma = tflite->GetOutClassification(11, 21);
  568. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  569. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  570. if ((_num == 10) || (_nachkomma == 10)) // NaN detektiert
  571. GENERAL[_ana]->ROI[i]->result_float = -1;
  572. else
  573. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  574. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  575. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  576. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  577. if (isLogImage)
  578. {
  579. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  580. if (isLogImageSelect)
  581. {
  582. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  583. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  584. }
  585. else
  586. {
  587. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  588. }
  589. }
  590. } break;
  591. */
  592. case DigitalHyprid10:
  593. {
  594. int _num, _nachkomma;
  595. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  596. tflite->Invoke();
  597. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  598. _num = tflite->GetOutClassification(0, 9);
  599. _nachkomma = tflite->GetOutClassification(10, 19);
  600. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  601. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  602. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  603. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  604. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  605. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  606. if (isLogImage)
  607. {
  608. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  609. if (isLogImageSelect)
  610. {
  611. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  612. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  613. }
  614. else
  615. {
  616. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  617. }
  618. }
  619. } break;
  620. case DoubleHyprid10:
  621. {
  622. int _num, _numplus, _numminus;
  623. float _val, _valplus, _valminus;
  624. float _fit;
  625. float _result_save_file;
  626. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  627. tflite->Invoke();
  628. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  629. _num = tflite->GetOutClassification(0, 9);
  630. _numplus = (_num + 1) % 10;
  631. _numminus = (_num - 1 + 10) % 10;
  632. _val = tflite->GetOutputValue(_num);
  633. _valplus = tflite->GetOutputValue(_numplus);
  634. _valminus = tflite->GetOutputValue(_numminus);
  635. float result = _num;
  636. if (_valplus > _valminus)
  637. {
  638. result = result + _valplus / (_valplus + _val);
  639. _fit = _val + _valplus;
  640. }
  641. else
  642. {
  643. result = result - _valminus / (_val + _valminus);
  644. _fit = _val + _valminus;
  645. }
  646. if (result > 10)
  647. result = result - 10;
  648. if (result < 0)
  649. result = result + 10;
  650. string zw = "_num (p, m): " + to_string(_num) + " " + to_string(_numplus) + " " + to_string(_numminus);
  651. zw = zw + " _val (p, m): " + to_string(_val) + " " + to_string(_valplus) + " " + to_string(_valminus);
  652. zw = zw + " result: " + to_string(result) + " _fit: " + to_string(_fit);
  653. printf("details cnn: %s\n", zw.c_str());
  654. LogFile.WriteToFile(zw);
  655. _result_save_file = result;
  656. if (_fit < CNNGoodThreshold)
  657. {
  658. GENERAL[_ana]->ROI[i]->isReject = true;
  659. result = -1;
  660. _result_save_file+= 100; // Für den Fall, dass fit nicht ausreichend, soll trotzdem das Ergebnis mit "-10x.y" abgespeichert werden.
  661. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold);
  662. printf("Value Rejected due to Threshold (Fit: %f, Threshold: %f\n", _fit, CNNGoodThreshold);
  663. LogFile.WriteToFile(zw);
  664. }
  665. else
  666. {
  667. GENERAL[_ana]->ROI[i]->isReject = false;
  668. }
  669. GENERAL[_ana]->ROI[i]->result_float = result;
  670. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  671. if (isLogImage)
  672. {
  673. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  674. if (isLogImageSelect)
  675. {
  676. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  677. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  678. }
  679. else
  680. {
  681. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  682. }
  683. }
  684. }
  685. break;
  686. case Digital100:
  687. case Analogue100:
  688. {
  689. int _num;
  690. float _result_save_file;
  691. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  692. tflite->Invoke();
  693. _num = tflite->GetOutClassification();
  694. GENERAL[_ana]->ROI[i]->result_float = (float)_num / 10.0;
  695. _result_save_file = GENERAL[_ana]->ROI[i]->result_float;
  696. GENERAL[_ana]->ROI[i]->isReject = false;
  697. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  698. if (isLogImage)
  699. {
  700. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  701. if (isLogImageSelect)
  702. {
  703. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  704. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  705. }
  706. else
  707. {
  708. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  709. }
  710. }
  711. } break;
  712. default:
  713. break;
  714. }
  715. }
  716. }
  717. delete tflite;
  718. return true;
  719. }
  720. bool ClassFlowCNNGeneral::isExtendedResolution(int _number)
  721. {
  722. if (!(CNNType == Digital))
  723. return true;
  724. return false;
  725. }
  726. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo()
  727. {
  728. std::vector<HTMLInfo*> result;
  729. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  730. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  731. {
  732. printf("Image: %d\n", (int) GENERAL[_ana]->ROI[i]->image);
  733. if (GENERAL[_ana]->ROI[i]->image)
  734. {
  735. if (GENERAL[_ana]->name == "default")
  736. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  737. else
  738. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  739. }
  740. HTMLInfo *zw = new HTMLInfo;
  741. if (GENERAL[_ana]->name == "default")
  742. {
  743. zw->filename = GENERAL[_ana]->ROI[i]->name + ".bmp";
  744. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  745. }
  746. else
  747. {
  748. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp";
  749. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  750. }
  751. if (CNNType == Digital)
  752. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  753. else
  754. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  755. zw->image = GENERAL[_ana]->ROI[i]->image;
  756. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  757. result.push_back(zw);
  758. }
  759. return result;
  760. }
  761. int ClassFlowCNNGeneral::getAnzahlGENERAL()
  762. {
  763. return GENERAL.size();
  764. }
  765. string ClassFlowCNNGeneral::getNameGENERAL(int _analog)
  766. {
  767. if (_analog < GENERAL.size())
  768. return GENERAL[_analog]->name;
  769. return "GENERAL DOES NOT EXIST";
  770. }
  771. general* ClassFlowCNNGeneral::GetGENERAL(int _analog)
  772. {
  773. if (_analog < GENERAL.size())
  774. return GENERAL[_analog];
  775. return NULL;
  776. }
  777. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers)
  778. {
  779. for (int _dig = 0; _dig < GENERAL.size(); _dig++)
  780. {
  781. std::string _name = GENERAL[_dig]->name;
  782. bool found = false;
  783. for (int i = 0; i < (*_name_numbers).size(); ++i)
  784. {
  785. if ((*_name_numbers)[i] == _name)
  786. found = true;
  787. }
  788. if (!found)
  789. (*_name_numbers).push_back(_name);
  790. }
  791. }