ClassFlowAnalog.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. return true;
  114. }
  115. string ClassFlowAnalog::getHTMLSingleStep(string host)
  116. {
  117. string result, zw;
  118. std::vector<HTMLInfo*> htmlinfo;
  119. result = "<p>Found ROIs: </p> <p><img src=\"" + host + "/img_tmp/alg_roi.jpg\"></p>\n";
  120. result = result + "Analog Pointers: <p> ";
  121. htmlinfo = GetHTMLInfo();
  122. for (int i = 0; i < htmlinfo.size(); ++i)
  123. {
  124. std::stringstream stream;
  125. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  126. zw = stream.str();
  127. result = result + "<img src=\"" + host + "/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  128. delete htmlinfo[i];
  129. }
  130. htmlinfo.clear();
  131. return result;
  132. }
  133. bool ClassFlowAnalog::doFlow(string time)
  134. {
  135. if (!doAlignAndCut(time)){
  136. return false;
  137. };
  138. if (debugdetailanalog) LogFile.WriteToFile("ClassFlowAnalog::doFlow nach Alignment");
  139. doNeuralNetwork(time);
  140. RemoveOldLogs();
  141. return true;
  142. }
  143. bool ClassFlowAnalog::doAlignAndCut(string time)
  144. {
  145. CAlignAndCutImage *caic = flowpostalignment->GetAlignAndCutImage();
  146. for (int i = 0; i < ROI.size(); ++i)
  147. {
  148. printf("Analog %d - Align&Cut\n", i);
  149. CResizeImage *rs = caic->CutAndSave(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay);
  150. if (ROI[i]->image_org)
  151. delete ROI[i]->image_org;
  152. ROI[i]->image_org = new CImageBasis((CImageBasis*) rs);
  153. if (SaveAllFiles) rs->SaveToFile(FormatFileName("/sdcard/img_tmp/" + ROI[i]->name + ".jpg"));
  154. rs->Resize(modelxsize, modelysize);
  155. if (SaveAllFiles) rs->SaveToFile(FormatFileName("/sdcard/img_tmp/" + ROI[i]->name + ".bmp"));
  156. if (ROI[i]->image)
  157. delete ROI[i]->image;
  158. ROI[i]->image = rs;
  159. }
  160. return true;
  161. }
  162. void ClassFlowAnalog::DrawROI(CImageBasis *_zw)
  163. {
  164. int r = 0;
  165. int g = 255;
  166. int b = 0;
  167. for (int i = 0; i < ROI.size(); ++i)
  168. {
  169. _zw->drawRect(ROI[i]->posx, ROI[i]->posy, ROI[i]->deltax, ROI[i]->deltay, r, g, b, 1);
  170. _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);
  171. _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);
  172. _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);
  173. }
  174. }
  175. bool ClassFlowAnalog::doNeuralNetwork(string time)
  176. {
  177. string logPath = CreateLogFolder(time);
  178. string input = "/sdcard/img_tmp/alg.jpg";
  179. string ioresize = "/sdcard/img_tmp/resize.bmp";
  180. string output;
  181. input = FormatFileName(input);
  182. #ifndef OHNETFLITE
  183. CTfLiteClass *tflite = new CTfLiteClass;
  184. string zwcnn = "/sdcard" + cnnmodelfile;
  185. zwcnn = FormatFileName(zwcnn);
  186. printf(zwcnn.c_str());printf("\n");
  187. tflite->LoadModel(zwcnn);
  188. tflite->MakeAllocate();
  189. #endif
  190. for (int i = 0; i < ROI.size(); ++i)
  191. {
  192. printf("Analog %d - TfLite\n", i);
  193. ioresize = "/sdcard/img_tmp/ra" + std::to_string(i) + ".bmp";
  194. ioresize = FormatFileName(ioresize);
  195. float f1, f2;
  196. f1 = 0; f2 = 0;
  197. #ifndef OHNETFLITE
  198. // LogFile.WriteToFile("ClassFlowAnalog::doNeuralNetwork vor CNN tflite->LoadInputImage(ioresize)");
  199. // tflite->LoadInputImage(ioresize);
  200. tflite->LoadInputImageBasis(ROI[i]->image);
  201. tflite->Invoke();
  202. if (debugdetailanalog) LogFile.WriteToFile("Nach Invoke");
  203. f1 = tflite->GetOutputValue(0);
  204. f2 = tflite->GetOutputValue(1);
  205. #endif
  206. float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
  207. // printf("Result sin, cos, ziffer: %f, %f, %f\n", f1, f2, result);
  208. ROI[i]->result = result * 10;
  209. printf("Result Analog%i: %f\n", i, ROI[i]->result);
  210. if (isLogImage)
  211. {
  212. LogImage(logPath, ROI[i]->name, &ROI[i]->result, NULL, time, ROI[i]->image_org);
  213. }
  214. }
  215. #ifndef OHNETFLITE
  216. delete tflite;
  217. #endif
  218. return true;
  219. }
  220. std::vector<HTMLInfo*> ClassFlowAnalog::GetHTMLInfo()
  221. {
  222. std::vector<HTMLInfo*> result;
  223. for (int i = 0; i < ROI.size(); ++i)
  224. {
  225. HTMLInfo *zw = new HTMLInfo;
  226. zw->filename = ROI[i]->name + ".jpg";
  227. zw->val = ROI[i]->result;
  228. result.push_back(zw);
  229. }
  230. return result;
  231. }