ClassFlowDigit.cpp 6.5 KB

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