ClassFlowAnalog.cpp 7.6 KB

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