ClassFlowDigit.cpp 6.9 KB

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