ClassFlowCNNGeneral.cpp 22 KB

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