ClassFlowCNNGeneral.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. #include "ClassFlowCNNGeneral.h"
  2. #include <math.h>
  3. #include <iomanip>
  4. #include <sys/types.h>
  5. #include <sstream> // std::stringstream
  6. #include "CTfLiteClass.h"
  7. #include "ClassLogFile.h"
  8. static const char* TAG = "flow_analog";
  9. bool debugdetailgeneral = false;
  10. ClassFlowCNNGeneral::ClassFlowCNNGeneral(ClassFlowAlignment *_flowalign, t_CNNType _cnntype) : ClassFlowImage(NULL, TAG)
  11. {
  12. string cnnmodelfile = "";
  13. modelxsize = 1;
  14. modelysize = 1;
  15. CNNGoodThreshold = 0.0;
  16. ListFlowControll = NULL;
  17. previousElement = NULL;
  18. SaveAllFiles = false;
  19. disabled = false;
  20. isLogImageSelect = false;
  21. CNNType = AutoDetect;
  22. CNNType = _cnntype;
  23. flowpostalignment = _flowalign;
  24. }
  25. string ClassFlowCNNGeneral::getReadout(int _analog = 0, bool _extendedResolution, int prev)
  26. {
  27. string result = "";
  28. if (GENERAL[_analog]->ROI.size() == 0)
  29. return result;
  30. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout _analog=" + std::to_string(_analog) + ", _extendedResolution=" + std::to_string(_extendedResolution) + ", prev=" + std::to_string(prev));
  31. if (CNNType == Analogue || CNNType == Analogue100)
  32. {
  33. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  34. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  35. prev = ZeigerEval(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev);
  36. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout(analog) zahl=" + std::to_string(zahl) + ", ergebnis_nachkomma=" + std::to_string(ergebnis_nachkomma) + ", prev=" + std::to_string(prev));
  37. result = std::to_string(prev);
  38. if (_extendedResolution && (CNNType != Digital))
  39. result = result + std::to_string(ergebnis_nachkomma);
  40. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  41. {
  42. prev = ZeigerEval(GENERAL[_analog]->ROI[i]->result_float, prev);
  43. result = std::to_string(prev) + result;
  44. }
  45. return result;
  46. }
  47. if (CNNType == Digital)
  48. {
  49. for (int i = 0; i < GENERAL[_analog]->ROI.size(); ++i)
  50. {
  51. if (GENERAL[_analog]->ROI[i]->result_klasse >= 10)
  52. result = result + "N";
  53. else
  54. result = result + std::to_string(GENERAL[_analog]->ROI[i]->result_klasse);
  55. }
  56. return result;
  57. }
  58. if ((CNNType == DoubleHyprid10) || (CNNType == Digital100))
  59. {
  60. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  61. if (zahl >= 0) // NaN?
  62. {
  63. if (_extendedResolution) // ist nur gesetzt, falls es die erste Ziffer ist (kein Analog vorher!)
  64. {
  65. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  66. int ergebnis_vorkomma = ((int) floor(zahl)) % 10;
  67. result = std::to_string(ergebnis_vorkomma) + std::to_string(ergebnis_nachkomma);
  68. prev = ergebnis_vorkomma;
  69. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout(dig100-ext) ergebnis_vorkomma=" + std::to_string(ergebnis_vorkomma) + ", ergebnis_nachkomma=" + std::to_string(ergebnis_nachkomma) + ", prev=" + std::to_string(prev));
  70. }
  71. else
  72. {
  73. // prev = ZeigerEval(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev);
  74. prev = ZeigerEvalHybrid(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, prev, prev);
  75. result = std::to_string(prev);
  76. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::getReadout(dig100) prev=" + std::to_string(prev));
  77. }
  78. }
  79. else
  80. {
  81. result = "N";
  82. if (_extendedResolution && (CNNType != Digital))
  83. result = "NN";
  84. }
  85. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  86. {
  87. if (GENERAL[_analog]->ROI[i]->result_float >= 0)
  88. {
  89. prev = ZeigerEvalHybrid(GENERAL[_analog]->ROI[i]->result_float, GENERAL[_analog]->ROI[i+1]->result_float, prev);
  90. result = std::to_string(prev) + result;
  91. }
  92. else
  93. {
  94. prev = -1;
  95. result = "N" + result;
  96. }
  97. }
  98. return result;
  99. }
  100. /*
  101. if (CNNType == Digital100)
  102. {
  103. int zif_akt = -1;
  104. float zahl = GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float;
  105. if (zahl >= 0) // NaN?
  106. {
  107. if (_extendedResolution)
  108. {
  109. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  110. int ergebnis_vorkomma = ((int) floor(zahl)) % 10;
  111. result = std::to_string(ergebnis_vorkomma) + std::to_string(ergebnis_nachkomma);
  112. zif_akt = ergebnis_vorkomma;
  113. }
  114. else
  115. {
  116. zif_akt = ZeigerEvalHybrid(GENERAL[_analog]->ROI[GENERAL[_analog]->ROI.size() - 1]->result_float, -1, -1);
  117. result = std::to_string(zif_akt);
  118. }
  119. }
  120. else
  121. {
  122. result = "N";
  123. if (_extendedResolution && (CNNType != Digital))
  124. result = "NN";
  125. }
  126. for (int i = GENERAL[_analog]->ROI.size() - 2; i >= 0; --i)
  127. {
  128. if (GENERAL[_analog]->ROI[i]->result_float >= 0)
  129. {
  130. zif_akt = ZeigerEvalHybrid(GENERAL[_analog]->ROI[i]->result_float, GENERAL[_analog]->ROI[i+1]->result_float, zif_akt);
  131. result = std::to_string(zif_akt) + result;
  132. }
  133. else
  134. {
  135. zif_akt = -1;
  136. result = "N" + result;
  137. }
  138. }
  139. return result;
  140. }
  141. */
  142. return result;
  143. }
  144. int ClassFlowCNNGeneral::ZeigerEvalHybrid(float zahl, float zahl_vorgaenger, int eval_vorgaenger)
  145. {
  146. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  147. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  148. if (eval_vorgaenger < 0) // keine Vorzahl vorhanden !!! --> Runde die Zahl
  149. {
  150. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  151. return ((int) round(zahl) + 10) % 10;
  152. else
  153. return ((int) trunc(zahl) + 10) % 10;
  154. }
  155. if ((zahl_vorgaenger >= 0.5 ) && (zahl_vorgaenger < 9.5))
  156. {
  157. // kein Ziffernwechsel, da Vorkomma weit genug weg ist (0+/-0.5) --> zahl wird gerundet
  158. return ((int) round(zahl) + 10) % 10;
  159. }
  160. else
  161. {
  162. if (eval_vorgaenger <= 1) // Nulldurchgang hat stattgefunden (!Bewertung über Prev_value und nicht Zahl!) --> hier aufrunden (2.8 --> 3, aber auch 3.1 --> 3)
  163. {
  164. if (ergebnis_nachkomma > 5)
  165. return (ergebnis_vorkomma + 1) % 10;
  166. else
  167. return ergebnis_vorkomma;
  168. }
  169. else // bleibt nur >= 9.5 --> noch kein Nulldurchgang --> 2.8 --> 2, und 3.1 --> 2
  170. {
  171. // hier auf 4 reduziert, da erst ab Vorgänder 9 anfängt umzustellen. Bei 9.5 Vorgänger kann die aktuelle
  172. // Zahl noch x.4 - x.5 sein.
  173. if (ergebnis_nachkomma >= 4)
  174. return ergebnis_vorkomma;
  175. else
  176. return (ergebnis_vorkomma - 1 + 10) % 10;
  177. }
  178. }
  179. return -1;
  180. /*
  181. if (zahl_vorgaenger > 9.2) // Ziffernwechsel beginnt
  182. {
  183. if (eval_vorgaenger == 0) // Wechsel hat schon stattgefunden
  184. {
  185. return ((int) round(zahl) + 10) % 10; // Annahme, dass die neue Zahl schon in der Nähe des Ziels ist
  186. }
  187. else
  188. {
  189. if (zahl_vorgaenger <= 9.5) // Wechsel startet gerade, aber beginnt erst
  190. {
  191. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  192. return ((int) round(zahl) + 10) % 10;
  193. else
  194. return ((int) trunc(zahl) + 10) % 10;
  195. }
  196. else
  197. {
  198. return ((int) trunc(zahl) + 10) % 10; // Wechsel schon weiter fortgeschritten, d.h. über 2 als Nachkomma
  199. }
  200. }
  201. }
  202. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  203. return ((int) round(zahl) + 10) % 10;
  204. return ((int) trunc(zahl) + 10) % 10;
  205. */
  206. }
  207. int ClassFlowCNNGeneral::ZeigerEval(float zahl, int ziffer_vorgaenger)
  208. {
  209. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  210. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  211. int ergebnis;
  212. float ergebnis_rating;
  213. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::ZeigerEval erg_v=" + std::to_string(ergebnis_vorkomma) + ", erg_n=" + std::to_string(ergebnis_nachkomma) + ", ziff_v=" + std::to_string(ziffer_vorgaenger));
  214. if (ziffer_vorgaenger == -1)
  215. return ergebnis_vorkomma % 10;
  216. // Ist die aktuelle Stelle schon umgesprungen und die Vorstelle noch nicht?
  217. // Akt.: 2.1, Vorstelle = 0.9 => 1.9
  218. // Problem sind mehrere Rundungen
  219. // Bsp. zahl=4.5, Vorgänger= 9.6 (ziffer_vorgaenger=0)
  220. // Tritt nur auf bei Übergang von analog auf digit
  221. ergebnis_rating = ergebnis_nachkomma - ziffer_vorgaenger;
  222. if (ergebnis_nachkomma >= 5)
  223. ergebnis_rating-=5.1;
  224. else
  225. ergebnis_rating+=5;
  226. ergebnis = (int) round(zahl);
  227. if (ergebnis_rating < 0)
  228. ergebnis-=1;
  229. if (ergebnis == -1)
  230. ergebnis+=10;
  231. ergebnis = (ergebnis + 10) % 10;
  232. return ergebnis;
  233. }
  234. bool ClassFlowCNNGeneral::ReadParameter(FILE* pfile, string& aktparamgraph)
  235. {
  236. std::vector<string> zerlegt;
  237. aktparamgraph = trim(aktparamgraph);
  238. if (aktparamgraph.size() == 0)
  239. if (!this->GetNextParagraph(pfile, aktparamgraph))
  240. return false;
  241. if ((toUpper(aktparamgraph) != "[ANALOG]") && (toUpper(aktparamgraph) != ";[ANALOG]")
  242. && (toUpper(aktparamgraph) != "[DIGIT]") && (toUpper(aktparamgraph) != ";[DIGIT]")
  243. && (toUpper(aktparamgraph) != "[DIGITS]") && (toUpper(aktparamgraph) != ";[DIGITS]")
  244. ) // Paragraph passt nicht
  245. return false;
  246. if (aktparamgraph[0] == ';')
  247. {
  248. disabled = true;
  249. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  250. printf("[Analog/Digit] is disabled !!!\n");
  251. return true;
  252. }
  253. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  254. {
  255. zerlegt = this->ZerlegeZeile(aktparamgraph);
  256. if ((toUpper(zerlegt[0]) == "LOGIMAGELOCATION") && (zerlegt.size() > 1))
  257. {
  258. this->LogImageLocation = "/sdcard" + zerlegt[1];
  259. this->isLogImage = true;
  260. }
  261. if ((toUpper(zerlegt[0]) == "LOGIMAGESELECT") && (zerlegt.size() > 1))
  262. {
  263. LogImageSelect = zerlegt[1];
  264. isLogImageSelect = true;
  265. }
  266. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  267. {
  268. this->logfileRetentionInDays = std::stoi(zerlegt[1]);
  269. }
  270. // if ((toUpper(zerlegt[0]) == "MODELTYPE") && (zerlegt.size() > 1))
  271. // {
  272. // if (toUpper(zerlegt[1]) == "DIGITHYPRID")
  273. // CNNType = DigitalHyprid;
  274. // }
  275. if ((toUpper(zerlegt[0]) == "MODEL") && (zerlegt.size() > 1))
  276. {
  277. this->cnnmodelfile = zerlegt[1];
  278. }
  279. if ((toUpper(zerlegt[0]) == "CNNGOODTHRESHOLD") && (zerlegt.size() > 1))
  280. {
  281. CNNGoodThreshold = std::stof(zerlegt[1]);
  282. }
  283. if (zerlegt.size() >= 5)
  284. {
  285. general* _analog = GetGENERAL(zerlegt[0], true);
  286. roi* neuroi = _analog->ROI[_analog->ROI.size()-1];
  287. neuroi->posx = std::stoi(zerlegt[1]);
  288. neuroi->posy = std::stoi(zerlegt[2]);
  289. neuroi->deltax = std::stoi(zerlegt[3]);
  290. neuroi->deltay = std::stoi(zerlegt[4]);
  291. neuroi->result_float = -1;
  292. neuroi->image = NULL;
  293. neuroi->image_org = NULL;
  294. }
  295. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  296. {
  297. if (toUpper(zerlegt[1]) == "TRUE")
  298. SaveAllFiles = true;
  299. }
  300. }
  301. if (!getNetworkParameter())
  302. return false;
  303. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  304. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  305. {
  306. GENERAL[_ana]->ROI[i]->image = new CImageBasis(modelxsize, modelysize, modelchannel);
  307. GENERAL[_ana]->ROI[i]->image_org = new CImageBasis(GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, 3);
  308. }
  309. return true;
  310. }
  311. general* ClassFlowCNNGeneral::FindGENERAL(string _name_number)
  312. {
  313. for (int i = 0; i < GENERAL.size(); ++i)
  314. if (GENERAL[i]->name == _name_number)
  315. return GENERAL[i];
  316. return NULL;
  317. }
  318. general* ClassFlowCNNGeneral::GetGENERAL(string _name, bool _create = true)
  319. {
  320. string _analog, _roi;
  321. int _pospunkt = _name.find_first_of(".");
  322. if (_pospunkt > -1)
  323. {
  324. _analog = _name.substr(0, _pospunkt);
  325. _roi = _name.substr(_pospunkt+1, _name.length() - _pospunkt - 1);
  326. }
  327. else
  328. {
  329. _analog = "default";
  330. _roi = _name;
  331. }
  332. general *_ret = NULL;
  333. for (int i = 0; i < GENERAL.size(); ++i)
  334. if (GENERAL[i]->name == _analog)
  335. _ret = GENERAL[i];
  336. if (!_create) // nicht gefunden und soll auch nicht erzeugt werden
  337. return _ret;
  338. if (_ret == NULL)
  339. {
  340. _ret = new general;
  341. _ret->name = _analog;
  342. GENERAL.push_back(_ret);
  343. }
  344. roi* neuroi = new roi;
  345. neuroi->name = _roi;
  346. _ret->ROI.push_back(neuroi);
  347. printf("GetGENERAL - GENERAL %s - roi %s\n", _analog.c_str(), _roi.c_str());
  348. return _ret;
  349. }
  350. string ClassFlowCNNGeneral::getHTMLSingleStep(string host)
  351. {
  352. string result, zw;
  353. std::vector<HTMLInfo*> htmlinfo;
  354. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  355. result = result + "Analog Pointers: <p> ";
  356. htmlinfo = GetHTMLInfo();
  357. for (int i = 0; i < htmlinfo.size(); ++i)
  358. {
  359. std::stringstream stream;
  360. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  361. zw = stream.str();
  362. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  363. delete htmlinfo[i];
  364. }
  365. htmlinfo.clear();
  366. return result;
  367. }
  368. bool ClassFlowCNNGeneral::doFlow(string time)
  369. {
  370. if (disabled)
  371. return true;
  372. if (!doAlignAndCut(time)){
  373. return false;
  374. };
  375. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::doFlow nach Alignment");
  376. doNeuralNetwork(time);
  377. RemoveOldLogs();
  378. return true;
  379. }
  380. bool ClassFlowCNNGeneral::doAlignAndCut(string time)
  381. {
  382. if (disabled)
  383. return true;
  384. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  385. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  386. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  387. {
  388. printf("General %d - Align&Cut\n", i);
  389. 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);
  390. if (SaveAllFiles)
  391. {
  392. if (GENERAL[_ana]->name == "default")
  393. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  394. else
  395. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  396. }
  397. GENERAL[_ana]->ROI[i]->image_org->Resize(modelxsize, modelysize, GENERAL[_ana]->ROI[i]->image);
  398. if (SaveAllFiles)
  399. {
  400. if (GENERAL[_ana]->name == "default")
  401. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  402. else
  403. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  404. }
  405. }
  406. return true;
  407. }
  408. void ClassFlowCNNGeneral::DrawROI(CImageBasis *_zw)
  409. {
  410. if (CNNType == Analogue || CNNType == Analogue100)
  411. {
  412. int r = 0;
  413. int g = 255;
  414. int b = 0;
  415. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  416. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  417. {
  418. _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);
  419. _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);
  420. _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);
  421. _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);
  422. }
  423. }
  424. else
  425. {
  426. for (int _dig = 0; _dig < GENERAL.size(); ++_dig)
  427. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i)
  428. _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);
  429. }
  430. }
  431. bool ClassFlowCNNGeneral::getNetworkParameter()
  432. {
  433. if (disabled)
  434. return true;
  435. CTfLiteClass *tflite = new CTfLiteClass;
  436. string zwcnn = "/sdcard" + cnnmodelfile;
  437. zwcnn = FormatFileName(zwcnn);
  438. printf(zwcnn.c_str());printf("\n");
  439. if (!tflite->LoadModel(zwcnn)) {
  440. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  441. LogFile.WriteToFile("Cannot load model");
  442. delete tflite;
  443. return false;
  444. }
  445. tflite->MakeAllocate();
  446. if (CNNType == AutoDetect)
  447. {
  448. tflite->GetInputDimension(false);
  449. modelxsize = tflite->ReadInputDimenstion(0);
  450. modelysize = tflite->ReadInputDimenstion(1);
  451. modelchannel = tflite->ReadInputDimenstion(2);
  452. int _anzoutputdimensions = tflite->GetAnzOutPut();
  453. switch (_anzoutputdimensions)
  454. {
  455. case 2:
  456. CNNType = Analogue;
  457. printf("TFlite-Type set to Analogue\n");
  458. break;
  459. case 10:
  460. CNNType = DoubleHyprid10;
  461. printf("TFlite-Type set to DoubleHyprid10\n");
  462. break;
  463. case 11:
  464. CNNType = Digital;
  465. printf("TFlite-Type set to Digital\n");
  466. break;
  467. case 20:
  468. CNNType = DigitalHyprid10;
  469. printf("TFlite-Type set to DigitalHyprid10\n");
  470. break;
  471. // case 22:
  472. // CNNType = DigitalHyprid;
  473. // printf("TFlite-Type set to DigitalHyprid\n");
  474. // break;
  475. case 100:
  476. if (modelxsize==32 && modelysize == 32) {
  477. CNNType = Analogue100;
  478. printf("TFlite-Type set to Analogue100\n");
  479. } else {
  480. CNNType = Digital100;
  481. printf("TFlite-Type set to Digital\n");
  482. }
  483. break;
  484. default:
  485. printf("ERROR ERROR ERROR - tflite passt nicht zur Firmware - ERROR ERROR ERROR\n");
  486. }
  487. }
  488. delete tflite;
  489. return true;
  490. }
  491. bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
  492. {
  493. if (disabled)
  494. return true;
  495. string logPath = CreateLogFolder(time);
  496. CTfLiteClass *tflite = new CTfLiteClass;
  497. string zwcnn = "/sdcard" + cnnmodelfile;
  498. zwcnn = FormatFileName(zwcnn);
  499. printf(zwcnn.c_str());printf("\n");
  500. if (!tflite->LoadModel(zwcnn)) {
  501. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  502. LogFile.WriteToFile("Cannot load model");
  503. delete tflite;
  504. return false;
  505. }
  506. tflite->MakeAllocate();
  507. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  508. {
  509. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  510. {
  511. printf("General %d - TfLite\n", i);
  512. switch (CNNType) {
  513. case Analogue:
  514. {
  515. float f1, f2;
  516. f1 = 0; f2 = 0;
  517. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  518. tflite->Invoke();
  519. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  520. f1 = tflite->GetOutputValue(0);
  521. f2 = tflite->GetOutputValue(1);
  522. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  523. GENERAL[_ana]->ROI[i]->result_float = result * 10;
  524. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  525. if (isLogImage)
  526. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  527. } break;
  528. case Digital:
  529. {
  530. GENERAL[_ana]->ROI[i]->result_klasse = 0;
  531. GENERAL[_ana]->ROI[i]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[_ana]->ROI[i]->image);
  532. printf("Result General(Digit)%i: %d\n", i, GENERAL[_ana]->ROI[i]->result_klasse);
  533. if (isLogImage)
  534. {
  535. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  536. if (isLogImageSelect)
  537. {
  538. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  539. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  540. }
  541. else
  542. {
  543. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  544. }
  545. }
  546. } break;
  547. /*
  548. case DigitalHyprid:
  549. {
  550. int _num, _nachkomma;
  551. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  552. tflite->Invoke();
  553. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  554. _num = tflite->GetOutClassification(0, 10);
  555. _nachkomma = tflite->GetOutClassification(11, 21);
  556. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  557. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  558. if ((_num == 10) || (_nachkomma == 10)) // NaN detektiert
  559. GENERAL[_ana]->ROI[i]->result_float = -1;
  560. else
  561. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  562. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  563. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  564. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  565. if (isLogImage)
  566. {
  567. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  568. if (isLogImageSelect)
  569. {
  570. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  571. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  572. }
  573. else
  574. {
  575. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  576. }
  577. }
  578. } break;
  579. */
  580. case DigitalHyprid10:
  581. {
  582. int _num, _nachkomma;
  583. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  584. tflite->Invoke();
  585. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  586. _num = tflite->GetOutClassification(0, 9);
  587. _nachkomma = tflite->GetOutClassification(10, 19);
  588. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  589. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  590. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  591. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  592. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  593. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  594. if (isLogImage)
  595. {
  596. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  597. if (isLogImageSelect)
  598. {
  599. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  600. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  601. }
  602. else
  603. {
  604. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  605. }
  606. }
  607. } break;
  608. case DoubleHyprid10:
  609. {
  610. int _num, _numplus, _numminus;
  611. float _val, _valplus, _valminus;
  612. float _fit;
  613. float _result_save_file;
  614. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  615. tflite->Invoke();
  616. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  617. _num = tflite->GetOutClassification(0, 9);
  618. _numplus = (_num + 1) % 10;
  619. _numminus = (_num - 1 + 10) % 10;
  620. _val = tflite->GetOutputValue(_num);
  621. _valplus = tflite->GetOutputValue(_numplus);
  622. _valminus = tflite->GetOutputValue(_numminus);
  623. float result = _num;
  624. if (_valplus > _valminus)
  625. {
  626. result = result + _valplus / (_valplus + _val);
  627. _fit = _val + _valplus;
  628. }
  629. else
  630. {
  631. result = result - _valminus / (_val + _valminus);
  632. _fit = _val + _valminus;
  633. }
  634. if (result >= 10)
  635. result = result - 10;
  636. if (result < 0)
  637. result = result + 10;
  638. string zw = "_num (p, m): " + to_string(_num) + " " + to_string(_numplus) + " " + to_string(_numminus);
  639. zw = zw + " _val (p, m): " + to_string(_val) + " " + to_string(_valplus) + " " + to_string(_valminus);
  640. zw = zw + " result: " + to_string(result) + " _fit: " + to_string(_fit);
  641. printf("details cnn: %s\n", zw.c_str());
  642. LogFile.WriteToFile(zw);
  643. _result_save_file = result;
  644. if (_fit < CNNGoodThreshold)
  645. {
  646. GENERAL[_ana]->ROI[i]->isReject = true;
  647. result = -1;
  648. _result_save_file+= 100; // Für den Fall, dass fit nicht ausreichend, soll trotzdem das Ergebnis mit "-10x.y" abgespeichert werden.
  649. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold);
  650. printf("Value Rejected due to Threshold (Fit: %f, Threshold: %f\n", _fit, CNNGoodThreshold);
  651. LogFile.WriteToFile(zw);
  652. }
  653. else
  654. {
  655. GENERAL[_ana]->ROI[i]->isReject = false;
  656. }
  657. GENERAL[_ana]->ROI[i]->result_float = result;
  658. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  659. if (isLogImage)
  660. {
  661. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  662. if (isLogImageSelect)
  663. {
  664. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  665. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  666. }
  667. else
  668. {
  669. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  670. }
  671. }
  672. }
  673. break;
  674. case Digital100:
  675. case Analogue100:
  676. {
  677. int _num;
  678. float _fit;
  679. float _result_save_file;
  680. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  681. tflite->Invoke();
  682. _num = tflite->GetOutClassification();
  683. _fit = tflite->GetOutputValue(_num);
  684. GENERAL[_ana]->ROI[i]->result_float = (float)_num / 10.0;
  685. _result_save_file = GENERAL[_ana]->ROI[i]->result_float;
  686. if (_fit < CNNGoodThreshold)
  687. {
  688. GENERAL[_ana]->ROI[i]->isReject = true;
  689. GENERAL[_ana]->ROI[i]->result_float = -1;
  690. _result_save_file+= 100; // Für den Fall, dass fit nicht ausreichend, soll trotzdem das Ergebnis mit "-10x.y" abgespeichert werden.
  691. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold);
  692. printf("Value Rejected due to Threshold (Fit: %f, Threshold: %f\n", _fit, CNNGoodThreshold);
  693. LogFile.WriteToFile(zw);
  694. }
  695. else
  696. {
  697. GENERAL[_ana]->ROI[i]->isReject = false;
  698. }
  699. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  700. if (isLogImage)
  701. {
  702. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  703. if (isLogImageSelect)
  704. {
  705. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  706. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  707. }
  708. else
  709. {
  710. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  711. }
  712. }
  713. } break;
  714. default:
  715. break;
  716. }
  717. }
  718. }
  719. delete tflite;
  720. return true;
  721. }
  722. bool ClassFlowCNNGeneral::isExtendedResolution(int _number)
  723. {
  724. if (!(CNNType == Digital))
  725. return true;
  726. return false;
  727. }
  728. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo()
  729. {
  730. std::vector<HTMLInfo*> result;
  731. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  732. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  733. {
  734. if (GENERAL[_ana]->name == "default")
  735. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  736. else
  737. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  738. HTMLInfo *zw = new HTMLInfo;
  739. if (GENERAL[_ana]->name == "default")
  740. {
  741. zw->filename = GENERAL[_ana]->ROI[i]->name + ".bmp";
  742. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  743. }
  744. else
  745. {
  746. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp";
  747. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  748. }
  749. if (CNNType == Digital)
  750. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  751. else
  752. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  753. zw->image = GENERAL[_ana]->ROI[i]->image;
  754. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  755. result.push_back(zw);
  756. }
  757. return result;
  758. }
  759. int ClassFlowCNNGeneral::getAnzahlGENERAL()
  760. {
  761. return GENERAL.size();
  762. }
  763. string ClassFlowCNNGeneral::getNameGENERAL(int _analog)
  764. {
  765. if (_analog < GENERAL.size())
  766. return GENERAL[_analog]->name;
  767. return "GENERAL DOES NOT EXIST";
  768. }
  769. general* ClassFlowCNNGeneral::GetGENERAL(int _analog)
  770. {
  771. if (_analog < GENERAL.size())
  772. return GENERAL[_analog];
  773. return NULL;
  774. }
  775. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers)
  776. {
  777. for (int _dig = 0; _dig < GENERAL.size(); _dig++)
  778. {
  779. std::string _name = GENERAL[_dig]->name;
  780. bool found = false;
  781. for (int i = 0; i < (*_name_numbers).size(); ++i)
  782. {
  783. if ((*_name_numbers)[i] == _name)
  784. found = true;
  785. }
  786. if (!found)
  787. (*_name_numbers).push_back(_name);
  788. }
  789. }