ClassFlowAnalog.cpp 7.8 KB

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