ClassFlowDigit.cpp 6.5 KB

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