ClassFlowAnalog.cpp 8.3 KB

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