ClassFlowDigit.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. }
  21. ClassFlowDigit::ClassFlowDigit() : ClassFlowImage(TAG)
  22. {
  23. SetInitialParameter();
  24. }
  25. ClassFlowDigit::ClassFlowDigit(std::vector<ClassFlow*>* lfc) : ClassFlowImage(lfc, TAG)
  26. {
  27. SetInitialParameter();
  28. ListFlowControll = lfc;
  29. for (int i = 0; i < ListFlowControll->size(); ++i)
  30. {
  31. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  32. {
  33. flowpostalignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
  34. }
  35. }
  36. }
  37. ClassFlowDigit::ClassFlowDigit(std::vector<ClassFlow*>* lfc, ClassFlow *_prev) : ClassFlowImage(lfc, _prev, TAG)
  38. {
  39. SetInitialParameter();
  40. ListFlowControll = lfc;
  41. previousElement = _prev;
  42. for (int i = 0; i < ListFlowControll->size(); ++i)
  43. {
  44. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  45. {
  46. flowpostalignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
  47. }
  48. }
  49. }
  50. string ClassFlowDigit::getReadout()
  51. {
  52. string rst = "";
  53. for (int i = 0; i < ROI.size(); ++i)
  54. {
  55. if (ROI[i]->resultklasse == 10)
  56. rst = rst + "N";
  57. else
  58. rst = rst + std::to_string(ROI[i]->resultklasse);
  59. }
  60. return rst;
  61. }
  62. bool ClassFlowDigit::ReadParameter(FILE* pfile, string& aktparamgraph)
  63. {
  64. std::vector<string> zerlegt;
  65. aktparamgraph = trim(aktparamgraph);
  66. if (aktparamgraph.size() == 0)
  67. if (!this->GetNextParagraph(pfile, aktparamgraph))
  68. return false;
  69. if (aktparamgraph.compare("[Digits]") != 0) // Paragraph passt nicht
  70. return false;
  71. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph))
  72. {
  73. zerlegt = this->ZerlegeZeile(aktparamgraph);
  74. if ((zerlegt[0] == "LogImageLocation") && (zerlegt.size() > 1))
  75. {
  76. LogImageLocation = "/sdcard" + zerlegt[1];
  77. isLogImage = true;
  78. }
  79. if ((zerlegt[0] == "Model") && (zerlegt.size() > 1))
  80. {
  81. cnnmodelfile = zerlegt[1];
  82. }
  83. if ((zerlegt[0] == "ModelInputSize") && (zerlegt.size() > 2))
  84. {
  85. modelxsize = std::stoi(zerlegt[1]);
  86. modelysize = std::stoi(zerlegt[2]);
  87. }
  88. if (zerlegt.size() >= 5)
  89. {
  90. roi* neuroi = new roi;
  91. neuroi->name = zerlegt[0];
  92. neuroi->posx = std::stoi(zerlegt[1]);
  93. neuroi->posy = std::stoi(zerlegt[2]);
  94. neuroi->deltax = std::stoi(zerlegt[3]);
  95. neuroi->deltay = std::stoi(zerlegt[4]);
  96. neuroi->resultklasse = -1;
  97. neuroi->image = NULL;
  98. neuroi->image_org = NULL;
  99. ROI.push_back(neuroi);
  100. }
  101. if ((toUpper(zerlegt[0]) == "SAVEALLFILES") && (zerlegt.size() > 1))
  102. {
  103. if (toUpper(zerlegt[1]) == "TRUE")
  104. SaveAllFiles = true;
  105. }
  106. }
  107. for (int i = 0; i < ROI.size(); ++i)
  108. {
  109. ROI[i]->image = new CImageBasis(modelxsize, modelysize, 3);
  110. ROI[i]->image_org = new CImageBasis(ROI[i]->deltax, ROI[i]->deltay, 3);
  111. }
  112. return true;
  113. }
  114. string ClassFlowDigit::getHTMLSingleStep(string host)
  115. {
  116. string result, zw;
  117. std::vector<HTMLInfo*> htmlinfo;
  118. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  119. result = result + "Digital Counter: <p> ";
  120. htmlinfo = GetHTMLInfo();
  121. for (int i = 0; i < htmlinfo.size(); ++i)
  122. {
  123. if (htmlinfo[i]->val == 10)
  124. zw = "NaN";
  125. else
  126. {
  127. zw = to_string((int) htmlinfo[i]->val);
  128. }
  129. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  130. delete htmlinfo[i];
  131. }
  132. htmlinfo.clear();
  133. return result;
  134. }
  135. bool ClassFlowDigit::doFlow(string time)
  136. {
  137. if (!doAlignAndCut(time)){
  138. return false;
  139. };
  140. doNeuralNetwork(time);
  141. RemoveOldLogs();
  142. return true;
  143. }
  144. bool ClassFlowDigit::doAlignAndCut(string time)
  145. {
  146. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  147. for (int i = 0; i < ROI.size(); ++i)
  148. {
  149. printf("DigitalDigit %d - Align&Cut\n", i);
  150. caic->CutAndSave(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, ROI[i]->image_org);
  151. if (SaveAllFiles) ROI[i]->image_org->SaveToFile(FormatFileName("/sdcard/img_tmp/" + ROI[i]->name + ".jpg"));
  152. ROI[i]->image_org->Resize(modelxsize, modelysize, ROI[i]->image);
  153. if (SaveAllFiles) ROI[i]->image->SaveToFile(FormatFileName("/sdcard/img_tmp/" + ROI[i]->name + ".bmp"));
  154. }
  155. return true;
  156. }
  157. bool ClassFlowDigit::doNeuralNetwork(string time)
  158. {
  159. string logPath = CreateLogFolder(time);
  160. #ifndef OHNETFLITE
  161. CTfLiteClass *tflite = new CTfLiteClass;
  162. string zwcnn = FormatFileName("/sdcard" + cnnmodelfile);
  163. printf(zwcnn.c_str());printf("\n");
  164. tflite->LoadModel(zwcnn);
  165. tflite->MakeAllocate();
  166. #endif
  167. for (int i = 0; i < ROI.size(); ++i)
  168. {
  169. printf("DigitalDigit %d - TfLite\n", i);
  170. ROI[i]->resultklasse = 0;
  171. #ifndef OHNETFLITE
  172. ROI[i]->resultklasse = tflite->GetClassFromImageBasis(ROI[i]->image);
  173. #endif
  174. printf("Result Digit%i: %d\n", i, ROI[i]->resultklasse);
  175. if (isLogImage)
  176. {
  177. LogImage(logPath, ROI[i]->name, NULL, &ROI[i]->resultklasse, time, ROI[i]->image_org);
  178. }
  179. }
  180. #ifndef OHNETFLITE
  181. delete tflite;
  182. #endif
  183. return true;
  184. }
  185. void ClassFlowDigit::DrawROI(CImageBasis *_zw)
  186. {
  187. for (int i = 0; i < ROI.size(); ++i)
  188. _zw->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, 0, 0, 255, 2);
  189. }
  190. std::vector<HTMLInfo*> ClassFlowDigit::GetHTMLInfo()
  191. {
  192. std::vector<HTMLInfo*> result;
  193. for (int i = 0; i < ROI.size(); ++i)
  194. {
  195. HTMLInfo *zw = new HTMLInfo;
  196. zw->filename = ROI[i]->name + ".bmp";
  197. zw->filename_org = ROI[i]->name + ".jpg";
  198. zw->val = ROI[i]->resultklasse;
  199. zw->image = ROI[i]->image;
  200. zw->image_org = ROI[i]->image_org;
  201. result.push_back(zw);
  202. }
  203. return result;
  204. }