ClassFlowCNNGeneral.cpp 22 KB

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