ClassFlowAnalog.cpp 8.5 KB

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