ClassFlowAnalog.cpp 8.1 KB

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