ClassFlowDigit.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. string ClassFlowDigit::getHTMLSingleStep(string host)
  78. {
  79. string result, zw;
  80. std::vector<HTMLInfo*> htmlinfo;
  81. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  82. result = result + "Digital Counter: <p> ";
  83. htmlinfo = GetHTMLInfo();
  84. for (int i = 0; i < htmlinfo.size(); ++i)
  85. {
  86. if (htmlinfo[i]->val == 10)
  87. zw = "NaN";
  88. else
  89. {
  90. zw = to_string((int) htmlinfo[i]->val);
  91. }
  92. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  93. delete htmlinfo[i];
  94. }
  95. htmlinfo.clear();
  96. return result;
  97. }
  98. bool ClassFlowDigit::doFlow(string time)
  99. {
  100. doAlignAndCut(time);
  101. doNeuralNetwork(time);
  102. return true;
  103. }
  104. bool ClassFlowDigit::doAlignAndCut(string time)
  105. {
  106. string input = "/sdcard/img_tmp/alg.jpg";
  107. string input_roi = "/sdcard/img_tmp/alg_roi.jpg";
  108. string ioresize = "/sdcard/img_tmp/resize.bmp";
  109. string output;
  110. string nm;
  111. input = FormatFileName(input);
  112. input_roi = FormatFileName(input_roi);
  113. CResizeImage *rs;
  114. CImageBasis *img_roi = NULL;
  115. CAlignAndCutImage *caic = new CAlignAndCutImage(input);
  116. if (input_roi.length() > 0)
  117. img_roi = new CImageBasis(input_roi);
  118. for (int i = 0; i < ROI.size(); ++i)
  119. {
  120. printf("DigitalDigit %d - Align&Cut\n", i);
  121. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  122. output = FormatFileName(output);
  123. caic->CutAndSave(output, ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
  124. rs = new CResizeImage(output);
  125. rs->Resize(modelxsize, modelysize);
  126. ioresize = "/sdcard/img_tmp/rd" + std::to_string(i) + ".bmp";
  127. ioresize = FormatFileName(ioresize);
  128. rs->SaveToFile(ioresize);
  129. delete rs;
  130. if (img_roi)
  131. {
  132. img_roi->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, 0, 0, 255, 2);
  133. }
  134. }
  135. delete caic;
  136. if (img_roi)
  137. {
  138. img_roi->SaveToFile(input_roi);
  139. delete img_roi;
  140. }
  141. return true;
  142. }
  143. bool ClassFlowDigit::doNeuralNetwork(string time)
  144. {
  145. string input = "/sdcard/img_tmp/alg.jpg";
  146. string ioresize = "/sdcard/img_tmp/resize.bmp";
  147. string output;
  148. string nm;
  149. input = FormatFileName(input);
  150. #ifndef OHNETFLITE
  151. CTfLiteClass *tflite = new CTfLiteClass;
  152. string zwcnn = "/sdcard" + cnnmodelfile;
  153. zwcnn = FormatFileName(zwcnn);
  154. printf(zwcnn.c_str());printf("\n");
  155. tflite->LoadModel(zwcnn);
  156. tflite->MakeAllocate();
  157. #endif
  158. for (int i = 0; i < ROI.size(); ++i)
  159. {
  160. printf("DigitalDigit %d - TfLite\n", i);
  161. ioresize = "/sdcard/img_tmp/rd" + std::to_string(i) + ".bmp";
  162. ioresize = FormatFileName(ioresize);
  163. // printf("output: %s, ioresize: %s\n", output.c_str(), ioresize.c_str());
  164. ROI[i]->resultklasse = 0;
  165. #ifndef OHNETFLITE
  166. ROI[i]->resultklasse = tflite->GetClassFromImage(ioresize);
  167. #endif
  168. printf("Result Digit%i: %d\n", i, ROI[i]->resultklasse);
  169. if (isLogImage)
  170. {
  171. nm = "/sdcard" + LogImageLocation + "/" + std::to_string(ROI[i]->resultklasse) + "/" + time + "_" + ROI[i]->name + ".jpg";
  172. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  173. output = FormatFileName(output);
  174. nm = FormatFileName(nm);
  175. CopyFile(output, nm);
  176. }
  177. }
  178. #ifndef OHNETFLITE
  179. delete tflite;
  180. #endif
  181. return true;
  182. }
  183. std::vector<HTMLInfo*> ClassFlowDigit::GetHTMLInfo()
  184. {
  185. std::vector<HTMLInfo*> result;
  186. for (int i = 0; i < ROI.size(); ++i)
  187. {
  188. HTMLInfo *zw = new HTMLInfo;
  189. zw->filename = ROI[i]->name + ".jpg";
  190. zw->val = ROI[i]->resultklasse;
  191. result.push_back(zw);
  192. }
  193. return result;
  194. }