ClassFlowCNNGeneral.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 || CNNType == Digital100))
  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. int ClassFlowCNNGeneral::ZeigerEval(float zahl, int ziffer_vorgaenger)
  174. {
  175. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  176. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  177. int ergebnis, ergebnis_rating;
  178. if (ziffer_vorgaenger == -1)
  179. return ergebnis_vorkomma % 10;
  180. ergebnis_rating = ergebnis_nachkomma - ziffer_vorgaenger;
  181. if (ergebnis_nachkomma >= 5)
  182. ergebnis_rating-=5;
  183. else
  184. ergebnis_rating+=5;
  185. ergebnis = (int) round(zahl);
  186. if (ergebnis_rating < 0)
  187. ergebnis-=1;
  188. if (ergebnis == -1)
  189. ergebnis+=10;
  190. ergebnis = (ergebnis + 10) % 10;
  191. return ergebnis;
  192. }
  193. bool ClassFlowCNNGeneral::ReadParameter(FILE* pfile, string& aktparamgraph)
  194. {
  195. std::vector<string> zerlegt;
  196. aktparamgraph = trim(aktparamgraph);
  197. if (aktparamgraph.size() == 0)
  198. if (!this->GetNextParagraph(pfile, aktparamgraph))
  199. return false;
  200. if ((toUpper(aktparamgraph) != "[ANALOG]") && (toUpper(aktparamgraph) != ";[ANALOG]")
  201. && (toUpper(aktparamgraph) != "[DIGIT]") && (toUpper(aktparamgraph) != ";[DIGIT]")
  202. && (toUpper(aktparamgraph) != "[DIGITS]") && (toUpper(aktparamgraph) != ";[DIGITS]")
  203. ) // Paragraph passt nicht
  204. return false;
  205. if (aktparamgraph[0] == ';')
  206. {
  207. disabled = true;
  208. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  209. printf("[Analog/Digit] is disabled !!!\n");
  210. return true;
  211. }
  212. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  213. {
  214. zerlegt = this->ZerlegeZeile(aktparamgraph);
  215. if ((toUpper(zerlegt[0]) == "LOGIMAGELOCATION") && (zerlegt.size() > 1))
  216. {
  217. this->LogImageLocation = "/sdcard" + zerlegt[1];
  218. this->isLogImage = true;
  219. }
  220. if ((toUpper(zerlegt[0]) == "LOGIMAGESELECT") && (zerlegt.size() > 1))
  221. {
  222. LogImageSelect = zerlegt[1];
  223. isLogImageSelect = true;
  224. }
  225. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  226. {
  227. this->logfileRetentionInDays = std::stoi(zerlegt[1]);
  228. }
  229. if ((toUpper(zerlegt[0]) == "MODELTYPE") && (zerlegt.size() > 1))
  230. {
  231. if (toUpper(zerlegt[1]) == "DIGITHYPRID")
  232. CNNType = DigitalHyprid;
  233. }
  234. if ((toUpper(zerlegt[0]) == "MODEL") && (zerlegt.size() > 1))
  235. {
  236. this->cnnmodelfile = zerlegt[1];
  237. }
  238. if ((toUpper(zerlegt[0]) == "CNNGOODTHRESHOLD") && (zerlegt.size() > 1))
  239. {
  240. CNNGoodThreshold = std::stof(zerlegt[1]);
  241. }
  242. /* if ((toUpper(zerlegt[0]) == "MODELINPUTSIZE") && (zerlegt.size() > 2))
  243. {
  244. this->modelxsize = std::stoi(zerlegt[1]);
  245. this->modelysize = std::stoi(zerlegt[2]);
  246. }
  247. */
  248. if (zerlegt.size() >= 5)
  249. {
  250. general* _analog = GetGENERAL(zerlegt[0], true);
  251. roi* neuroi = _analog->ROI[_analog->ROI.size()-1];
  252. neuroi->posx = std::stoi(zerlegt[1]);
  253. neuroi->posy = std::stoi(zerlegt[2]);
  254. neuroi->deltax = std::stoi(zerlegt[3]);
  255. neuroi->deltay = std::stoi(zerlegt[4]);
  256. neuroi->result_float = -1;
  257. neuroi->image = NULL;
  258. neuroi->image_org = NULL;
  259. }
  260. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  261. {
  262. if (toUpper(zerlegt[1]) == "TRUE")
  263. SaveAllFiles = true;
  264. }
  265. }
  266. if (!getNetworkParameter())
  267. return false;
  268. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  269. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  270. {
  271. GENERAL[_ana]->ROI[i]->image = new CImageBasis(modelxsize, modelysize, modelchannel);
  272. GENERAL[_ana]->ROI[i]->image_org = new CImageBasis(GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, 3);
  273. }
  274. return true;
  275. }
  276. general* ClassFlowCNNGeneral::FindGENERAL(string _name_number)
  277. {
  278. for (int i = 0; i < GENERAL.size(); ++i)
  279. if (GENERAL[i]->name == _name_number)
  280. return GENERAL[i];
  281. return NULL;
  282. }
  283. general* ClassFlowCNNGeneral::GetGENERAL(string _name, bool _create = true)
  284. {
  285. string _analog, _roi;
  286. int _pospunkt = _name.find_first_of(".");
  287. if (_pospunkt > -1)
  288. {
  289. _analog = _name.substr(0, _pospunkt);
  290. _roi = _name.substr(_pospunkt+1, _name.length() - _pospunkt - 1);
  291. }
  292. else
  293. {
  294. _analog = "default";
  295. _roi = _name;
  296. }
  297. general *_ret = NULL;
  298. for (int i = 0; i < GENERAL.size(); ++i)
  299. if (GENERAL[i]->name == _analog)
  300. _ret = GENERAL[i];
  301. if (!_create) // nicht gefunden und soll auch nicht erzeugt werden
  302. return _ret;
  303. if (_ret == NULL)
  304. {
  305. _ret = new general;
  306. _ret->name = _analog;
  307. GENERAL.push_back(_ret);
  308. }
  309. roi* neuroi = new roi;
  310. neuroi->name = _roi;
  311. _ret->ROI.push_back(neuroi);
  312. printf("GetGENERAL - GENERAL %s - roi %s\n", _analog.c_str(), _roi.c_str());
  313. return _ret;
  314. }
  315. string ClassFlowCNNGeneral::getHTMLSingleStep(string host)
  316. {
  317. string result, zw;
  318. std::vector<HTMLInfo*> htmlinfo;
  319. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  320. result = result + "Analog Pointers: <p> ";
  321. htmlinfo = GetHTMLInfo();
  322. for (int i = 0; i < htmlinfo.size(); ++i)
  323. {
  324. std::stringstream stream;
  325. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  326. zw = stream.str();
  327. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  328. delete htmlinfo[i];
  329. }
  330. htmlinfo.clear();
  331. return result;
  332. }
  333. bool ClassFlowCNNGeneral::doFlow(string time)
  334. {
  335. if (disabled)
  336. return true;
  337. if (!doAlignAndCut(time)){
  338. return false;
  339. };
  340. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::doFlow nach Alignment");
  341. doNeuralNetwork(time);
  342. RemoveOldLogs();
  343. return true;
  344. }
  345. bool ClassFlowCNNGeneral::doAlignAndCut(string time)
  346. {
  347. if (disabled)
  348. return true;
  349. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  350. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  351. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  352. {
  353. printf("General %d - Align&Cut\n", i);
  354. 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);
  355. if (SaveAllFiles)
  356. {
  357. if (GENERAL[_ana]->name == "default")
  358. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  359. else
  360. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  361. }
  362. GENERAL[_ana]->ROI[i]->image_org->Resize(modelxsize, modelysize, GENERAL[_ana]->ROI[i]->image);
  363. if (SaveAllFiles)
  364. {
  365. if (GENERAL[_ana]->name == "default")
  366. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  367. else
  368. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  369. }
  370. }
  371. return true;
  372. }
  373. void ClassFlowCNNGeneral::DrawROI(CImageBasis *_zw)
  374. {
  375. if (CNNType == Analogue)
  376. {
  377. int r = 0;
  378. int g = 255;
  379. int b = 0;
  380. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  381. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  382. {
  383. _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);
  384. _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);
  385. _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);
  386. _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);
  387. }
  388. }
  389. else
  390. {
  391. for (int _dig = 0; _dig < GENERAL.size(); ++_dig)
  392. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i)
  393. _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);
  394. }
  395. }
  396. bool ClassFlowCNNGeneral::getNetworkParameter()
  397. {
  398. if (disabled)
  399. return true;
  400. CTfLiteClass *tflite = new CTfLiteClass;
  401. string zwcnn = "/sdcard" + cnnmodelfile;
  402. zwcnn = FormatFileName(zwcnn);
  403. printf(zwcnn.c_str());printf("\n");
  404. if (!tflite->LoadModel(zwcnn)) {
  405. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  406. LogFile.WriteToFile("Cannot load model");
  407. delete tflite;
  408. return false;
  409. }
  410. tflite->MakeAllocate();
  411. if (CNNType == AutoDetect)
  412. {
  413. tflite->GetInputDimension(false);
  414. modelxsize = tflite->ReadInputDimenstion(0);
  415. modelysize = tflite->ReadInputDimenstion(1);
  416. modelchannel = tflite->ReadInputDimenstion(2);
  417. int _anzoutputdimensions = tflite->GetAnzOutPut();
  418. switch (_anzoutputdimensions)
  419. {
  420. case 2:
  421. CNNType = Analogue;
  422. printf("TFlite-Type set to Analogue\n");
  423. break;
  424. case 10:
  425. CNNType = DoubleHyprid10;
  426. printf("TFlite-Type set to DoubleHyprid10\n");
  427. break;
  428. case 11:
  429. CNNType = Digital;
  430. printf("TFlite-Type set to Digital\n");
  431. break;
  432. case 20:
  433. CNNType = DigitalHyprid10;
  434. printf("TFlite-Type set to DigitalHyprid10\n");
  435. break;
  436. case 22:
  437. CNNType = DigitalHyprid;
  438. printf("TFlite-Type set to DigitalHyprid\n");
  439. break;
  440. case 100:
  441. CNNType = Digital100;
  442. printf("TFlite-Type set to Digital\n");
  443. break;
  444. default:
  445. printf("ERROR ERROR ERROR - tflite passt nicht zur Firmware - ERROR ERROR ERROR\n");
  446. }
  447. }
  448. delete tflite;
  449. return true;
  450. }
  451. bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
  452. {
  453. if (disabled)
  454. return true;
  455. string logPath = CreateLogFolder(time);
  456. CTfLiteClass *tflite = new CTfLiteClass;
  457. string zwcnn = "/sdcard" + cnnmodelfile;
  458. zwcnn = FormatFileName(zwcnn);
  459. printf(zwcnn.c_str());printf("\n");
  460. if (!tflite->LoadModel(zwcnn)) {
  461. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  462. LogFile.WriteToFile("Cannot load model");
  463. delete tflite;
  464. return false;
  465. }
  466. tflite->MakeAllocate();
  467. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  468. {
  469. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  470. {
  471. printf("General %d - TfLite\n", i);
  472. switch (CNNType) {
  473. case Analogue:
  474. {
  475. float f1, f2;
  476. f1 = 0; f2 = 0;
  477. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  478. tflite->Invoke();
  479. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  480. f1 = tflite->GetOutputValue(0);
  481. f2 = tflite->GetOutputValue(1);
  482. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  483. GENERAL[_ana]->ROI[i]->result_float = result * 10;
  484. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  485. if (isLogImage)
  486. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  487. } break;
  488. case Digital:
  489. {
  490. GENERAL[_ana]->ROI[i]->result_klasse = 0;
  491. GENERAL[_ana]->ROI[i]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[_ana]->ROI[i]->image);
  492. printf("Result General(Digit)%i: %d\n", i, GENERAL[_ana]->ROI[i]->result_klasse);
  493. if (isLogImage)
  494. {
  495. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  496. if (isLogImageSelect)
  497. {
  498. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  499. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  500. }
  501. else
  502. {
  503. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  504. }
  505. }
  506. } break;
  507. case DigitalHyprid:
  508. {
  509. int _num, _nachkomma;
  510. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  511. tflite->Invoke();
  512. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  513. _num = tflite->GetOutClassification(0, 10);
  514. _nachkomma = tflite->GetOutClassification(11, 21);
  515. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  516. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  517. if ((_num == 10) || (_nachkomma == 10)) // NaN detektiert
  518. GENERAL[_ana]->ROI[i]->result_float = -1;
  519. else
  520. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  521. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  522. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  523. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  524. if (isLogImage)
  525. {
  526. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  527. if (isLogImageSelect)
  528. {
  529. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  530. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  531. }
  532. else
  533. {
  534. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  535. }
  536. }
  537. } break;
  538. case DigitalHyprid10:
  539. {
  540. int _num, _nachkomma;
  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. _nachkomma = tflite->GetOutClassification(10, 19);
  546. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  547. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  548. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  549. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  550. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  551. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  552. if (isLogImage)
  553. {
  554. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  555. if (isLogImageSelect)
  556. {
  557. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  558. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  559. }
  560. else
  561. {
  562. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  563. }
  564. }
  565. } break;
  566. case DoubleHyprid10:
  567. {
  568. int _num, _numplus, _numminus;
  569. float _val, _valplus, _valminus;
  570. float _fit;
  571. float _result_save_file;
  572. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  573. tflite->Invoke();
  574. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  575. _num = tflite->GetOutClassification(0, 9);
  576. _numplus = (_num + 1) % 10;
  577. _numminus = (_num - 1) % 10;
  578. _val = tflite->GetOutputValue(_num);
  579. _valplus = tflite->GetOutputValue(_numplus);
  580. _valminus = tflite->GetOutputValue(_numminus);
  581. float result = _num;
  582. if (_valplus > _numminus)
  583. {
  584. result = result + _valplus / (_valplus + _val);
  585. _fit = _val + _valplus;
  586. }
  587. else
  588. {
  589. result = result - _valminus / (_val + _valminus);
  590. _fit = _val + _valminus;
  591. }
  592. if (result > 10)
  593. result = result - 10;
  594. if (result < 0)
  595. result = result + 10;
  596. _result_save_file = result;
  597. if (_fit < CNNGoodThreshold)
  598. {
  599. GENERAL[_ana]->ROI[i]->isReject = true;
  600. result = -1;
  601. _result_save_file+= 100; // Für den Fall, dass fit nicht ausreichend, soll trotzdem das Ergebnis mit "-10x.y" abgespeichert werden.
  602. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold);
  603. printf("Value Rejected due to Threshold (Fit: %f, Threshold: %f\n", _fit, CNNGoodThreshold);
  604. LogFile.WriteToFile(zw);
  605. }
  606. else
  607. {
  608. GENERAL[_ana]->ROI[i]->isReject = false;
  609. }
  610. GENERAL[_ana]->ROI[i]->result_float = result;
  611. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  612. if (isLogImage)
  613. {
  614. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  615. if (isLogImageSelect)
  616. {
  617. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  618. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  619. }
  620. else
  621. {
  622. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  623. }
  624. }
  625. }
  626. break;
  627. case Digital100:
  628. {
  629. GENERAL[_ana]->ROI[i]->result_float = (float)tflite->GetClassFromImageBasis(GENERAL[_ana]->ROI[i]->image) /10.0;
  630. printf("Result General(Digit)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  631. if (isLogImage)
  632. {
  633. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  634. if (isLogImageSelect)
  635. {
  636. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  637. LogImage(logPath, _imagename, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  638. }
  639. else
  640. {
  641. LogImage(logPath, _imagename, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  642. }
  643. }
  644. } break;
  645. default:
  646. break;
  647. }
  648. }
  649. }
  650. delete tflite;
  651. return true;
  652. }
  653. bool ClassFlowCNNGeneral::isExtendedResolution(int _number)
  654. {
  655. if (!(CNNType == Digital))
  656. return true;
  657. return false;
  658. }
  659. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo()
  660. {
  661. std::vector<HTMLInfo*> result;
  662. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  663. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  664. {
  665. if (GENERAL[_ana]->name == "default")
  666. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  667. else
  668. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  669. HTMLInfo *zw = new HTMLInfo;
  670. if (GENERAL[_ana]->name == "default")
  671. {
  672. zw->filename = GENERAL[_ana]->ROI[i]->name + ".bmp";
  673. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  674. }
  675. else
  676. {
  677. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp";
  678. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  679. }
  680. if (CNNType == Digital)
  681. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  682. else
  683. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  684. zw->image = GENERAL[_ana]->ROI[i]->image;
  685. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  686. result.push_back(zw);
  687. }
  688. return result;
  689. }
  690. int ClassFlowCNNGeneral::getAnzahlGENERAL()
  691. {
  692. return GENERAL.size();
  693. }
  694. string ClassFlowCNNGeneral::getNameGENERAL(int _analog)
  695. {
  696. if (_analog < GENERAL.size())
  697. return GENERAL[_analog]->name;
  698. return "GENERAL DOES NOT EXIST";
  699. }
  700. general* ClassFlowCNNGeneral::GetGENERAL(int _analog)
  701. {
  702. if (_analog < GENERAL.size())
  703. return GENERAL[_analog];
  704. return NULL;
  705. }
  706. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers)
  707. {
  708. for (int _dig = 0; _dig < GENERAL.size(); _dig++)
  709. {
  710. std::string _name = GENERAL[_dig]->name;
  711. bool found = false;
  712. for (int i = 0; i < (*_name_numbers).size(); ++i)
  713. {
  714. if ((*_name_numbers)[i] == _name)
  715. found = true;
  716. }
  717. if (!found)
  718. (*_name_numbers).push_back(_name);
  719. }
  720. }