ClassFlowCNNGeneral.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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.9;
  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 (CNNType == Analogue)
  31. {
  32. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  33. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  34. prev = ZeigerEval(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev);
  35. result = std::to_string(prev);
  36. if (_extendedResolution && (CNNType != Digital))
  37. result = result + std::to_string(ergebnis_nachkomma);
  38. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  39. {
  40. prev = ZeigerEval(GENERAL[_analog]->ROI[i]->result_float, prev);
  41. result = std::to_string(prev) + result;
  42. }
  43. return result;
  44. }
  45. if (CNNType == Digital)
  46. {
  47. for (int i = 0; i < GENERAL[_analog]->ROI.size(); ++i)
  48. {
  49. if (GENERAL[_analog]->ROI[i]->result_klasse >= 10)
  50. result = result + "N";
  51. else
  52. result = result + std::to_string(GENERAL[_analog]->ROI[i]->result_klasse);
  53. }
  54. return result;
  55. }
  56. if ((CNNType == DoubleHyprid10))
  57. {
  58. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  59. if (zahl >= 0) // NaN?
  60. {
  61. if (_extendedResolution)
  62. {
  63. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  64. int ergebnis_vorkomma = ((int) floor(zahl)) % 10;
  65. result = std::to_string(ergebnis_vorkomma) + std::to_string(ergebnis_nachkomma);
  66. prev = ergebnis_vorkomma;
  67. }
  68. else
  69. {
  70. prev = ZeigerEval(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev);
  71. // prev = ZeigerEvalHybrid(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev, prev);
  72. result = std::to_string(prev);
  73. }
  74. }
  75. else
  76. {
  77. result = "N";
  78. if (_extendedResolution && (CNNType != Digital))
  79. result = "NN";
  80. }
  81. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  82. {
  83. if (GENERAL[_analog]->ROI[i]->result_float >= 0)
  84. {
  85. prev = ZeigerEvalHybrid(GENERAL[_analog]->ROI[i]->result_float, GENERAL[_analog]->ROI[i+1]->result_float, prev);
  86. result = std::to_string(prev) + result;
  87. }
  88. else
  89. {
  90. prev = -1;
  91. result = "N" + result;
  92. }
  93. }
  94. return result;
  95. }
  96. if ((CNNType == DigitalHyprid))
  97. {
  98. int zif_akt = -1;
  99. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  100. if (zahl >= 0) // NaN?
  101. {
  102. if (_extendedResolution)
  103. {
  104. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  105. int ergebnis_vorkomma = ((int) floor(zahl)) % 10;
  106. result = std::to_string(ergebnis_vorkomma) + std::to_string(ergebnis_nachkomma);
  107. zif_akt = ergebnis_vorkomma;
  108. }
  109. else
  110. {
  111. zif_akt = ZeigerEvalHybrid(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, -1, -1);
  112. result = std::to_string(zif_akt);
  113. }
  114. }
  115. else
  116. {
  117. result = "N";
  118. if (_extendedResolution && (CNNType != Digital))
  119. result = "NN";
  120. }
  121. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  122. {
  123. if (GENERAL[_analog]->ROI[i]->result_float >= 0)
  124. {
  125. zif_akt = ZeigerEvalHybrid(GENERAL[_analog]->ROI[i]->result_float, GENERAL[_analog]->ROI[i+1]->result_float, zif_akt);
  126. result = std::to_string(zif_akt) + result;
  127. }
  128. else
  129. {
  130. zif_akt = -1;
  131. result = "N" + result;
  132. }
  133. }
  134. return result;
  135. }
  136. return result;
  137. }
  138. int ClassFlowCNNGeneral::ZeigerEvalHybrid(float zahl, float zahl_vorgaenger, int eval_vorgaenger)
  139. {
  140. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  141. if (zahl_vorgaenger < 0) // keine Vorzahl vorhanden !!! --> Runde die Zahl
  142. {
  143. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  144. return ((int) round(zahl) + 10) % 10;
  145. else
  146. return ((int) trunc(zahl) + 10) % 10;
  147. }
  148. if (zahl_vorgaenger > 9.2) // Ziffernwechsel beginnt
  149. {
  150. if (eval_vorgaenger == 0) // Wechsel hat schon stattgefunden
  151. {
  152. return ((int) round(zahl) + 10) % 10; // Annahme, dass die neue Zahl schon in der Nähe des Ziels ist
  153. }
  154. else
  155. {
  156. if (zahl_vorgaenger <= 9.5) // Wechsel startet gerade, aber beginnt erst
  157. {
  158. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  159. return ((int) round(zahl) + 10) % 10;
  160. else
  161. return ((int) trunc(zahl) + 10) % 10;
  162. }
  163. else
  164. {
  165. return ((int) trunc(zahl) + 10) % 10; // Wechsel schon weiter fortgeschritten, d.h. über 2 als Nachkomma
  166. }
  167. }
  168. }
  169. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  170. return ((int) round(zahl) + 10) % 10;
  171. return ((int) trunc(zahl) + 10) % 10;
  172. }
  173. /*
  174. int ClassFlowCNNGeneral::ZeigerEvalHybrid_NEU(float zahl, float zahl_vorgaenger)
  175. {
  176. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  177. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  178. int ergebnis, ergebnis_rating;
  179. if (zahl_vorgaenger < 0)
  180. return ergebnis_vorkomma % 10;
  181. ergebnis_rating = ergebnis_nachkomma - zahl_vorgaenger;
  182. if (ergebnis_nachkomma >= 5)
  183. ergebnis_rating-=5;
  184. else
  185. ergebnis_rating+=5;
  186. ergebnis = (int) round(zahl);
  187. if (ergebnis_rating < 0)
  188. ergebnis-=1;
  189. if (ergebnis == -1)
  190. ergebnis+=10;
  191. ergebnis = (ergebnis + 10) % 10;
  192. return ergebnis;
  193. }
  194. */
  195. int ClassFlowCNNGeneral::ZeigerEval(float zahl, int ziffer_vorgaenger)
  196. {
  197. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  198. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  199. int ergebnis, ergebnis_rating;
  200. if (ziffer_vorgaenger == -1)
  201. return ergebnis_vorkomma % 10;
  202. ergebnis_rating = ergebnis_nachkomma - ziffer_vorgaenger;
  203. if (ergebnis_nachkomma >= 5)
  204. ergebnis_rating-=5;
  205. else
  206. ergebnis_rating+=5;
  207. ergebnis = (int) round(zahl);
  208. if (ergebnis_rating < 0)
  209. ergebnis-=1;
  210. if (ergebnis == -1)
  211. ergebnis+=10;
  212. ergebnis = (ergebnis + 10) % 10;
  213. return ergebnis;
  214. }
  215. bool ClassFlowCNNGeneral::ReadParameter(FILE* pfile, string& aktparamgraph)
  216. {
  217. std::vector<string> zerlegt;
  218. aktparamgraph = trim(aktparamgraph);
  219. if (aktparamgraph.size() == 0)
  220. if (!this->GetNextParagraph(pfile, aktparamgraph))
  221. return false;
  222. if ((toUpper(aktparamgraph) != "[ANALOG]") && (toUpper(aktparamgraph) != ";[ANALOG]")
  223. && (toUpper(aktparamgraph) != "[DIGIT]") && (toUpper(aktparamgraph) != ";[DIGIT]")
  224. && (toUpper(aktparamgraph) != "[DIGITS]") && (toUpper(aktparamgraph) != ";[DIGITS]")
  225. ) // Paragraph passt nicht
  226. return false;
  227. if (aktparamgraph[0] == ';')
  228. {
  229. disabled = true;
  230. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  231. printf("[Analog/Digit] is disabled !!!\n");
  232. return true;
  233. }
  234. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  235. {
  236. zerlegt = this->ZerlegeZeile(aktparamgraph);
  237. if ((toUpper(zerlegt[0]) == "LOGIMAGELOCATION") && (zerlegt.size() > 1))
  238. {
  239. this->LogImageLocation = "/sdcard" + zerlegt[1];
  240. this->isLogImage = true;
  241. }
  242. if ((toUpper(zerlegt[0]) == "LOGIMAGESELECT") && (zerlegt.size() > 1))
  243. {
  244. LogImageSelect = zerlegt[1];
  245. isLogImageSelect = true;
  246. }
  247. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  248. {
  249. this->logfileRetentionInDays = std::stoi(zerlegt[1]);
  250. }
  251. if ((toUpper(zerlegt[0]) == "MODELTYPE") && (zerlegt.size() > 1))
  252. {
  253. if (toUpper(zerlegt[1]) == "DIGITHYPRID")
  254. CNNType = DigitalHyprid;
  255. }
  256. if ((toUpper(zerlegt[0]) == "MODEL") && (zerlegt.size() > 1))
  257. {
  258. this->cnnmodelfile = zerlegt[1];
  259. }
  260. if ((toUpper(zerlegt[0]) == "CNNGOODTHRESHOLD") && (zerlegt.size() > 1))
  261. {
  262. CNNGoodThreshold = std::stof(zerlegt[1]);
  263. }
  264. if ((toUpper(zerlegt[0]) == "MODELINPUTSIZE") && (zerlegt.size() > 2))
  265. {
  266. this->modelxsize = std::stoi(zerlegt[1]);
  267. this->modelysize = std::stoi(zerlegt[2]);
  268. }
  269. if (zerlegt.size() >= 5)
  270. {
  271. general* _analog = GetGENERAL(zerlegt[0], true);
  272. roi* neuroi = _analog->ROI[_analog->ROI.size()-1];
  273. neuroi->posx = std::stoi(zerlegt[1]);
  274. neuroi->posy = std::stoi(zerlegt[2]);
  275. neuroi->deltax = std::stoi(zerlegt[3]);
  276. neuroi->deltay = std::stoi(zerlegt[4]);
  277. neuroi->result_float = -1;
  278. neuroi->image = NULL;
  279. neuroi->image_org = NULL;
  280. }
  281. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  282. {
  283. if (toUpper(zerlegt[1]) == "TRUE")
  284. SaveAllFiles = true;
  285. }
  286. }
  287. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  288. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  289. {
  290. GENERAL[_ana]->ROI[i]->image = new CImageBasis(modelxsize, modelysize, 3);
  291. GENERAL[_ana]->ROI[i]->image_org = new CImageBasis(GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, 3);
  292. }
  293. return true;
  294. }
  295. general* ClassFlowCNNGeneral::FindGENERAL(string _name_number)
  296. {
  297. for (int i = 0; i < GENERAL.size(); ++i)
  298. if (GENERAL[i]->name == _name_number)
  299. return GENERAL[i];
  300. return NULL;
  301. }
  302. general* ClassFlowCNNGeneral::GetGENERAL(string _name, bool _create = true)
  303. {
  304. string _analog, _roi;
  305. int _pospunkt = _name.find_first_of(".");
  306. if (_pospunkt > -1)
  307. {
  308. _analog = _name.substr(0, _pospunkt);
  309. _roi = _name.substr(_pospunkt+1, _name.length() - _pospunkt - 1);
  310. }
  311. else
  312. {
  313. _analog = "default";
  314. _roi = _name;
  315. }
  316. general *_ret = NULL;
  317. for (int i = 0; i < GENERAL.size(); ++i)
  318. if (GENERAL[i]->name == _analog)
  319. _ret = GENERAL[i];
  320. if (!_create) // nicht gefunden und soll auch nicht erzeugt werden
  321. return _ret;
  322. if (_ret == NULL)
  323. {
  324. _ret = new general;
  325. _ret->name = _analog;
  326. GENERAL.push_back(_ret);
  327. }
  328. roi* neuroi = new roi;
  329. neuroi->name = _roi;
  330. _ret->ROI.push_back(neuroi);
  331. printf("GetGENERAL - GENERAL %s - roi %s\n", _analog.c_str(), _roi.c_str());
  332. return _ret;
  333. }
  334. string ClassFlowCNNGeneral::getHTMLSingleStep(string host)
  335. {
  336. string result, zw;
  337. std::vector<HTMLInfo*> htmlinfo;
  338. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  339. result = result + "Analog Pointers: <p> ";
  340. htmlinfo = GetHTMLInfo();
  341. for (int i = 0; i < htmlinfo.size(); ++i)
  342. {
  343. std::stringstream stream;
  344. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  345. zw = stream.str();
  346. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  347. delete htmlinfo[i];
  348. }
  349. htmlinfo.clear();
  350. return result;
  351. }
  352. bool ClassFlowCNNGeneral::doFlow(string time)
  353. {
  354. if (disabled)
  355. return true;
  356. if (!doAlignAndCut(time)){
  357. return false;
  358. };
  359. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::doFlow nach Alignment");
  360. doNeuralNetwork(time);
  361. RemoveOldLogs();
  362. return true;
  363. }
  364. bool ClassFlowCNNGeneral::doAlignAndCut(string time)
  365. {
  366. if (disabled)
  367. return true;
  368. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  369. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  370. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  371. {
  372. printf("General %d - Align&Cut\n", i);
  373. 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);
  374. if (SaveAllFiles)
  375. {
  376. if (GENERAL[_ana]->name == "default")
  377. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  378. else
  379. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  380. }
  381. GENERAL[_ana]->ROI[i]->image_org->Resize(modelxsize, modelysize, GENERAL[_ana]->ROI[i]->image);
  382. if (SaveAllFiles)
  383. {
  384. if (GENERAL[_ana]->name == "default")
  385. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  386. else
  387. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  388. }
  389. }
  390. return true;
  391. }
  392. void ClassFlowCNNGeneral::DrawROI(CImageBasis *_zw)
  393. {
  394. if (CNNType == Analogue)
  395. {
  396. int r = 0;
  397. int g = 255;
  398. int b = 0;
  399. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  400. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  401. {
  402. _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);
  403. _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);
  404. _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);
  405. _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);
  406. }
  407. }
  408. else
  409. {
  410. for (int _dig = 0; _dig < GENERAL.size(); ++_dig)
  411. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i)
  412. _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);
  413. }
  414. }
  415. bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
  416. {
  417. if (disabled)
  418. return true;
  419. string logPath = CreateLogFolder(time);
  420. CTfLiteClass *tflite = new CTfLiteClass;
  421. string zwcnn = "/sdcard" + cnnmodelfile;
  422. zwcnn = FormatFileName(zwcnn);
  423. printf(zwcnn.c_str());printf("\n");
  424. if (!tflite->LoadModel(zwcnn)) {
  425. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  426. LogFile.WriteToFile("Cannot load model");
  427. delete tflite;
  428. return false;
  429. }
  430. tflite->MakeAllocate();
  431. if (CNNType == AutoDetect)
  432. {
  433. int _anzoutputdimensions = tflite->GetAnzOutPut();
  434. switch (_anzoutputdimensions)
  435. {
  436. case 2:
  437. CNNType = Analogue;
  438. printf("TFlite-Type set to Analogue\n");
  439. break;
  440. case 10:
  441. CNNType = DoubleHyprid10;
  442. printf("TFlite-Type set to DoubleHyprid10\n");
  443. break;
  444. case 11:
  445. CNNType = Digital;
  446. printf("TFlite-Type set to Digital\n");
  447. break;
  448. case 20:
  449. CNNType = DigitalHyprid10;
  450. printf("TFlite-Type set to DigitalHyprid10\n");
  451. break;
  452. case 22:
  453. CNNType = DigitalHyprid;
  454. printf("TFlite-Type set to DigitalHyprid\n");
  455. break;
  456. default:
  457. printf("ERROR ERROR ERROR - tflite passt nicht zur Firmware - ERROR ERROR ERROR\n");
  458. }
  459. }
  460. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  461. {
  462. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  463. {
  464. printf("General %d - TfLite\n", i);
  465. switch (CNNType) {
  466. case Analogue:
  467. {
  468. float f1, f2;
  469. f1 = 0; f2 = 0;
  470. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  471. tflite->Invoke();
  472. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  473. f1 = tflite->GetOutputValue(0);
  474. f2 = tflite->GetOutputValue(1);
  475. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  476. GENERAL[_ana]->ROI[i]->result_float = result * 10;
  477. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  478. if (isLogImage)
  479. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  480. } break;
  481. case Digital:
  482. {
  483. GENERAL[_ana]->ROI[i]->result_klasse = 0;
  484. GENERAL[_ana]->ROI[i]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[_ana]->ROI[i]->image);
  485. printf("Result General(Digit)%i: %d\n", i, GENERAL[_ana]->ROI[i]->result_klasse);
  486. if (isLogImage)
  487. {
  488. if (isLogImageSelect)
  489. {
  490. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  491. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  492. }
  493. else
  494. {
  495. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  496. }
  497. }
  498. } break;
  499. case DigitalHyprid:
  500. {
  501. int _num, _nachkomma;
  502. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  503. tflite->Invoke();
  504. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  505. _num = tflite->GetOutClassification(0, 10);
  506. _nachkomma = tflite->GetOutClassification(11, 21);
  507. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  508. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  509. if ((_num == 10) || (_nachkomma == 10)) // NaN detektiert
  510. GENERAL[_ana]->ROI[i]->result_float = -1;
  511. else
  512. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  513. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  514. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  515. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  516. if (isLogImage)
  517. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  518. } break;
  519. case DigitalHyprid10:
  520. {
  521. int _num, _nachkomma;
  522. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  523. tflite->Invoke();
  524. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  525. _num = tflite->GetOutClassification(0, 9);
  526. _nachkomma = tflite->GetOutClassification(10, 19);
  527. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  528. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  529. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  530. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  531. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  532. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  533. if (isLogImage)
  534. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  535. } break;
  536. case DoubleHyprid10:
  537. {
  538. int _num, _numplus, _numminus;
  539. float _val, _valplus, _valminus;
  540. float _fit;
  541. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  542. tflite->Invoke();
  543. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  544. _num = tflite->GetOutClassification(0, 9);
  545. _numplus = (_num + 1) % 10;
  546. _numminus = (_num - 1) % 10;
  547. _val = tflite->GetOutputValue(_num);
  548. _valplus = tflite->GetOutputValue(_numplus);
  549. _valminus = tflite->GetOutputValue(_numminus);
  550. float result = _num;
  551. if (_valplus > _numminus)
  552. {
  553. result = result + _valplus / (_valplus + _val);
  554. _fit = _val + _valplus;
  555. }
  556. else
  557. {
  558. result = result - _valminus / (_val + _valminus);
  559. _fit = _val + _valminus;
  560. }
  561. if (result > 10)
  562. result = result - 10;
  563. if (result < 0)
  564. result = result + 10;
  565. if (_fit < CNNGoodThreshold)
  566. {
  567. GENERAL[_ana]->ROI[i]->isReject = true;
  568. result = -1;
  569. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold);
  570. printf("Value Rejected due to Threshold (Fit: %f, Threshold: %f\n", _fit, CNNGoodThreshold);
  571. LogFile.WriteToFile(zw);
  572. }
  573. else
  574. {
  575. GENERAL[_ana]->ROI[i]->isReject = false;
  576. }
  577. GENERAL[_ana]->ROI[i]->result_float = result;
  578. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  579. }
  580. break;
  581. default:
  582. break;
  583. }
  584. }
  585. }
  586. delete tflite;
  587. return true;
  588. }
  589. bool ClassFlowCNNGeneral::isExtendedResolution(int _number)
  590. {
  591. if (!(CNNType == Digital))
  592. return true;
  593. return false;
  594. }
  595. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo()
  596. {
  597. std::vector<HTMLInfo*> result;
  598. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  599. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  600. {
  601. if (GENERAL[_ana]->name == "default")
  602. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  603. else
  604. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  605. HTMLInfo *zw = new HTMLInfo;
  606. if (GENERAL[_ana]->name == "default")
  607. {
  608. zw->filename = GENERAL[_ana]->ROI[i]->name + ".bmp";
  609. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  610. }
  611. else
  612. {
  613. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp";
  614. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  615. }
  616. if (CNNType == Digital)
  617. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  618. else
  619. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  620. zw->image = GENERAL[_ana]->ROI[i]->image;
  621. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  622. result.push_back(zw);
  623. }
  624. return result;
  625. }
  626. int ClassFlowCNNGeneral::getAnzahlGENERAL()
  627. {
  628. return GENERAL.size();
  629. }
  630. string ClassFlowCNNGeneral::getNameGENERAL(int _analog)
  631. {
  632. if (_analog < GENERAL.size())
  633. return GENERAL[_analog]->name;
  634. return "GENERAL DOES NOT EXIST";
  635. }
  636. general* ClassFlowCNNGeneral::GetGENERAL(int _analog)
  637. {
  638. if (_analog < GENERAL.size())
  639. return GENERAL[_analog];
  640. return NULL;
  641. }
  642. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers)
  643. {
  644. for (int _dig = 0; _dig < GENERAL.size(); _dig++)
  645. {
  646. std::string _name = GENERAL[_dig]->name;
  647. bool found = false;
  648. for (int i = 0; i < (*_name_numbers).size(); ++i)
  649. {
  650. if ((*_name_numbers)[i] == _name)
  651. found = true;
  652. }
  653. if (!found)
  654. (*_name_numbers).push_back(_name);
  655. }
  656. }