ClassFlowDigit.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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()
  53. {
  54. string rst = "";
  55. for (int i = 0; i < ROI.size(); ++i)
  56. {
  57. if (ROI[i]->resultklasse == 10)
  58. rst = rst + "N";
  59. else
  60. rst = rst + std::to_string(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. /*
  73. if ((aktparamgraph.compare("[Digits]") != 0) && (aktparamgraph.compare(";[Digits]") != 0)) // Paragraph passt nich zu MakeImage
  74. return false;
  75. */
  76. if ((aktparamgraph.compare(0, 7, "[Digits") != 0) && (aktparamgraph.compare(0, 8, ";[Digits") != 0)) // Paragraph passt nich zu MakeImage
  77. return false;
  78. int _pospkt = aktparamgraph.find_first_of(".");
  79. int _posklammerzu = aktparamgraph.find_first_of("]");
  80. // printf("Pos: %d, %d\n", _pospkt, _posklammerzu);
  81. if (_pospkt > -1)
  82. NameDigit = aktparamgraph.substr(_pospkt+1, _posklammerzu - _pospkt-1);
  83. else
  84. NameDigit = "";
  85. printf("Name Digit: %s\n", NameDigit.c_str());
  86. if (aktparamgraph[0] == ';')
  87. {
  88. disabled = true;
  89. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  90. printf("[Digits] is disabled !!!\n");
  91. return true;
  92. }
  93. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph))
  94. {
  95. zerlegt = this->ZerlegeZeile(aktparamgraph);
  96. if ((zerlegt[0] == "LogImageLocation") && (zerlegt.size() > 1))
  97. {
  98. LogImageLocation = "/sdcard" + zerlegt[1];
  99. isLogImage = true;
  100. }
  101. if ((zerlegt[0] == "Model") && (zerlegt.size() > 1))
  102. {
  103. cnnmodelfile = zerlegt[1];
  104. }
  105. if ((zerlegt[0] == "ModelInputSize") && (zerlegt.size() > 2))
  106. {
  107. modelxsize = std::stoi(zerlegt[1]);
  108. modelysize = std::stoi(zerlegt[2]);
  109. }
  110. if (zerlegt.size() >= 5)
  111. {
  112. roi* neuroi = new roi;
  113. neuroi->name = zerlegt[0];
  114. neuroi->posx = std::stoi(zerlegt[1]);
  115. neuroi->posy = std::stoi(zerlegt[2]);
  116. neuroi->deltax = std::stoi(zerlegt[3]);
  117. neuroi->deltay = std::stoi(zerlegt[4]);
  118. neuroi->resultklasse = -1;
  119. neuroi->image = NULL;
  120. neuroi->image_org = NULL;
  121. ROI.push_back(neuroi);
  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 i = 0; i < ROI.size(); ++i)
  130. {
  131. ROI[i]->image = new CImageBasis(modelxsize, modelysize, 3);
  132. ROI[i]->image_org = new CImageBasis(ROI[i]->deltax, ROI[i]->deltay, 3);
  133. }
  134. return true;
  135. }
  136. string ClassFlowDigit::getHTMLSingleStep(string host)
  137. {
  138. string result, zw;
  139. std::vector<HTMLInfo*> htmlinfo;
  140. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  141. result = result + "Digital Counter: <p> ";
  142. htmlinfo = GetHTMLInfo();
  143. for (int i = 0; i < htmlinfo.size(); ++i)
  144. {
  145. if (htmlinfo[i]->val == 10)
  146. zw = "NaN";
  147. else
  148. {
  149. zw = to_string((int) htmlinfo[i]->val);
  150. }
  151. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  152. delete htmlinfo[i];
  153. }
  154. htmlinfo.clear();
  155. return result;
  156. }
  157. bool ClassFlowDigit::doFlow(string time)
  158. {
  159. if (disabled)
  160. return true;
  161. if (!doAlignAndCut(time)){
  162. return false;
  163. };
  164. doNeuralNetwork(time);
  165. RemoveOldLogs();
  166. return true;
  167. }
  168. bool ClassFlowDigit::doAlignAndCut(string time)
  169. {
  170. if (disabled)
  171. return true;
  172. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  173. for (int i = 0; i < ROI.size(); ++i)
  174. {
  175. printf("DigitalDigit %d - Align&Cut\n", i);
  176. caic->CutAndSave(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, ROI[i]->image_org);
  177. if (SaveAllFiles) ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + ROI[i]->name + ".jpg"));
  178. ROI[i]->image_org->Resize(modelxsize, modelysize, ROI[i]->image);
  179. if (SaveAllFiles) ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + ROI[i]->name + ".bmp"));
  180. }
  181. return true;
  182. }
  183. bool ClassFlowDigit::doNeuralNetwork(string time)
  184. {
  185. if (disabled)
  186. return true;
  187. string logPath = CreateLogFolder(time);
  188. #ifndef OHNETFLITE
  189. CTfLiteClass *tflite = new CTfLiteClass;
  190. string zwcnn = FormatFileName("/sdcard" + cnnmodelfile);
  191. printf(zwcnn.c_str());printf("\n");
  192. if (!tflite->LoadModel(zwcnn)) {
  193. printf("Can't read model file /sdcard%s\n", cnnmodelfile.c_str());
  194. return false;
  195. }
  196. tflite->MakeAllocate();
  197. #endif
  198. for (int i = 0; i < ROI.size(); ++i)
  199. {
  200. printf("DigitalDigit %d - TfLite\n", i);
  201. ROI[i]->resultklasse = 0;
  202. #ifndef OHNETFLITE
  203. ROI[i]->resultklasse = tflite->GetClassFromImageBasis(ROI[i]->image);
  204. #endif
  205. printf("Result Digit%i: %d\n", i, ROI[i]->resultklasse);
  206. if (isLogImage)
  207. {
  208. LogImage(logPath, ROI[i]->name, NULL, &ROI[i]->resultklasse, time, ROI[i]->image_org);
  209. }
  210. }
  211. #ifndef OHNETFLITE
  212. delete tflite;
  213. #endif
  214. return true;
  215. }
  216. void ClassFlowDigit::DrawROI(CImageBasis *_zw)
  217. {
  218. for (int i = 0; i < ROI.size(); ++i)
  219. _zw->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, 0, 0, 255, 2);
  220. }
  221. std::vector<HTMLInfo*> ClassFlowDigit::GetHTMLInfo()
  222. {
  223. std::vector<HTMLInfo*> result;
  224. for (int i = 0; i < ROI.size(); ++i)
  225. {
  226. HTMLInfo *zw = new HTMLInfo;
  227. zw->filename = ROI[i]->name + ".bmp";
  228. zw->filename_org = ROI[i]->name + ".jpg";
  229. zw->val = ROI[i]->resultklasse;
  230. zw->image = ROI[i]->image;
  231. zw->image_org = ROI[i]->image_org;
  232. result.push_back(zw);
  233. }
  234. return result;
  235. }