ClassFlowDigit.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. static const char* TAG = "flow_digital";
  11. ClassFlowDigit::ClassFlowDigit() : ClassFlowImage(TAG)
  12. {
  13. string cnnmodelfile = "";
  14. modelxsize = 1;
  15. modelysize = 1;
  16. ListFlowControll = NULL;
  17. }
  18. ClassFlowDigit::ClassFlowDigit(std::vector<ClassFlow*>* lfc) : ClassFlowImage(lfc, TAG)
  19. {
  20. string cnnmodelfile = "";
  21. modelxsize = 1;
  22. modelysize = 1;
  23. }
  24. string ClassFlowDigit::getReadout()
  25. {
  26. string rst = "";
  27. for (int i = 0; i < ROI.size(); ++i)
  28. {
  29. if (ROI[i]->resultklasse == 10)
  30. rst = rst + "N";
  31. else
  32. rst = rst + std::to_string(ROI[i]->resultklasse);
  33. }
  34. return rst;
  35. }
  36. bool ClassFlowDigit::ReadParameter(FILE* pfile, string& aktparamgraph)
  37. {
  38. std::vector<string> zerlegt;
  39. aktparamgraph = trim(aktparamgraph);
  40. if (aktparamgraph.size() == 0)
  41. if (!this->GetNextParagraph(pfile, aktparamgraph))
  42. return false;
  43. if (aktparamgraph.compare("[Digits]") != 0) // Paragraph passt nich zu MakeImage
  44. return false;
  45. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  46. {
  47. zerlegt = this->ZerlegeZeile(aktparamgraph);
  48. if ((zerlegt[0] == "LogImageLocation") && (zerlegt.size() > 1))
  49. {
  50. LogImageLocation = "/sdcard" + zerlegt[1];
  51. isLogImage = true;
  52. }
  53. if ((zerlegt[0] == "Model") && (zerlegt.size() > 1))
  54. {
  55. cnnmodelfile = zerlegt[1];
  56. }
  57. if ((zerlegt[0] == "ModelInputSize") && (zerlegt.size() > 2))
  58. {
  59. modelxsize = std::stoi(zerlegt[1]);
  60. modelysize = std::stoi(zerlegt[2]);
  61. }
  62. if (zerlegt.size() >= 5)
  63. {
  64. roi* neuroi = new roi;
  65. neuroi->name = zerlegt[0];
  66. neuroi->posx = std::stoi(zerlegt[1]);
  67. neuroi->posy = std::stoi(zerlegt[2]);
  68. neuroi->deltax = std::stoi(zerlegt[3]);
  69. neuroi->deltay = std::stoi(zerlegt[4]);
  70. neuroi->resultklasse = -1;
  71. ROI.push_back(neuroi);
  72. }
  73. }
  74. return true;
  75. }
  76. string ClassFlowDigit::getHTMLSingleStep(string host)
  77. {
  78. string result, zw;
  79. std::vector<HTMLInfo*> htmlinfo;
  80. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  81. result = result + "Digital Counter: <p> ";
  82. htmlinfo = GetHTMLInfo();
  83. for (int i = 0; i < htmlinfo.size(); ++i)
  84. {
  85. if (htmlinfo[i]->val == 10)
  86. zw = "NaN";
  87. else
  88. {
  89. zw = to_string((int) htmlinfo[i]->val);
  90. }
  91. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  92. delete htmlinfo[i];
  93. }
  94. htmlinfo.clear();
  95. return result;
  96. }
  97. bool ClassFlowDigit::doFlow(string time)
  98. {
  99. if (!doAlignAndCut(time)){
  100. return false;
  101. };
  102. doNeuralNetwork(time);
  103. RemoveOldLogs();
  104. return true;
  105. }
  106. bool ClassFlowDigit::doAlignAndCut(string time)
  107. {
  108. string input = "/sdcard/img_tmp/alg.jpg";
  109. string input_roi = "/sdcard/img_tmp/alg_roi.jpg";
  110. string ioresize = "/sdcard/img_tmp/resize.bmp";
  111. string output;
  112. string nm;
  113. input = FormatFileName(input);
  114. input_roi = FormatFileName(input_roi);
  115. CResizeImage *rs;
  116. CImageBasis *img_roi = NULL;
  117. CAlignAndCutImage *caic = new CAlignAndCutImage(input);
  118. if (!caic->ImageOkay()){
  119. LogFile.WriteToFile("ClassFlowDigit::doAlignAndCut not okay!");
  120. delete caic;
  121. return false;
  122. }
  123. if (input_roi.length() > 0){
  124. img_roi = new CImageBasis(input_roi);
  125. if (!img_roi->ImageOkay()){
  126. LogFile.WriteToFile("ClassFlowDigit::doAlignAndCut ImageRoi not okay!");
  127. delete caic;
  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 logPath = CreateLogFolder(time);
  160. string input = "/sdcard/img_tmp/alg.jpg";
  161. string ioresize = "/sdcard/img_tmp/resize.bmp";
  162. string output;
  163. string nm;
  164. input = FormatFileName(input);
  165. #ifndef OHNETFLITE
  166. CTfLiteClass *tflite = new CTfLiteClass;
  167. string zwcnn = "/sdcard" + cnnmodelfile;
  168. zwcnn = FormatFileName(zwcnn);
  169. printf(zwcnn.c_str());printf("\n");
  170. tflite->LoadModel(zwcnn);
  171. tflite->MakeAllocate();
  172. #endif
  173. for (int i = 0; i < ROI.size(); ++i)
  174. {
  175. printf("DigitalDigit %d - TfLite\n", i);
  176. ioresize = "/sdcard/img_tmp/rd" + std::to_string(i) + ".bmp";
  177. ioresize = FormatFileName(ioresize);
  178. // printf("output: %s, ioresize: %s\n", output.c_str(), ioresize.c_str());
  179. ROI[i]->resultklasse = 0;
  180. #ifndef OHNETFLITE
  181. ROI[i]->resultklasse = tflite->GetClassFromImage(ioresize);
  182. #endif
  183. printf("Result Digit%i: %d\n", i, ROI[i]->resultklasse);
  184. LogImage(logPath, ROI[i]->name, NULL, &ROI[i]->resultklasse, time);
  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. }