ClassFlowDigit.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. ClassFlowDigit::ClassFlowDigit()
  10. {
  11. isLogImage = false;
  12. string cnnmodelfile = "";
  13. modelxsize = 1;
  14. modelysize = 1;
  15. ListFlowControll = NULL;
  16. }
  17. ClassFlowDigit::ClassFlowDigit(std::vector<ClassFlow*>* lfc)
  18. {
  19. isLogImage = false;
  20. string cnnmodelfile = "";
  21. modelxsize = 1;
  22. modelysize = 1;
  23. ListFlowControll = NULL;
  24. ListFlowControll = lfc;
  25. }
  26. string ClassFlowDigit::getReadout()
  27. {
  28. string rst = "";
  29. for (int i = 0; i < ROI.size(); ++i)
  30. {
  31. if (ROI[i]->resultklasse == 10)
  32. rst = rst + "N";
  33. else
  34. rst = rst + std::to_string(ROI[i]->resultklasse);
  35. }
  36. return rst;
  37. }
  38. bool ClassFlowDigit::ReadParameter(FILE* pfile, string& aktparamgraph)
  39. {
  40. std::vector<string> zerlegt;
  41. aktparamgraph = trim(aktparamgraph);
  42. if (aktparamgraph.size() == 0)
  43. if (!this->GetNextParagraph(pfile, aktparamgraph))
  44. return false;
  45. if (aktparamgraph.compare("[Digits]") != 0) // Paragraph passt nich zu MakeImage
  46. return false;
  47. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  48. {
  49. zerlegt = this->ZerlegeZeile(aktparamgraph);
  50. if ((zerlegt[0] == "LogImageLocation") && (zerlegt.size() > 1))
  51. {
  52. isLogImage = true;
  53. LogImageLocation = zerlegt[1];
  54. }
  55. if ((zerlegt[0] == "Model") && (zerlegt.size() > 1))
  56. {
  57. cnnmodelfile = zerlegt[1];
  58. }
  59. if ((zerlegt[0] == "ModelInputSize") && (zerlegt.size() > 2))
  60. {
  61. modelxsize = std::stoi(zerlegt[1]);
  62. modelysize = std::stoi(zerlegt[2]);
  63. }
  64. if (zerlegt.size() >= 5)
  65. {
  66. roi* neuroi = new roi;
  67. neuroi->name = zerlegt[0];
  68. neuroi->posx = std::stoi(zerlegt[1]);
  69. neuroi->posy = std::stoi(zerlegt[2]);
  70. neuroi->deltax = std::stoi(zerlegt[3]);
  71. neuroi->deltay = std::stoi(zerlegt[4]);
  72. ROI.push_back(neuroi);
  73. }
  74. }
  75. return true;
  76. }
  77. bool ClassFlowDigit::doFlow(string time)
  78. {
  79. doAlignAndCut(time);
  80. doNeuralNetwork(time);
  81. return true;
  82. }
  83. bool ClassFlowDigit::doAlignAndCut(string time)
  84. {
  85. string input = "/sdcard/img_tmp/alg.jpg";
  86. string input_roi = "/sdcard/img_tmp/alg_roi.jpg";
  87. string ioresize = "/sdcard/img_tmp/resize.bmp";
  88. string output;
  89. string nm;
  90. input = FormatFileName(input);
  91. input_roi = FormatFileName(input_roi);
  92. CResizeImage *rs;
  93. CImageBasis *img_roi = NULL;
  94. CAlignAndCutImage *caic = new CAlignAndCutImage(input);
  95. if (input_roi.length() > 0)
  96. img_roi = new CImageBasis(input_roi);
  97. for (int i = 0; i < ROI.size(); ++i)
  98. {
  99. printf("DigitalDigit %d - Align&Cut\n", i);
  100. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  101. output = FormatFileName(output);
  102. caic->CutAndSave(output, ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
  103. rs = new CResizeImage(output);
  104. rs->Resize(modelxsize, modelysize);
  105. ioresize = "/sdcard/img_tmp/rd" + std::to_string(i) + ".bmp";
  106. ioresize = FormatFileName(ioresize);
  107. rs->SaveToFile(ioresize);
  108. delete rs;
  109. if (img_roi)
  110. {
  111. img_roi->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, 0, 0, 255, 2);
  112. }
  113. }
  114. delete caic;
  115. if (img_roi)
  116. {
  117. img_roi->SaveToFile(input_roi);
  118. delete img_roi;
  119. }
  120. return true;
  121. }
  122. bool ClassFlowDigit::doNeuralNetwork(string time)
  123. {
  124. string input = "/sdcard/img_tmp/alg.jpg";
  125. string ioresize = "/sdcard/img_tmp/resize.bmp";
  126. string output;
  127. string nm;
  128. input = FormatFileName(input);
  129. #ifndef OHNETFLITE
  130. CTfLiteClass *tflite = new CTfLiteClass;
  131. string zwcnn = "/sdcard" + cnnmodelfile;
  132. zwcnn = FormatFileName(zwcnn);
  133. printf(zwcnn.c_str());printf("\n");
  134. tflite->LoadModel(zwcnn);
  135. tflite->MakeAllocate();
  136. #endif
  137. for (int i = 0; i < ROI.size(); ++i)
  138. {
  139. printf("DigitalDigit %d - TfLite\n", i);
  140. ioresize = "/sdcard/img_tmp/rd" + std::to_string(i) + ".bmp";
  141. ioresize = FormatFileName(ioresize);
  142. // printf("output: %s, ioresize: %s\n", output.c_str(), ioresize.c_str());
  143. ROI[i]->resultklasse = 0;
  144. #ifndef OHNETFLITE
  145. ROI[i]->resultklasse = tflite->GetClassFromImage(ioresize);
  146. #endif
  147. printf("Result Digit%i: %d\n", i, ROI[i]->resultklasse);
  148. if (isLogImage)
  149. {
  150. nm = "/sdcard" + LogImageLocation + "/" + std::to_string(ROI[i]->resultklasse) + "/" + time + "_" + ROI[i]->name + ".jpg";
  151. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  152. output = FormatFileName(output);
  153. nm = FormatFileName(nm);
  154. CopyFile(output, nm);
  155. }
  156. }
  157. #ifndef OHNETFLITE
  158. delete tflite;
  159. #endif
  160. return true;
  161. }
  162. std::vector<HTMLInfo*> ClassFlowDigit::GetHTMLInfo()
  163. {
  164. std::vector<HTMLInfo*> result;
  165. for (int i = 0; i < ROI.size(); ++i)
  166. {
  167. HTMLInfo *zw = new HTMLInfo;
  168. zw->filename = ROI[i]->name + ".jpg";
  169. zw->val = ROI[i]->resultklasse;
  170. result.push_back(zw);
  171. }
  172. return result;
  173. }