ClassFlowAnalog.cpp 9.2 KB

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