ClassFlowDigit.cpp 12 KB

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