ClassFlowDigit.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include "ClassFlowDigit.h"
  2. //#include "CFindTemplate.h"
  3. //#include "CTfLiteClass.h"
  4. // #define OHNETFLITE
  5. #ifndef OHNETFLITE
  6. #include "CTfLiteClass.h"
  7. #endif
  8. // #include "bitmap_image.hpp"
  9. #include "ClassLogFile.h"
  10. static const char* TAG = "flow_digital";
  11. void ClassFlowDigit::SetInitialParameter(void)
  12. {
  13. string cnnmodelfile = "";
  14. modelxsize = 1;
  15. modelysize = 1;
  16. ListFlowControll = NULL;
  17. previousElement = NULL;
  18. SaveAllFiles = false;
  19. disabled = false;
  20. DecimalShift = 0;
  21. DecimalShiftEnabled = false;
  22. isLogImageSelect = false;
  23. }
  24. ClassFlowDigit::ClassFlowDigit() : ClassFlowImage(TAG)
  25. {
  26. SetInitialParameter();
  27. }
  28. ClassFlowDigit::ClassFlowDigit(std::vector<ClassFlow*>* lfc) : ClassFlowImage(lfc, TAG)
  29. {
  30. SetInitialParameter();
  31. ListFlowControll = lfc;
  32. for (int i = 0; i < ListFlowControll->size(); ++i)
  33. {
  34. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  35. {
  36. flowpostalignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
  37. }
  38. }
  39. }
  40. ClassFlowDigit::ClassFlowDigit(std::vector<ClassFlow*>* lfc, ClassFlow *_prev) : ClassFlowImage(lfc, _prev, TAG)
  41. {
  42. SetInitialParameter();
  43. ListFlowControll = lfc;
  44. previousElement = _prev;
  45. for (int i = 0; i < ListFlowControll->size(); ++i)
  46. {
  47. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  48. {
  49. flowpostalignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
  50. }
  51. }
  52. }
  53. string ClassFlowDigit::getReadout(int _digit = 0)
  54. {
  55. string rst = "";
  56. for (int i = 0; i < DIGIT[_digit]->ROI.size(); ++i)
  57. {
  58. if (DIGIT[_digit]->ROI[i]->resultklasse == 10)
  59. rst = rst + "N";
  60. else
  61. rst = rst + std::to_string(DIGIT[_digit]->ROI[i]->resultklasse);
  62. }
  63. return rst;
  64. }
  65. bool ClassFlowDigit::ReadParameter(FILE* pfile, string& aktparamgraph)
  66. {
  67. std::vector<string> zerlegt;
  68. aktparamgraph = trim(aktparamgraph);
  69. if (aktparamgraph.size() == 0)
  70. if (!this->GetNextParagraph(pfile, aktparamgraph))
  71. return false;
  72. printf("aktparamgraph: %s\n", aktparamgraph.c_str());
  73. if ((aktparamgraph.compare(0, 7, "[Digits") != 0) && (aktparamgraph.compare(0, 8, ";[Digits") != 0)) // Paragraph passt nich zu MakeImage
  74. return false;
  75. int _pospkt = aktparamgraph.find_first_of(".");
  76. int _posklammerzu = aktparamgraph.find_first_of("]");
  77. if (_pospkt > -1)
  78. NameDigit = aktparamgraph.substr(_pospkt+1, _posklammerzu - _pospkt-1);
  79. else
  80. NameDigit = "";
  81. printf("Name Digit: %s\n", NameDigit.c_str());
  82. if (aktparamgraph[0] == ';')
  83. {
  84. disabled = true;
  85. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  86. printf("[Digits] is disabled !!!\n");
  87. return true;
  88. }
  89. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph))
  90. {
  91. zerlegt = this->ZerlegeZeile(aktparamgraph);
  92. if ((zerlegt[0] == "LogImageLocation") && (zerlegt.size() > 1))
  93. {
  94. LogImageLocation = "/sdcard" + zerlegt[1];
  95. isLogImage = true;
  96. }
  97. if ((zerlegt[0] == "LogImageSelect") && (zerlegt.size() > 1))
  98. {
  99. LogImageSelect = zerlegt[1];
  100. isLogImageSelect = true;
  101. }
  102. if ((zerlegt[0] == "Model") && (zerlegt.size() > 1))
  103. {
  104. cnnmodelfile = zerlegt[1];
  105. }
  106. if ((zerlegt[0] == "ModelInputSize") && (zerlegt.size() > 2))
  107. {
  108. modelxsize = std::stoi(zerlegt[1]);
  109. modelysize = std::stoi(zerlegt[2]);
  110. }
  111. if (zerlegt.size() >= 5)
  112. {
  113. digit* _digit = GetDIGIT(zerlegt[0], true);
  114. roi* neuroi = _digit->ROI[_digit->ROI.size()-1];
  115. neuroi->posx = std::stoi(zerlegt[1]);
  116. neuroi->posy = std::stoi(zerlegt[2]);
  117. neuroi->deltax = std::stoi(zerlegt[3]);
  118. neuroi->deltay = std::stoi(zerlegt[4]);
  119. neuroi->resultklasse = -1;
  120. neuroi->image = NULL;
  121. neuroi->image_org = NULL;
  122. }
  123. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  124. {
  125. if (toUpper(zerlegt[1]) == "TRUE")
  126. SaveAllFiles = true;
  127. }
  128. }
  129. for (int _dig = 0; _dig < DIGIT.size(); ++_dig)
  130. for (int i = 0; i < DIGIT[_dig]->ROI.size(); ++i)
  131. {
  132. DIGIT[_dig]->ROI[i]->image = new CImageBasis(modelxsize, modelysize, 3);
  133. DIGIT[_dig]->ROI[i]->image_org = new CImageBasis(DIGIT[_dig]->ROI[i]->deltax, DIGIT[_dig]->ROI[i]->deltay, 3);
  134. }
  135. return true;
  136. }
  137. digit* ClassFlowDigit::FindDIGIT(string _name_number)
  138. {
  139. for (int i = 0; i < DIGIT.size(); ++i)
  140. {
  141. if (DIGIT[i]->name == _name_number)
  142. return DIGIT[i];
  143. }
  144. return NULL;
  145. }
  146. digit* ClassFlowDigit::GetDIGIT(string _name, bool _create = true)
  147. {
  148. string _digit, _roi;
  149. int _pospunkt = _name.find_first_of(".");
  150. // printf("Name: %s, Pospunkt: %d\n", _name.c_str(), _pospunkt);
  151. if (_pospunkt > -1)
  152. {
  153. _digit = _name.substr(0, _pospunkt);
  154. _roi = _name.substr(_pospunkt+1, _name.length() - _pospunkt - 1);
  155. }
  156. else
  157. {
  158. _digit = "default";
  159. _roi = _name;
  160. }
  161. digit *_ret = NULL;
  162. for (int i = 0; i < DIGIT.size(); ++i)
  163. {
  164. if (DIGIT[i]->name == _digit)
  165. _ret = DIGIT[i];
  166. }
  167. if (!_create) // nicht gefunden und soll auch nicht erzeugt werden, ggf. geht eine NULL zurück
  168. return _ret;
  169. if (_ret == NULL)
  170. {
  171. _ret = new digit;
  172. _ret->name = _digit;
  173. DIGIT.push_back(_ret);
  174. }
  175. roi* neuroi = new roi;
  176. neuroi->name = _roi;
  177. _ret->ROI.push_back(neuroi);
  178. printf("GetDIGIT - digit %s - roi %s\n", _digit.c_str(), _roi.c_str());
  179. return _ret;
  180. }
  181. string ClassFlowDigit::getHTMLSingleStep(string host)
  182. {
  183. string result, zw;
  184. std::vector<HTMLInfo*> htmlinfo;
  185. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  186. result = result + "Digital Counter: <p> ";
  187. htmlinfo = GetHTMLInfo();
  188. for (int i = 0; i < htmlinfo.size(); ++i)
  189. {
  190. if (htmlinfo[i]->val == 10)
  191. zw = "NaN";
  192. else
  193. {
  194. zw = to_string((int) htmlinfo[i]->val);
  195. }
  196. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  197. delete htmlinfo[i];
  198. }
  199. htmlinfo.clear();
  200. return result;
  201. }
  202. bool ClassFlowDigit::doFlow(string time)
  203. {
  204. if (disabled)
  205. return true;
  206. if (!doAlignAndCut(time)){
  207. return false;
  208. };
  209. doNeuralNetwork(time);
  210. RemoveOldLogs();
  211. return true;
  212. }
  213. bool ClassFlowDigit::doAlignAndCut(string time)
  214. {
  215. if (disabled)
  216. return true;
  217. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  218. for (int _dig = 0; _dig < DIGIT.size(); ++_dig)
  219. {
  220. printf("DIGIT[_dig]->ROI.size() %d\n", DIGIT[_dig]->ROI.size());
  221. for (int i = 0; i < DIGIT[_dig]->ROI.size(); ++i)
  222. {
  223. printf("DigitalDigit %d - Align&Cut\n", i);
  224. caic->CutAndSave(DIGIT[_dig]->ROI[i]->posx, DIGIT[_dig]->ROI[i]->posy, DIGIT[_dig]->ROI[i]->deltax, DIGIT[_dig]->ROI[i]->deltay, DIGIT[_dig]->ROI[i]->image_org);
  225. if (SaveAllFiles)
  226. {
  227. if (DIGIT[_dig]->name == "default")
  228. DIGIT[_dig]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + DIGIT[_dig]->ROI[i]->name + ".jpg"));
  229. else
  230. DIGIT[_dig]->ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + DIGIT[_dig]->name + "_" + DIGIT[_dig]->ROI[i]->name + ".jpg"));
  231. }
  232. DIGIT[_dig]->ROI[i]->image_org->Resize(modelxsize, modelysize, DIGIT[_dig]->ROI[i]->image);
  233. if (SaveAllFiles)
  234. {
  235. if (DIGIT[_dig]->name == "default")
  236. DIGIT[_dig]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + DIGIT[_dig]->ROI[i]->name + ".bmp"));
  237. else
  238. DIGIT[_dig]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + DIGIT[_dig]->name + "_" + DIGIT[_dig]->ROI[i]->name + ".bmp"));
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. bool ClassFlowDigit::doNeuralNetwork(string time)
  245. {
  246. if (disabled)
  247. return true;
  248. string logPath = CreateLogFolder(time);
  249. #ifndef OHNETFLITE
  250. CTfLiteClass *tflite = new CTfLiteClass;
  251. string zwcnn = FormatFileName("/sdcard" + cnnmodelfile);
  252. printf(zwcnn.c_str());printf("\n");
  253. if (!tflite->LoadModel(zwcnn)) {
  254. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  255. delete tflite;
  256. return false;
  257. }
  258. tflite->MakeAllocate();
  259. #endif
  260. for (int _dig = 0; _dig < DIGIT.size(); ++_dig)
  261. for (int i = 0; i < DIGIT[_dig]->ROI.size(); ++i)
  262. {
  263. printf("DigitalDigit %d - TfLite\n", i);
  264. DIGIT[_dig]->ROI[i]->resultklasse = 0;
  265. #ifndef OHNETFLITE
  266. DIGIT[_dig]->ROI[i]->resultklasse = tflite->GetClassFromImageBasis(DIGIT[_dig]->ROI[i]->image);
  267. #endif
  268. printf("Result Digit%i: %d\n", i, DIGIT[_dig]->ROI[i]->resultklasse);
  269. if (isLogImage)
  270. {
  271. if (isLogImageSelect)
  272. {
  273. if (LogImageSelect.find(DIGIT[_dig]->ROI[i]->name) != std::string::npos)
  274. {
  275. LogImage(logPath, DIGIT[_dig]->ROI[i]->name, NULL, &DIGIT[_dig]->ROI[i]->resultklasse, time, DIGIT[_dig]->ROI[i]->image_org);
  276. }
  277. }
  278. else
  279. {
  280. LogImage(logPath, DIGIT[_dig]->ROI[i]->name, NULL, &DIGIT[_dig]->ROI[i]->resultklasse, time, DIGIT[_dig]->ROI[i]->image_org);
  281. }
  282. }
  283. }
  284. #ifndef OHNETFLITE
  285. delete tflite;
  286. #endif
  287. return true;
  288. }
  289. void ClassFlowDigit::DrawROI(CImageBasis *_zw)
  290. {
  291. for (int _dig = 0; _dig < DIGIT.size(); ++_dig)
  292. for (int i = 0; i < DIGIT[_dig]->ROI.size(); ++i)
  293. _zw->drawRect(DIGIT[_dig]->ROI[i]->posx, DIGIT[_dig]->ROI[i]->posy, DIGIT[_dig]->ROI[i]->deltax, DIGIT[_dig]->ROI[i]->deltay, 0, 0, (255 - _dig*100), 2);
  294. }
  295. std::vector<HTMLInfo*> ClassFlowDigit::GetHTMLInfo()
  296. {
  297. std::vector<HTMLInfo*> result;
  298. for (int _dig = 0; _dig < DIGIT.size(); ++_dig)
  299. for (int i = 0; i < DIGIT[_dig]->ROI.size(); ++i)
  300. {
  301. if (DIGIT[_dig]->name == "default")
  302. DIGIT[_dig]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + DIGIT[_dig]->ROI[i]->name + ".bmp"));
  303. else
  304. DIGIT[_dig]->ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + DIGIT[_dig]->name + "_" + DIGIT[_dig]->ROI[i]->name + ".bmp"));
  305. HTMLInfo *zw = new HTMLInfo;
  306. if (DIGIT[_dig]->name == "default")
  307. {
  308. zw->filename = DIGIT[_dig]->ROI[i]->name + ".bmp";
  309. zw->filename_org = DIGIT[_dig]->ROI[i]->name + ".jpg";
  310. }
  311. else
  312. {
  313. zw->filename = DIGIT[_dig]->name + "_" + DIGIT[_dig]->ROI[i]->name + ".bmp";
  314. zw->filename_org = DIGIT[_dig]->name + "_" + DIGIT[_dig]->ROI[i]->name + ".jpg";
  315. }
  316. zw->val = DIGIT[_dig]->ROI[i]->resultklasse;
  317. zw->image = DIGIT[_dig]->ROI[i]->image;
  318. zw->image_org = DIGIT[_dig]->ROI[i]->image_org;
  319. result.push_back(zw);
  320. }
  321. return result;
  322. }
  323. int ClassFlowDigit::getAnzahlDIGIT()
  324. {
  325. return DIGIT.size();
  326. }
  327. string ClassFlowDigit::getNameDIGIT(int _digit)
  328. {
  329. if (_digit < DIGIT.size())
  330. return DIGIT[_digit]->name;
  331. return "DIGIT DOES NOT EXIST";
  332. }
  333. digit* ClassFlowDigit::GetDIGIT(int _digit)
  334. {
  335. if (_digit < DIGIT.size())
  336. return DIGIT[_digit];
  337. return NULL;
  338. }
  339. void ClassFlowDigit::UpdateNameNumbers(std::vector<std::string> *_name_numbers)
  340. {
  341. for (int _dig = 0; _dig < DIGIT.size(); _dig++)
  342. {
  343. std::string _name = DIGIT[_dig]->name;
  344. bool found = false;
  345. for (int i = 0; i < (*_name_numbers).size(); ++i)
  346. {
  347. if ((*_name_numbers)[i] == _name)
  348. found = true;
  349. }
  350. if (!found)
  351. (*_name_numbers).push_back(_name);
  352. }
  353. }