ClassFlowAlignment.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include "ClassFlowAlignment.h"
  2. #include "ClassFlowTakeImage.h"
  3. #include "ClassFlow.h"
  4. #include "server_tflite.h"
  5. #include "CRotateImage.h"
  6. #include "esp_log.h"
  7. #include "ClassLogFile.h"
  8. #include "../../include/defines.h"
  9. static const char *TAG = "ALIGN";
  10. // #define DEBUG_DETAIL_ON
  11. void ClassFlowAlignment::SetInitialParameter(void)
  12. {
  13. initalrotate = 0;
  14. anz_ref = 0;
  15. initialmirror = false;
  16. use_antialiasing = false;
  17. initialflip = false;
  18. SaveAllFiles = false;
  19. namerawimage = "/sdcard/img_tmp/raw.jpg";
  20. FileStoreRefAlignment = "/sdcard/config/align.txt";
  21. ListFlowControll = NULL;
  22. AlignAndCutImage = NULL;
  23. ImageBasis = NULL;
  24. ImageTMP = NULL;
  25. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  26. AlgROI = (ImageData*)heap_caps_malloc(sizeof(ImageData), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  27. #endif
  28. previousElement = NULL;
  29. disabled = false;
  30. SAD_criteria = 0.05;
  31. }
  32. ClassFlowAlignment::ClassFlowAlignment(std::vector<ClassFlow*>* lfc)
  33. {
  34. SetInitialParameter();
  35. ListFlowControll = lfc;
  36. for (int i = 0; i < ListFlowControll->size(); ++i)
  37. {
  38. if (((*ListFlowControll)[i])->name().compare("ClassFlowTakeImage") == 0)
  39. {
  40. ImageBasis = ((ClassFlowTakeImage*) (*ListFlowControll)[i])->rawImage;
  41. }
  42. }
  43. if (!ImageBasis) // the function take pictures does not exist --> must be created first ONLY FOR TEST PURPOSES
  44. {
  45. ESP_LOGD(TAG, "CImageBasis had to be created");
  46. ImageBasis = new CImageBasis(namerawimage);
  47. }
  48. }
  49. bool ClassFlowAlignment::ReadParameter(FILE* pfile, string& aktparamgraph)
  50. {
  51. std::vector<string> splitted;
  52. int suchex = 40;
  53. int suchey = 40;
  54. int alg_algo = 0;
  55. aktparamgraph = trim(aktparamgraph);
  56. if (aktparamgraph.size() == 0)
  57. if (!this->GetNextParagraph(pfile, aktparamgraph))
  58. return false;
  59. if (aktparamgraph.compare("[Alignment]") != 0) //Paragraph does not fit Alignment
  60. return false;
  61. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  62. {
  63. splitted = ZerlegeZeile(aktparamgraph);
  64. if ((toUpper(splitted[0]) == "FLIPIMAGESIZE") && (splitted.size() > 1))
  65. {
  66. if (toUpper(splitted[1]) == "TRUE")
  67. initialflip = true;
  68. }
  69. if ((toUpper(splitted[0]) == "INITIALMIRROR") && (splitted.size() > 1))
  70. {
  71. if (toUpper(splitted[1]) == "TRUE")
  72. initialmirror = true;
  73. }
  74. if (((toUpper(splitted[0]) == "INITALROTATE") || (toUpper(splitted[0]) == "INITIALROTATE")) && (splitted.size() > 1))
  75. {
  76. this->initalrotate = std::stod(splitted[1]);
  77. }
  78. if ((toUpper(splitted[0]) == "SEARCHFIELDX") && (splitted.size() > 1))
  79. {
  80. suchex = std::stod(splitted[1]);
  81. }
  82. if ((toUpper(splitted[0]) == "SEARCHFIELDY") && (splitted.size() > 1))
  83. {
  84. suchey = std::stod(splitted[1]);
  85. }
  86. if ((toUpper(splitted[0]) == "ANTIALIASING") && (splitted.size() > 1))
  87. {
  88. if (toUpper(splitted[1]) == "TRUE")
  89. use_antialiasing = true;
  90. }
  91. if ((splitted.size() == 3) && (anz_ref < 2))
  92. {
  93. References[anz_ref].image_file = FormatFileName("/sdcard" + splitted[0]);
  94. References[anz_ref].target_x = std::stod(splitted[1]);
  95. References[anz_ref].target_y = std::stod(splitted[2]);
  96. anz_ref++;
  97. }
  98. if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  99. {
  100. if (toUpper(splitted[1]) == "TRUE")
  101. SaveAllFiles = true;
  102. }
  103. if ((toUpper(splitted[0]) == "ALIGNMENTALGO") && (splitted.size() > 1))
  104. {
  105. #ifdef DEBUG_DETAIL_ON
  106. std::string zw2 = "Alignment mode selected: " + splitted[1];
  107. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  108. #endif
  109. if (toUpper(splitted[1]) == "HIGHACCURACY")
  110. alg_algo = 1;
  111. if (toUpper(splitted[1]) == "FAST")
  112. alg_algo = 2;
  113. }
  114. }
  115. for (int i = 0; i < anz_ref; ++i)
  116. {
  117. References[i].search_x = suchex;
  118. References[i].search_y = suchey;
  119. References[i].fastalg_SAD_criteria = SAD_criteria;
  120. References[i].alignment_algo = alg_algo;
  121. #ifdef DEBUG_DETAIL_ON
  122. std::string zw2 = "Alignment mode written: " + std::to_string(alg_algo);
  123. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  124. #endif
  125. }
  126. LoadReferenceAlignmentValues();
  127. return true;
  128. }
  129. string ClassFlowAlignment::getHTMLSingleStep(string host)
  130. {
  131. string result;
  132. result = "<p>Rotated Image: </p> <p><img src=\"" + host + "/img_tmp/rot.jpg\"></p>\n";
  133. result = result + "<p>Found Alignment: </p> <p><img src=\"" + host + "/img_tmp/rot_roi.jpg\"></p>\n";
  134. result = result + "<p>Aligned Image: </p> <p><img src=\"" + host + "/img_tmp/alg.jpg\"></p>\n";
  135. return result;
  136. }
  137. bool ClassFlowAlignment::doFlow(string time)
  138. {
  139. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  140. if (!AlgROI) // AlgROI needs to be allocated before ImageTMP to avoid heap fragmentation
  141. {
  142. AlgROI = (ImageData*)heap_caps_realloc(AlgROI, sizeof(ImageData), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  143. if (!AlgROI)
  144. {
  145. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlgROI");
  146. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  147. }
  148. }
  149. if (AlgROI)
  150. {
  151. ImageBasis->writeToMemoryAsJPG((ImageData*)AlgROI, 90);
  152. }
  153. #endif
  154. if (!ImageTMP)
  155. {
  156. ImageTMP = new CImageBasis(ImageBasis);
  157. if (!ImageTMP)
  158. {
  159. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate ImageTMP -> Exec this round aborted!");
  160. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  161. return false;
  162. }
  163. }
  164. delete AlignAndCutImage;
  165. AlignAndCutImage = new CAlignAndCutImage(ImageBasis, ImageTMP);
  166. if (!AlignAndCutImage)
  167. {
  168. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlignAndCutImage -> Exec this round aborted!");
  169. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  170. return false;
  171. }
  172. CRotateImage rt(AlignAndCutImage, ImageTMP, initialflip);
  173. if (initialflip)
  174. {
  175. int _zw = ImageBasis->height;
  176. ImageBasis->height = ImageBasis->width;
  177. ImageBasis->width = _zw;
  178. _zw = ImageTMP->width;
  179. ImageTMP->width = ImageTMP->height;
  180. ImageTMP->height = _zw;
  181. }
  182. if (initialmirror)
  183. {
  184. ESP_LOGD(TAG, "do mirror");
  185. rt.Mirror();
  186. if (SaveAllFiles)
  187. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/mirror.jpg"));
  188. }
  189. if ((initalrotate != 0) || initialflip)
  190. {
  191. if (use_antialiasing)
  192. rt.RotateAntiAliasing(initalrotate);
  193. else
  194. rt.Rotate(initalrotate);
  195. if (SaveAllFiles)
  196. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
  197. }
  198. if (!AlignAndCutImage->Align(&References[0], &References[1]))
  199. {
  200. SaveReferenceAlignmentValues();
  201. }
  202. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  203. if (AlgROI) {
  204. DrawRef(ImageTMP);
  205. tfliteflow.DigitalDrawROI(ImageTMP);
  206. tfliteflow.AnalogDrawROI(ImageTMP);
  207. ImageTMP->writeToMemoryAsJPG((ImageData*)AlgROI, 90);
  208. }
  209. #endif
  210. if (SaveAllFiles)
  211. {
  212. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
  213. ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
  214. }
  215. // must be deleted to have memory space for loading tflite
  216. delete ImageTMP;
  217. ImageTMP = NULL;
  218. LoadReferenceAlignmentValues();
  219. return true;
  220. }
  221. void ClassFlowAlignment::SaveReferenceAlignmentValues()
  222. {
  223. FILE* pFile;
  224. std::string zwtime, zwvalue;
  225. pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  226. if (strlen(zwtime.c_str()) == 0)
  227. {
  228. time_t rawtime;
  229. struct tm* timeinfo;
  230. char buffer[80];
  231. time(&rawtime);
  232. timeinfo = localtime(&rawtime);
  233. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  234. zwtime = std::string(buffer);
  235. }
  236. fputs(zwtime.c_str(), pFile);
  237. fputs("\n", pFile);
  238. zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  239. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_SAD)+ "\t" +std::to_string(References[0].fastalg_min);
  240. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_max)+ "\t" +std::to_string(References[0].fastalg_avg);
  241. fputs(zwvalue.c_str(), pFile);
  242. fputs("\n", pFile);
  243. zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  244. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_SAD)+ "\t" +std::to_string(References[1].fastalg_min);
  245. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_max)+ "\t" +std::to_string(References[1].fastalg_avg);
  246. fputs(zwvalue.c_str(), pFile);
  247. fputs("\n", pFile);
  248. fclose(pFile);
  249. }
  250. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  251. {
  252. FILE* pFile;
  253. char zw[1024];
  254. string zwvalue;
  255. std::vector<string> splitted;
  256. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  257. if (pFile == NULL)
  258. return false;
  259. fgets(zw, 1024, pFile);
  260. ESP_LOGD(TAG, "%s", zw);
  261. fgets(zw, 1024, pFile);
  262. splitted = ZerlegeZeile(std::string(zw), " \t");
  263. if (splitted.size() < 6)
  264. {
  265. fclose(pFile);
  266. return false;
  267. }
  268. References[0].fastalg_x = stoi(splitted[0]);
  269. References[0].fastalg_y = stoi(splitted[1]);
  270. References[0].fastalg_SAD = stof(splitted[2]);
  271. References[0].fastalg_min = stoi(splitted[3]);
  272. References[0].fastalg_max = stoi(splitted[4]);
  273. References[0].fastalg_avg = stof(splitted[5]);
  274. fgets(zw, 1024, pFile);
  275. splitted = ZerlegeZeile(std::string(zw));
  276. if (splitted.size() < 6)
  277. {
  278. fclose(pFile);
  279. return false;
  280. }
  281. References[1].fastalg_x = stoi(splitted[0]);
  282. References[1].fastalg_y = stoi(splitted[1]);
  283. References[1].fastalg_SAD = stof(splitted[2]);
  284. References[1].fastalg_min = stoi(splitted[3]);
  285. References[1].fastalg_max = stoi(splitted[4]);
  286. References[1].fastalg_avg = stof(splitted[5]);
  287. fclose(pFile);
  288. /*#ifdef DEBUG_DETAIL_ON
  289. std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
  290. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  291. _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  292. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  293. _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
  294. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  295. _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  296. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  297. #endif*/
  298. return true;
  299. }
  300. void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
  301. {
  302. if (_zw->ImageOkay())
  303. {
  304. _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  305. _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  306. }
  307. }