ClassFlowAnalog.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "ClassFlowAnalog.h"
  2. #include <math.h>
  3. #include <iomanip>
  4. #include <sstream>
  5. // #define OHNETFLITE
  6. #ifndef OHNETFLITE
  7. #include "CTfLiteClass.h"
  8. #endif
  9. #include "ClassLogFile.h"
  10. bool debugdetailanalog = false;
  11. ClassFlowAnalog::ClassFlowAnalog()
  12. {
  13. isLogImage = false;
  14. string cnnmodelfile = "";
  15. modelxsize = 1;
  16. modelysize = 1;
  17. ListFlowControll = NULL;
  18. }
  19. ClassFlowAnalog::ClassFlowAnalog(std::vector<ClassFlow*>* lfc)
  20. {
  21. isLogImage = false;
  22. string cnnmodelfile = "";
  23. modelxsize = 1;
  24. modelysize = 1;
  25. ListFlowControll = NULL;
  26. ListFlowControll = lfc;
  27. }
  28. string ClassFlowAnalog::getReadout()
  29. {
  30. int prev = -1;
  31. string result = "";
  32. for (int i = ROI.size() - 1; i >= 0; --i)
  33. {
  34. prev = ZeigerEval(ROI[i]->result, prev);
  35. result = std::to_string(prev) + result;
  36. }
  37. return result;
  38. }
  39. int ClassFlowAnalog::ZeigerEval(float zahl, int ziffer_vorgaenger)
  40. {
  41. int ergebnis_nachkomma = ((int) floor(zahl * 10)) % 10;
  42. int ergebnis_vorkomma = ((int) floor(zahl)) % 10;
  43. int ergebnis, ergebnis_rating;
  44. if (ziffer_vorgaenger == -1)
  45. return ergebnis_vorkomma % 10;
  46. ergebnis_rating = ergebnis_nachkomma - ziffer_vorgaenger;
  47. if (ergebnis_nachkomma >= 5)
  48. ergebnis_rating-=5;
  49. else
  50. ergebnis_rating+=5;
  51. ergebnis = (int) round(zahl);
  52. if (ergebnis_rating < 0)
  53. ergebnis-=1;
  54. if (ergebnis == -1)
  55. ergebnis+=10;
  56. ergebnis = ergebnis % 10;
  57. return ergebnis;
  58. }
  59. bool ClassFlowAnalog::ReadParameter(FILE* pfile, string& aktparamgraph)
  60. {
  61. std::vector<string> zerlegt;
  62. aktparamgraph = trim(aktparamgraph);
  63. if (aktparamgraph.size() == 0)
  64. if (!this->GetNextParagraph(pfile, aktparamgraph))
  65. return false;
  66. if (aktparamgraph.compare("[Analog]") != 0) // Paragraph passt nich zu MakeImage
  67. return false;
  68. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  69. {
  70. zerlegt = this->ZerlegeZeile(aktparamgraph);
  71. if ((zerlegt[0] == "LogImageLocation") && (zerlegt.size() > 1))
  72. {
  73. this->isLogImage = true;
  74. this->LogImageLocation = zerlegt[1];
  75. }
  76. if ((zerlegt[0] == "Model") && (zerlegt.size() > 1))
  77. {
  78. this->cnnmodelfile = zerlegt[1];
  79. }
  80. if ((zerlegt[0] == "ModelInputSize") && (zerlegt.size() > 2))
  81. {
  82. this->modelxsize = std::stoi(zerlegt[1]);
  83. this->modelysize = std::stoi(zerlegt[2]);
  84. }
  85. if (zerlegt.size() >= 5)
  86. {
  87. roianalog* neuroi = new roianalog;
  88. neuroi->name = zerlegt[0];
  89. neuroi->posx = std::stoi(zerlegt[1]);
  90. neuroi->posy = std::stoi(zerlegt[2]);
  91. neuroi->deltax = std::stoi(zerlegt[3]);
  92. neuroi->deltay = std::stoi(zerlegt[4]);
  93. neuroi->result = -1;
  94. ROI.push_back(neuroi);
  95. }
  96. }
  97. return true;
  98. }
  99. string ClassFlowAnalog::getHTMLSingleStep(string host)
  100. {
  101. string result, zw;
  102. std::vector<HTMLInfo*> htmlinfo;
  103. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  104. result = result + "Analog Pointers: <p> ";
  105. htmlinfo = GetHTMLInfo();
  106. for (int i = 0; i < htmlinfo.size(); ++i)
  107. {
  108. std::stringstream stream;
  109. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  110. zw = stream.str();
  111. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  112. delete htmlinfo[i];
  113. }
  114. htmlinfo.clear();
  115. return result;
  116. }
  117. bool ClassFlowAnalog::doFlow(string time)
  118. {
  119. if (!doAlignAndCut(time)){
  120. return false;
  121. };
  122. if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doFlow nach Alignment");
  123. doNeuralNetwork(time);
  124. return true;
  125. }
  126. bool ClassFlowAnalog::doAlignAndCut(string time)
  127. {
  128. string input = "/sdcard/img_tmp/alg.jpg";
  129. string input_roi = "/sdcard/img_tmp/alg_roi.jpg";
  130. string ioresize = "/sdcard/img_tmp/resize.bmp";
  131. string output;
  132. string nm;
  133. input = FormatFileName(input);
  134. input_roi = FormatFileName(input_roi);
  135. CResizeImage *rs;
  136. CImageBasis *img_roi = NULL;
  137. CAlignAndCutImage *caic = new CAlignAndCutImage(input);
  138. if (!caic->ImageOkay()){
  139. if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut not okay!");
  140. delete caic;
  141. return false;
  142. }
  143. if (input_roi.length() > 0){
  144. img_roi = new CImageBasis(input_roi);
  145. if (!img_roi->ImageOkay()){
  146. if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut ImageRoi not okay!");
  147. delete caic;
  148. delete img_roi;
  149. return false;
  150. }
  151. }
  152. for (int i = 0; i < ROI.size(); ++i)
  153. {
  154. printf("Analog %d - Align&Cut\n", i);
  155. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  156. output = FormatFileName(output);
  157. caic->CutAndSave(output, ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
  158. rs = new CResizeImage(output);
  159. if (!rs->ImageOkay()){
  160. if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doAlignAndCut CResizeImage(output);!");
  161. delete caic;
  162. delete rs;
  163. return false;
  164. }
  165. rs->Resize(modelxsize, modelysize);
  166. ioresize = "/sdcard/img_tmp/ra" + std::to_string(i) + ".bmp";
  167. ioresize = FormatFileName(ioresize);
  168. rs->SaveToFile(ioresize);
  169. delete rs;
  170. if (img_roi)
  171. {
  172. int r = 0;
  173. int g = 255;
  174. int b = 0;
  175. img_roi->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, r, g, b, 1);
  176. img_roi->drawCircle((int) (ROI[i]->posx + ROI[i]->deltax/2), (int) (ROI[i]->posy + ROI[i]->deltay/2), (int) (ROI[i]->deltax/2), r, g, b, 2);
  177. img_roi->drawLine((int) (ROI[i]->posx + ROI[i]->deltax/2), (int) ROI[i]->posy, (int) (ROI[i]->posx + ROI[i]->deltax/2), (int) (ROI[i]->posy + ROI[i]->deltay), r, g, b, 2);
  178. img_roi->drawLine((int) ROI[i]->posx, (int) (ROI[i]->posy + ROI[i]->deltay/2), (int) ROI[i]->posx + ROI[i]->deltax, (int) (ROI[i]->posy + ROI[i]->deltay/2), r, g, b, 2);
  179. }
  180. }
  181. delete caic;
  182. if (img_roi)
  183. {
  184. img_roi->SaveToFile(input_roi);
  185. delete img_roi;
  186. }
  187. return true;
  188. }
  189. bool ClassFlowAnalog::doNeuralNetwork(string time)
  190. {
  191. string input = "/sdcard/img_tmp/alg.jpg";
  192. string ioresize = "/sdcard/img_tmp/resize.bmp";
  193. string output;
  194. string nm;
  195. input = FormatFileName(input);
  196. #ifndef OHNETFLITE
  197. CTfLiteClass *tflite = new CTfLiteClass;
  198. string zwcnn = "/sdcard" + cnnmodelfile;
  199. zwcnn = FormatFileName(zwcnn);
  200. printf(zwcnn.c_str());printf("\n");
  201. tflite->LoadModel(zwcnn);
  202. tflite->MakeAllocate();
  203. #endif
  204. for (int i = 0; i < ROI.size(); ++i)
  205. {
  206. printf("Analog %d - TfLite\n", i);
  207. ioresize = "/sdcard/img_tmp/ra" + std::to_string(i) + ".bmp";
  208. ioresize = FormatFileName(ioresize);
  209. float f1, f2;
  210. f1 = 0; f2 = 0;
  211. #ifndef OHNETFLITE
  212. // LogFile.WriteToFile("ClassFlowAnalog::doNeuralNetwork vor CNN tflite->LoadInputImage(ioresize)");
  213. tflite->LoadInputImage(ioresize);
  214. tflite->Invoke();
  215. if (debugdetailanalog) LogFile.WriteToFile("Nach Invoke");
  216. f1 = tflite->GetOutputValue(0);
  217. f2 = tflite->GetOutputValue(1);
  218. #endif
  219. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  220. // printf("Result sin, cos, ziffer: %f, %f, %f\n", f1, f2, result);
  221. ROI[i]->result = result * 10;
  222. printf("Result Analog%i: %f\n", i, ROI[i]->result);
  223. if (isLogImage)
  224. {
  225. std::stringstream stream;
  226. stream << std::fixed << std::setprecision(1) << ROI[i]->result;
  227. std::string s = stream.str();
  228. // std::snprintf(&s[0], s.size(), "%.2f", pi);
  229. nm = "/sdcard" + LogImageLocation + "/" + s + "_" + ROI[i]->name + "_" + time + ".jpg";
  230. nm = FormatFileName(nm);
  231. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  232. output = FormatFileName(output);
  233. printf("Analog - save to file: %s\n", nm.c_str());
  234. CopyFile(output, nm);
  235. }
  236. }
  237. #ifndef OHNETFLITE
  238. delete tflite;
  239. #endif
  240. return true;
  241. }
  242. std::vector<HTMLInfo*> ClassFlowAnalog::GetHTMLInfo()
  243. {
  244. std::vector<HTMLInfo*> result;
  245. for (int i = 0; i < ROI.size(); ++i)
  246. {
  247. HTMLInfo *zw = new HTMLInfo;
  248. zw->filename = ROI[i]->name + ".jpg";
  249. zw->val = ROI[i]->result;
  250. result.push_back(zw);
  251. }
  252. return result;
  253. }