ClassFlowAnalog.cpp 9.3 KB

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