ClassFlowDigit.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. if (!img_roi->ImageOkay()){
  127. LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut ImageRoi not okay!");
  128. delete img_roi;
  129. return false;
  130. }
  131. }
  132. for (int i = 0; i < ROI.size(); ++i)
  133. {
  134. printf("DigitalDigit %d - Align&Cut\n", i);
  135. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  136. output = FormatFileName(output);
  137. caic->CutAndSave(output, ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
  138. rs = new CResizeImage(output);
  139. rs->Resize(modelxsize, modelysize);
  140. ioresize = "/sdcard/img_tmp/rd" + std::to_string(i) + ".bmp";
  141. ioresize = FormatFileName(ioresize);
  142. rs->SaveToFile(ioresize);
  143. delete rs;
  144. if (img_roi)
  145. {
  146. img_roi->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, 0, 0, 255, 2);
  147. }
  148. }
  149. delete caic;
  150. if (img_roi)
  151. {
  152. img_roi->SaveToFile(input_roi);
  153. delete img_roi;
  154. }
  155. return true;
  156. }
  157. bool ClassFlowDigit::doNeuralNetwork(string time)
  158. {
  159. string input = "/sdcard/img_tmp/alg.jpg";
  160. string ioresize = "/sdcard/img_tmp/resize.bmp";
  161. string output;
  162. string nm;
  163. input = FormatFileName(input);
  164. #ifndef OHNETFLITE
  165. CTfLiteClass *tflite = new CTfLiteClass;
  166. string zwcnn = "/sdcard" + cnnmodelfile;
  167. zwcnn = FormatFileName(zwcnn);
  168. printf(zwcnn.c_str());printf("\n");
  169. tflite->LoadModel(zwcnn);
  170. tflite->MakeAllocate();
  171. #endif
  172. for (int i = 0; i < ROI.size(); ++i)
  173. {
  174. printf("DigitalDigit %d - TfLite\n", i);
  175. ioresize = "/sdcard/img_tmp/rd" + std::to_string(i) + ".bmp";
  176. ioresize = FormatFileName(ioresize);
  177. // printf("output: %s, ioresize: %s\n", output.c_str(), ioresize.c_str());
  178. ROI[i]->resultklasse = 0;
  179. #ifndef OHNETFLITE
  180. ROI[i]->resultklasse = tflite->GetClassFromImage(ioresize);
  181. #endif
  182. printf("Result Digit%i: %d\n", i, ROI[i]->resultklasse);
  183. if (isLogImage)
  184. {
  185. nm = "/sdcard" + LogImageLocation + "/" + std::to_string(ROI[i]->resultklasse) + "/" + time + "_" + ROI[i]->name + ".jpg";
  186. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  187. output = FormatFileName(output);
  188. nm = FormatFileName(nm);
  189. CopyFile(output, nm);
  190. }
  191. }
  192. #ifndef OHNETFLITE
  193. delete tflite;
  194. #endif
  195. return true;
  196. }
  197. std::vector<HTMLInfo*> ClassFlowDigit::GetHTMLInfo()
  198. {
  199. std::vector<HTMLInfo*> result;
  200. for (int i = 0; i < ROI.size(); ++i)
  201. {
  202. HTMLInfo *zw = new HTMLInfo;
  203. zw->filename = ROI[i]->name + ".jpg";
  204. zw->val = ROI[i]->resultklasse;
  205. result.push_back(zw);
  206. }
  207. return result;
  208. }