ClassFlowAnalog.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. bool ClassFlowAnalog::doFlow(string time)
  97. {
  98. doAlignAndCut(time);
  99. doNeuralNetwork(time);
  100. return true;
  101. }
  102. bool ClassFlowAnalog::doAlignAndCut(string time)
  103. {
  104. string input = "/sdcard/img_tmp/alg.jpg";
  105. string input_roi = "/sdcard/img_tmp/alg_roi.jpg";
  106. string ioresize = "/sdcard/img_tmp/resize.bmp";
  107. string output;
  108. string nm;
  109. input = FormatFileName(input);
  110. input_roi = FormatFileName(input_roi);
  111. CResizeImage *rs;
  112. CImageBasis *img_roi = NULL;
  113. CAlignAndCutImage *caic = new CAlignAndCutImage(input);
  114. if (input_roi.length() > 0)
  115. img_roi = new CImageBasis(input_roi);
  116. for (int i = 0; i < ROI.size(); ++i)
  117. {
  118. printf("Analog %d - Align&Cut\n", i);
  119. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  120. output = FormatFileName(output);
  121. caic->CutAndSave(output, ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
  122. rs = new CResizeImage(output);
  123. rs->Resize(modelxsize, modelysize);
  124. ioresize = "/sdcard/img_tmp/ra" + std::to_string(i) + ".bmp";
  125. ioresize = FormatFileName(ioresize);
  126. rs->SaveToFile(ioresize);
  127. delete rs;
  128. if (img_roi)
  129. {
  130. int r = 0;
  131. int g = 255;
  132. int b = 0;
  133. img_roi->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, r, g, b, 1);
  134. 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);
  135. 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);
  136. 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);
  137. }
  138. }
  139. delete caic;
  140. if (img_roi)
  141. {
  142. img_roi->SaveToFile(input_roi);
  143. delete img_roi;
  144. }
  145. return true;
  146. }
  147. bool ClassFlowAnalog::doNeuralNetwork(string time)
  148. {
  149. string input = "/sdcard/img_tmp/alg.jpg";
  150. string ioresize = "/sdcard/img_tmp/resize.bmp";
  151. string output;
  152. string nm;
  153. input = FormatFileName(input);
  154. #ifndef OHNETFLITE
  155. CTfLiteClass *tflite = new CTfLiteClass;
  156. string zwcnn = "/sdcard" + cnnmodelfile;
  157. zwcnn = FormatFileName(zwcnn);
  158. printf(zwcnn.c_str());printf("\n");
  159. tflite->LoadModel(zwcnn);
  160. tflite->MakeAllocate();
  161. #endif
  162. for (int i = 0; i < ROI.size(); ++i)
  163. {
  164. printf("Analog %d - TfLite\n", i);
  165. ioresize = "/sdcard/img_tmp/ra" + std::to_string(i) + ".bmp";
  166. ioresize = FormatFileName(ioresize);
  167. float f1, f2;
  168. f1 = 0; f2 = 0;
  169. #ifndef OHNETFLITE
  170. tflite->LoadInputImage(ioresize);
  171. tflite->Invoke();
  172. f1 = tflite->GetOutputValue(0);
  173. f2 = tflite->GetOutputValue(1);
  174. #endif
  175. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  176. // printf("Result sin, cos, ziffer: %f, %f, %f\n", f1, f2, result);
  177. ROI[i]->result = result * 10;
  178. printf("Result Analog%i: %f\n", i, ROI[i]->result);
  179. if (isLogImage)
  180. {
  181. std::stringstream stream;
  182. stream << std::fixed << std::setprecision(1) << ROI[i]->result;
  183. std::string s = stream.str();
  184. // std::snprintf(&s[0], s.size(), "%.2f", pi);
  185. nm = "/sdcard" + LogImageLocation + "/" + s + "_" + ROI[i]->name + "_" + time + ".jpg";
  186. nm = FormatFileName(nm);
  187. output = "/sdcard/img_tmp/" + ROI[i]->name + ".jpg";
  188. output = FormatFileName(output);
  189. printf("Analog - save to file: %s\n", nm.c_str());
  190. CopyFile(output, nm);
  191. }
  192. }
  193. #ifndef OHNETFLITE
  194. delete tflite;
  195. #endif
  196. return true;
  197. }
  198. std::vector<HTMLInfo*> ClassFlowAnalog::GetHTMLInfo()
  199. {
  200. std::vector<HTMLInfo*> result;
  201. for (int i = 0; i < ROI.size(); ++i)
  202. {
  203. HTMLInfo *zw = new HTMLInfo;
  204. zw->filename = ROI[i]->name + ".jpg";
  205. zw->val = ROI[i]->result;
  206. result.push_back(zw);
  207. }
  208. return result;
  209. }