ClassFlowDigit.cpp 6.2 KB

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