ClassFlowCNNGeneral.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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)
  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. if (ergebnis_nachkomma > 5)
  172. return ergebnis_vorkomma;
  173. else
  174. return (ergebnis_vorkomma - 1 + 10) % 10;
  175. }
  176. }
  177. return -1;
  178. /*
  179. if (zahl_vorgaenger > 9.2) // Ziffernwechsel beginnt
  180. {
  181. if (eval_vorgaenger == 0) // Wechsel hat schon stattgefunden
  182. {
  183. return ((int) round(zahl) + 10) % 10; // Annahme, dass die neue Zahl schon in der Nähe des Ziels ist
  184. }
  185. else
  186. {
  187. if (zahl_vorgaenger <= 9.5) // Wechsel startet gerade, aber beginnt erst
  188. {
  189. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  190. return ((int) round(zahl) + 10) % 10;
  191. else
  192. return ((int) trunc(zahl) + 10) % 10;
  193. }
  194. else
  195. {
  196. return ((int) trunc(zahl) + 10) % 10; // Wechsel schon weiter fortgeschritten, d.h. über 2 als Nachkomma
  197. }
  198. }
  199. }
  200. if ((ergebnis_nachkomma <= 2) || (ergebnis_nachkomma >= 8)) // Band um die Ziffer --> Runden, da Ziffer im Rahmen Ungenauigkeit erreicht
  201. return ((int) round(zahl) + 10) % 10;
  202. return ((int) trunc(zahl) + 10) % 10;
  203. */
  204. }
  205. int ClassFlowCNNGeneral::ZeigerEval(float zahl, int ziffer_vorgaenger)
  206. {
  207. int ergebnis_nachkomma = ((int) floor(zahl * 10) + 10) % 10;
  208. int ergebnis_vorkomma = ((int) floor(zahl) + 10) % 10;
  209. int ergebnis, ergebnis_rating;
  210. 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));
  211. if (ziffer_vorgaenger == -1)
  212. return ergebnis_vorkomma % 10;
  213. // Ist die aktuelle Stelle schon umgesprungen und die Vorstelle noch nicht?
  214. // Akt.: 2.1, Vorstelle = 0.9 => 1.9
  215. ergebnis_rating = ergebnis_nachkomma - ziffer_vorgaenger;
  216. if (ergebnis_nachkomma >= 5)
  217. ergebnis_rating-=5;
  218. else
  219. ergebnis_rating+=5;
  220. ergebnis = (int) round(zahl);
  221. if (ergebnis_rating < 0)
  222. ergebnis-=1;
  223. if (ergebnis == -1)
  224. ergebnis+=10;
  225. ergebnis = (ergebnis + 10) % 10;
  226. return ergebnis;
  227. }
  228. bool ClassFlowCNNGeneral::ReadParameter(FILE* pfile, string& aktparamgraph)
  229. {
  230. std::vector<string> zerlegt;
  231. aktparamgraph = trim(aktparamgraph);
  232. if (aktparamgraph.size() == 0)
  233. if (!this->GetNextParagraph(pfile, aktparamgraph))
  234. return false;
  235. if ((toUpper(aktparamgraph) != "[ANALOG]") && (toUpper(aktparamgraph) != ";[ANALOG]")
  236. && (toUpper(aktparamgraph) != "[DIGIT]") && (toUpper(aktparamgraph) != ";[DIGIT]")
  237. && (toUpper(aktparamgraph) != "[DIGITS]") && (toUpper(aktparamgraph) != ";[DIGITS]")
  238. ) // Paragraph passt nicht
  239. return false;
  240. if (aktparamgraph[0] == ';')
  241. {
  242. disabled = true;
  243. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  244. printf("[Analog/Digit] is disabled !!!\n");
  245. return true;
  246. }
  247. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  248. {
  249. zerlegt = this->ZerlegeZeile(aktparamgraph);
  250. if ((toUpper(zerlegt[0]) == "LOGIMAGELOCATION") && (zerlegt.size() > 1))
  251. {
  252. this->LogImageLocation = "/sdcard" + zerlegt[1];
  253. this->isLogImage = true;
  254. }
  255. if ((toUpper(zerlegt[0]) == "LOGIMAGESELECT") && (zerlegt.size() > 1))
  256. {
  257. LogImageSelect = zerlegt[1];
  258. isLogImageSelect = true;
  259. }
  260. if ((toUpper(zerlegt[0]) == "LOGFILERETENTIONINDAYS") && (zerlegt.size() > 1))
  261. {
  262. this->logfileRetentionInDays = std::stoi(zerlegt[1]);
  263. }
  264. // if ((toUpper(zerlegt[0]) == "MODELTYPE") && (zerlegt.size() > 1))
  265. // {
  266. // if (toUpper(zerlegt[1]) == "DIGITHYPRID")
  267. // CNNType = DigitalHyprid;
  268. // }
  269. if ((toUpper(zerlegt[0]) == "MODEL") && (zerlegt.size() > 1))
  270. {
  271. this->cnnmodelfile = zerlegt[1];
  272. }
  273. if ((toUpper(zerlegt[0]) == "CNNGOODTHRESHOLD") && (zerlegt.size() > 1))
  274. {
  275. CNNGoodThreshold = std::stof(zerlegt[1]);
  276. }
  277. if (zerlegt.size() >= 5)
  278. {
  279. general* _analog = GetGENERAL(zerlegt[0], true);
  280. roi* neuroi = _analog->ROI[_analog->ROI.size()-1];
  281. neuroi->posx = std::stoi(zerlegt[1]);
  282. neuroi->posy = std::stoi(zerlegt[2]);
  283. neuroi->deltax = std::stoi(zerlegt[3]);
  284. neuroi->deltay = std::stoi(zerlegt[4]);
  285. neuroi->result_float = -1;
  286. neuroi->image = NULL;
  287. neuroi->image_org = NULL;
  288. }
  289. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  290. {
  291. if (toUpper(zerlegt[1]) == "TRUE")
  292. SaveAllFiles = true;
  293. }
  294. }
  295. if (!getNetworkParameter())
  296. return false;
  297. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  298. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  299. {
  300. GENERAL[_ana]->ROI[i]->image = new CImageBasis(modelxsize, modelysize, modelchannel);
  301. GENERAL[_ana]->ROI[i]->image_org = new CImageBasis(GENERAL[_ana]->ROI[i]->deltax, GENERAL[_ana]->ROI[i]->deltay, 3);
  302. }
  303. return true;
  304. }
  305. general* ClassFlowCNNGeneral::FindGENERAL(string _name_number)
  306. {
  307. for (int i = 0; i < GENERAL.size(); ++i)
  308. if (GENERAL[i]->name == _name_number)
  309. return GENERAL[i];
  310. return NULL;
  311. }
  312. general* ClassFlowCNNGeneral::GetGENERAL(string _name, bool _create = true)
  313. {
  314. string _analog, _roi;
  315. int _pospunkt = _name.find_first_of(".");
  316. if (_pospunkt > -1)
  317. {
  318. _analog = _name.substr(0, _pospunkt);
  319. _roi = _name.substr(_pospunkt+1, _name.length() - _pospunkt - 1);
  320. }
  321. else
  322. {
  323. _analog = "default";
  324. _roi = _name;
  325. }
  326. general *_ret = NULL;
  327. for (int i = 0; i < GENERAL.size(); ++i)
  328. if (GENERAL[i]->name == _analog)
  329. _ret = GENERAL[i];
  330. if (!_create) // nicht gefunden und soll auch nicht erzeugt werden
  331. return _ret;
  332. if (_ret == NULL)
  333. {
  334. _ret = new general;
  335. _ret->name = _analog;
  336. GENERAL.push_back(_ret);
  337. }
  338. roi* neuroi = new roi;
  339. neuroi->name = _roi;
  340. _ret->ROI.push_back(neuroi);
  341. printf("GetGENERAL - GENERAL %s - roi %s\n", _analog.c_str(), _roi.c_str());
  342. return _ret;
  343. }
  344. string ClassFlowCNNGeneral::getHTMLSingleStep(string host)
  345. {
  346. string result, zw;
  347. std::vector<HTMLInfo*> htmlinfo;
  348. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  349. result = result + "Analog Pointers: <p> ";
  350. htmlinfo = GetHTMLInfo();
  351. for (int i = 0; i < htmlinfo.size(); ++i)
  352. {
  353. std::stringstream stream;
  354. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  355. zw = stream.str();
  356. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  357. delete htmlinfo[i];
  358. }
  359. htmlinfo.clear();
  360. return result;
  361. }
  362. bool ClassFlowCNNGeneral::doFlow(string time)
  363. {
  364. if (disabled)
  365. return true;
  366. if (!doAlignAndCut(time)){
  367. return false;
  368. };
  369. if (debugdetailgeneral) LogFile.WriteToFile("ClassFlowCNNGeneral::doFlow nach Alignment");
  370. doNeuralNetwork(time);
  371. RemoveOldLogs();
  372. return true;
  373. }
  374. bool ClassFlowCNNGeneral::doAlignAndCut(string time)
  375. {
  376. if (disabled)
  377. return true;
  378. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  379. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  380. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  381. {
  382. printf("General %d - Align&Cut\n", i);
  383. 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);
  384. if (SaveAllFiles)
  385. {
  386. if (GENERAL[_ana]->name == "default")
  387. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  388. else
  389. GENERAL[_ana]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg"));
  390. }
  391. GENERAL[_ana]->ROI[i]->image_org->Resize(modelxsize, modelysize, GENERAL[_ana]->ROI[i]->image);
  392. if (SaveAllFiles)
  393. {
  394. if (GENERAL[_ana]->name == "default")
  395. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  396. else
  397. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  398. }
  399. }
  400. return true;
  401. }
  402. void ClassFlowCNNGeneral::DrawROI(CImageBasis *_zw)
  403. {
  404. if (CNNType == Analogue)
  405. {
  406. int r = 0;
  407. int g = 255;
  408. int b = 0;
  409. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  410. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  411. {
  412. _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);
  413. _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);
  414. _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);
  415. _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);
  416. }
  417. }
  418. else
  419. {
  420. for (int _dig = 0; _dig < GENERAL.size(); ++_dig)
  421. for (int i = 0; i < GENERAL[_dig]->ROI.size(); ++i)
  422. _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);
  423. }
  424. }
  425. bool ClassFlowCNNGeneral::getNetworkParameter()
  426. {
  427. if (disabled)
  428. return true;
  429. CTfLiteClass *tflite = new CTfLiteClass;
  430. string zwcnn = "/sdcard" + cnnmodelfile;
  431. zwcnn = FormatFileName(zwcnn);
  432. printf(zwcnn.c_str());printf("\n");
  433. if (!tflite->LoadModel(zwcnn)) {
  434. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  435. LogFile.WriteToFile("Cannot load model");
  436. delete tflite;
  437. return false;
  438. }
  439. tflite->MakeAllocate();
  440. if (CNNType == AutoDetect)
  441. {
  442. tflite->GetInputDimension(false);
  443. modelxsize = tflite->ReadInputDimenstion(0);
  444. modelysize = tflite->ReadInputDimenstion(1);
  445. modelchannel = tflite->ReadInputDimenstion(2);
  446. int _anzoutputdimensions = tflite->GetAnzOutPut();
  447. switch (_anzoutputdimensions)
  448. {
  449. case 2:
  450. CNNType = Analogue;
  451. printf("TFlite-Type set to Analogue\n");
  452. break;
  453. case 10:
  454. CNNType = DoubleHyprid10;
  455. printf("TFlite-Type set to DoubleHyprid10\n");
  456. break;
  457. case 11:
  458. CNNType = Digital;
  459. printf("TFlite-Type set to Digital\n");
  460. break;
  461. case 20:
  462. CNNType = DigitalHyprid10;
  463. printf("TFlite-Type set to DigitalHyprid10\n");
  464. break;
  465. // case 22:
  466. // CNNType = DigitalHyprid;
  467. // printf("TFlite-Type set to DigitalHyprid\n");
  468. // break;
  469. case 100:
  470. CNNType = Digital100;
  471. printf("TFlite-Type set to Digital\n");
  472. break;
  473. default:
  474. printf("ERROR ERROR ERROR - tflite passt nicht zur Firmware - ERROR ERROR ERROR\n");
  475. }
  476. }
  477. delete tflite;
  478. return true;
  479. }
  480. bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
  481. {
  482. if (disabled)
  483. return true;
  484. string logPath = CreateLogFolder(time);
  485. CTfLiteClass *tflite = new CTfLiteClass;
  486. string zwcnn = "/sdcard" + cnnmodelfile;
  487. zwcnn = FormatFileName(zwcnn);
  488. printf(zwcnn.c_str());printf("\n");
  489. if (!tflite->LoadModel(zwcnn)) {
  490. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  491. LogFile.WriteToFile("Cannot load model");
  492. delete tflite;
  493. return false;
  494. }
  495. tflite->MakeAllocate();
  496. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  497. {
  498. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  499. {
  500. printf("General %d - TfLite\n", i);
  501. switch (CNNType) {
  502. case Analogue:
  503. {
  504. float f1, f2;
  505. f1 = 0; f2 = 0;
  506. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  507. tflite->Invoke();
  508. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  509. f1 = tflite->GetOutputValue(0);
  510. f2 = tflite->GetOutputValue(1);
  511. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  512. GENERAL[_ana]->ROI[i]->result_float = result * 10;
  513. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  514. if (isLogImage)
  515. LogImage(logPath, GENERAL[_ana]->ROI[i]->name, &GENERAL[_ana]->ROI[i]->result_float, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  516. } break;
  517. case Digital:
  518. {
  519. GENERAL[_ana]->ROI[i]->result_klasse = 0;
  520. GENERAL[_ana]->ROI[i]->result_klasse = tflite->GetClassFromImageBasis(GENERAL[_ana]->ROI[i]->image);
  521. printf("Result General(Digit)%i: %d\n", i, GENERAL[_ana]->ROI[i]->result_klasse);
  522. if (isLogImage)
  523. {
  524. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  525. if (isLogImageSelect)
  526. {
  527. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  528. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  529. }
  530. else
  531. {
  532. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  533. }
  534. }
  535. } break;
  536. /*
  537. case DigitalHyprid:
  538. {
  539. int _num, _nachkomma;
  540. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  541. tflite->Invoke();
  542. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  543. _num = tflite->GetOutClassification(0, 10);
  544. _nachkomma = tflite->GetOutClassification(11, 21);
  545. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  546. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  547. if ((_num == 10) || (_nachkomma == 10)) // NaN detektiert
  548. GENERAL[_ana]->ROI[i]->result_float = -1;
  549. else
  550. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  551. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  552. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  553. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  554. if (isLogImage)
  555. {
  556. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  557. if (isLogImageSelect)
  558. {
  559. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  560. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  561. }
  562. else
  563. {
  564. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  565. }
  566. }
  567. } break;
  568. */
  569. case DigitalHyprid10:
  570. {
  571. int _num, _nachkomma;
  572. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  573. tflite->Invoke();
  574. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  575. _num = tflite->GetOutClassification(0, 9);
  576. _nachkomma = tflite->GetOutClassification(10, 19);
  577. string _zwres = "Nach Invoke - Nummer: " + to_string(_num) + " Nachkomma: " + to_string(_nachkomma);
  578. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  579. GENERAL[_ana]->ROI[i]->result_float = fmod((double) _num + (((double)_nachkomma)-5)/10 + (double) 10, 10);
  580. printf("Result General(DigitalHyprid)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  581. _zwres = "Result General(DigitalHyprid)" + to_string(i) + ": " + to_string(GENERAL[_ana]->ROI[i]->result_float);
  582. if (debugdetailgeneral) LogFile.WriteToFile(_zwres);
  583. if (isLogImage)
  584. {
  585. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  586. if (isLogImageSelect)
  587. {
  588. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  589. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  590. }
  591. else
  592. {
  593. LogImage(logPath, _imagename, NULL, &GENERAL[_ana]->ROI[i]->result_klasse, time, GENERAL[_ana]->ROI[i]->image_org);
  594. }
  595. }
  596. } break;
  597. case DoubleHyprid10:
  598. {
  599. int _num, _numplus, _numminus;
  600. float _val, _valplus, _valminus;
  601. float _fit;
  602. float _result_save_file;
  603. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  604. tflite->Invoke();
  605. if (debugdetailgeneral) LogFile.WriteToFile("Nach Invoke");
  606. _num = tflite->GetOutClassification(0, 9);
  607. _numplus = (_num + 1) % 10;
  608. _numminus = (_num - 1 + 10) % 10;
  609. _val = tflite->GetOutputValue(_num);
  610. _valplus = tflite->GetOutputValue(_numplus);
  611. _valminus = tflite->GetOutputValue(_numminus);
  612. float result = _num;
  613. if (_valplus > _valminus)
  614. {
  615. result = result + _valplus / (_valplus + _val);
  616. _fit = _val + _valplus;
  617. }
  618. else
  619. {
  620. result = result - _valminus / (_val + _valminus);
  621. _fit = _val + _valminus;
  622. }
  623. if (result > 10)
  624. result = result - 10;
  625. if (result < 0)
  626. result = result + 10;
  627. string zw = "_num (p, m): " + to_string(_num) + " " + to_string(_numplus) + " " + to_string(_numminus);
  628. zw = zw + " _val (p, m): " + to_string(_val) + " " + to_string(_valplus) + " " + to_string(_valminus);
  629. zw = zw + " result: " + to_string(result) + " _fit: " + to_string(_fit);
  630. printf("details cnn: %s\n", zw.c_str());
  631. LogFile.WriteToFile(zw);
  632. _result_save_file = result;
  633. if (_fit < CNNGoodThreshold)
  634. {
  635. GENERAL[_ana]->ROI[i]->isReject = true;
  636. result = -1;
  637. _result_save_file+= 100; // Für den Fall, dass fit nicht ausreichend, soll trotzdem das Ergebnis mit "-10x.y" abgespeichert werden.
  638. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold);
  639. printf("Value Rejected due to Threshold (Fit: %f, Threshold: %f\n", _fit, CNNGoodThreshold);
  640. LogFile.WriteToFile(zw);
  641. }
  642. else
  643. {
  644. GENERAL[_ana]->ROI[i]->isReject = false;
  645. }
  646. GENERAL[_ana]->ROI[i]->result_float = result;
  647. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  648. if (isLogImage)
  649. {
  650. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  651. if (isLogImageSelect)
  652. {
  653. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  654. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  655. }
  656. else
  657. {
  658. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  659. }
  660. }
  661. }
  662. break;
  663. case Digital100:
  664. {
  665. int _num;
  666. float _fit;
  667. float _result_save_file;
  668. tflite->LoadInputImageBasis(GENERAL[_ana]->ROI[i]->image);
  669. tflite->Invoke();
  670. _num = tflite->GetOutClassification();
  671. _fit = tflite->GetOutputValue(_num);
  672. GENERAL[_ana]->ROI[i]->result_float = (float)_num / 10.0;
  673. _result_save_file = GENERAL[_ana]->ROI[i]->result_float;
  674. if (_fit < CNNGoodThreshold)
  675. {
  676. GENERAL[_ana]->ROI[i]->isReject = true;
  677. GENERAL[_ana]->ROI[i]->result_float = -1;
  678. _result_save_file+= 100; // Für den Fall, dass fit nicht ausreichend, soll trotzdem das Ergebnis mit "-10x.y" abgespeichert werden.
  679. string zw = "Value Rejected due to Threshold (Fit: " + to_string(_fit) + "Threshold: " + to_string(CNNGoodThreshold);
  680. printf("Value Rejected due to Threshold (Fit: %f, Threshold: %f\n", _fit, CNNGoodThreshold);
  681. LogFile.WriteToFile(zw);
  682. }
  683. else
  684. {
  685. GENERAL[_ana]->ROI[i]->isReject = false;
  686. }
  687. printf("Result General(Analog)%i: %f\n", i, GENERAL[_ana]->ROI[i]->result_float);
  688. if (isLogImage)
  689. {
  690. string _imagename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name;
  691. if (isLogImageSelect)
  692. {
  693. if (LogImageSelect.find(GENERAL[_ana]->ROI[i]->name) != std::string::npos)
  694. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  695. }
  696. else
  697. {
  698. LogImage(logPath, _imagename, &_result_save_file, NULL, time, GENERAL[_ana]->ROI[i]->image_org);
  699. }
  700. }
  701. } break;
  702. default:
  703. break;
  704. }
  705. }
  706. }
  707. delete tflite;
  708. return true;
  709. }
  710. bool ClassFlowCNNGeneral::isExtendedResolution(int _number)
  711. {
  712. if (!(CNNType == Digital))
  713. return true;
  714. return false;
  715. }
  716. std::vector<HTMLInfo*> ClassFlowCNNGeneral::GetHTMLInfo()
  717. {
  718. std::vector<HTMLInfo*> result;
  719. for (int _ana = 0; _ana < GENERAL.size(); ++_ana)
  720. for (int i = 0; i < GENERAL[_ana]->ROI.size(); ++i)
  721. {
  722. if (GENERAL[_ana]->name == "default")
  723. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  724. else
  725. GENERAL[_ana]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp"));
  726. HTMLInfo *zw = new HTMLInfo;
  727. if (GENERAL[_ana]->name == "default")
  728. {
  729. zw->filename = GENERAL[_ana]->ROI[i]->name + ".bmp";
  730. zw->filename_org = GENERAL[_ana]->ROI[i]->name + ".jpg";
  731. }
  732. else
  733. {
  734. zw->filename = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".bmp";
  735. zw->filename_org = GENERAL[_ana]->name + "_" + GENERAL[_ana]->ROI[i]->name + ".jpg";
  736. }
  737. if (CNNType == Digital)
  738. zw->val = GENERAL[_ana]->ROI[i]->result_klasse;
  739. else
  740. zw->val = GENERAL[_ana]->ROI[i]->result_float;
  741. zw->image = GENERAL[_ana]->ROI[i]->image;
  742. zw->image_org = GENERAL[_ana]->ROI[i]->image_org;
  743. result.push_back(zw);
  744. }
  745. return result;
  746. }
  747. int ClassFlowCNNGeneral::getAnzahlGENERAL()
  748. {
  749. return GENERAL.size();
  750. }
  751. string ClassFlowCNNGeneral::getNameGENERAL(int _analog)
  752. {
  753. if (_analog < GENERAL.size())
  754. return GENERAL[_analog]->name;
  755. return "GENERAL DOES NOT EXIST";
  756. }
  757. general* ClassFlowCNNGeneral::GetGENERAL(int _analog)
  758. {
  759. if (_analog < GENERAL.size())
  760. return GENERAL[_analog];
  761. return NULL;
  762. }
  763. void ClassFlowCNNGeneral::UpdateNameNumbers(std::vector<std::string> *_name_numbers)
  764. {
  765. for (int _dig = 0; _dig < GENERAL.size(); _dig++)
  766. {
  767. std::string _name = GENERAL[_dig]->name;
  768. bool found = false;
  769. for (int i = 0; i < (*_name_numbers).size(); ++i)
  770. {
  771. if ((*_name_numbers)[i] == _name)
  772. found = true;
  773. }
  774. if (!found)
  775. (*_name_numbers).push_back(_name);
  776. }
  777. }