ClassFlowCNNGeneral.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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->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);
  362. _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);
  363. _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);
  364. }
  365. }
  366. else
  367. {
  368. for (int _dig = 0; _dig < GENERAL.size(); ++_dig)
  369. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i)
  370. _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);
  371. }
  372. }
  373. bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
  374. {
  375. if (disabled)
  376. return true;
  377. string logPath = CreateLogFolder(time);
  378. CTfLiteClass *tflite = new CTfLiteClass;
  379. string zwcnn = "/sdcard" + cnnmodelfile;
  380. zwcnn = FormatFileName(zwcnn);
  381. printf(zwcnn.c_str());printf("\n");
  382. if (!tflite->LoadModel(zwcnn)) {
  383. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  384. LogFile.WriteToFile("Cannot load model");
  385. delete tflite;
  386. return false;
  387. }
  388. tflite->MakeAllocate();
  389. if (CNNType == AutoDetect)
  390. {
  391. int _anzoutputdimensions = tflite->GetAnzOutPut();
  392. switch (_anzoutputdimensions)
  393. {
  394. case 2:
  395. CNNType = Analogue;
  396. printf("TFlite-Type set to Analogue\n");
  397. break;
  398. case 11:
  399. CNNType = Digital;
  400. printf("TFlite-Type set to Digital\n");
  401. break;
  402. case 22:
  403. CNNType = DigitalHyprid;
  404. printf("TFlite-Type set to DigitalHyprid\n");
  405. break;
  406. default:
  407. printf("ERROR ERROR ERROR - tflite passt nicht zur Firmware - ERROR ERROR ERROR\n");
  408. }
  409. // flowpostprocessing->UpdateNachkommaDecimalShift();
  410. }
  411. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  412. {
  413. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  414. {
  415. printf("General %d - TfLite\n", i);
  416. switch (CNNType) {
  417. case Analogue:
  418. {
  419. float f1, f2;
  420. f1 = 0; f2 = 0;
  421. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  422. tflite->Invoke();
  423. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  424. f1 = tflite->GetOutputValue(0);
  425. f2 = tflite->GetOutputValue(1);
  426. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  427. GENERAL[_ana]->ROI[i]->result_float = result * 10;
  428. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  429. if (isLogImage)
  430. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  431. } break;
  432. case Digital:
  433. {
  434. GENERAL[_ana]->ROI[i]->result_klasse = 0;
  435. GENERAL[_ana]->ROI[i]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[_ana]->ROI[i]->image);
  436. printf("Result General(Digit)%i: %d\n", i, GENERAL[_ana]->ROI[i]->result_klasse);
  437. if (isLogImage)
  438. {
  439. if (isLogImageSelect)
  440. {
  441. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  442. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  443. }
  444. else
  445. {
  446. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  447. }
  448. }
  449. } break;
  450. case DigitalHyprid:
  451. {
  452. int _num, _nachkomma;
  453. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  454. tflite->Invoke();
  455. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  456. _num = tflite->GetOutClassification(0, 10);
  457. _nachkomma = tflite->GetOutClassification(11, 21);
  458. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  459. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  460. if ((_num == 10) || (_nachkomma == 10)) // NaN detektiert
  461. GENERAL[_ana]->ROI[i]->result_float = -1;
  462. else
  463. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  464. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  465. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  466. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  467. if (isLogImage)
  468. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  469. } break;
  470. default:
  471. break;
  472. }
  473. }
  474. }
  475. delete tflite;
  476. return true;
  477. }
  478. bool ClassFlowCNNGeneral::isExtendedResolution(int _number)
  479. {
  480. // if (extendedResolution && !(CNNType == Digital))
  481. if (!(CNNType == Digital))
  482. return true;
  483. return false;
  484. }
  485. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo()
  486. {
  487. std::vector<HTMLInfo*> result;
  488. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  489. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  490. {
  491. if (GENERAL[_ana]->name == "default")
  492. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  493. else
  494. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  495. HTMLInfo *zw = new HTMLInfo;
  496. if (GENERAL[_ana]->name == "default")
  497. {
  498. zw->filename = GENERAL[_ana]->ROI[i]->name + ".bmp";
  499. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  500. }
  501. else
  502. {
  503. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp";
  504. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  505. }
  506. if (CNNType == Digital)
  507. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  508. else
  509. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  510. zw->image = GENERAL[_ana]->ROI[i]->image;
  511. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  512. // printf("Push %s\n", zw->filename.c_str());
  513. result.push_back(zw);
  514. }
  515. // printf("größe: %d\n", result.size());
  516. return result;
  517. }
  518. int ClassFlowCNNGeneral::getAnzahlGENERAL()
  519. {
  520. return GENERAL.size();
  521. }
  522. string ClassFlowCNNGeneral::getNameGENERAL(int _analog)
  523. {
  524. if (_analog < GENERAL.size())
  525. return GENERAL[_analog]->name;
  526. return "GENERAL DOES NOT EXIST";
  527. }
  528. general* ClassFlowCNNGeneral::GetGENERAL(int _analog)
  529. {
  530. if (_analog < GENERAL.size())
  531. return GENERAL[_analog];
  532. return NULL;
  533. }
  534. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers)
  535. {
  536. for (int _dig = 0; _dig < GENERAL.size(); _dig++)
  537. {
  538. std::string _name = GENERAL[_dig]->name;
  539. bool found = false;
  540. for (int i = 0; i < (*_name_numbers).size(); ++i)
  541. {
  542. if ((*_name_numbers)[i] == _name)
  543. found = true;
  544. }
  545. if (!found)
  546. (*_name_numbers).push_back(_name);
  547. }
  548. }