ClassFlowCNNGeneral.cpp 22 KB

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