ClassFlowAlignment.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "ClassFlowAlignment.h"
  2. #include "ClassFlowTakeImage.h"
  3. #include "ClassFlow.h"
  4. #include "MainFlowControl.h"
  5. #include "CRotateImage.h"
  6. #include "esp_log.h"
  7. #include "ClassLogFile.h"
  8. #include "psram.h"
  9. #include "../../include/defines.h"
  10. static const char *TAG = "ALIGN";
  11. // #define DEBUG_DETAIL_ON
  12. void ClassFlowAlignment::SetInitialParameter(void)
  13. {
  14. initialrotate = 0;
  15. anz_ref = 0;
  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 *)malloc_psram_heap(std::string(TAG) + "->AlgROI", 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("ImageBasis", 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; // default=0; 1 =HIGHACCURACY; 2= FAST; 3= OFF //add disable aligment algo |01.2023
  55. aktparamgraph = trim(aktparamgraph);
  56. if (aktparamgraph.size() == 0)
  57. {
  58. if (!this->GetNextParagraph(pfile, aktparamgraph))
  59. {
  60. return false;
  61. }
  62. }
  63. if (aktparamgraph.compare("[Alignment]") != 0)
  64. {
  65. // Paragraph does not fit Alignment
  66. return false;
  67. }
  68. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  69. {
  70. splitted = ZerlegeZeile(aktparamgraph);
  71. if ((toUpper(splitted[0]) == "FLIPIMAGESIZE") && (splitted.size() > 1))
  72. {
  73. if (toUpper(splitted[1]) == "TRUE")
  74. {
  75. initialflip = true;
  76. }
  77. }
  78. else if (((toUpper(splitted[0]) == "initialrotate") || (toUpper(splitted[0]) == "INITIALROTATE")) && (splitted.size() > 1))
  79. {
  80. this->initialrotate = std::stod(splitted[1]);
  81. }
  82. else if ((toUpper(splitted[0]) == "SEARCHFIELDX") && (splitted.size() > 1))
  83. {
  84. suchex = std::stod(splitted[1]);
  85. }
  86. else if ((toUpper(splitted[0]) == "SEARCHFIELDY") && (splitted.size() > 1))
  87. {
  88. suchey = std::stod(splitted[1]);
  89. }
  90. else if ((toUpper(splitted[0]) == "ANTIALIASING") && (splitted.size() > 1))
  91. {
  92. if (toUpper(splitted[1]) == "TRUE")
  93. {
  94. use_antialiasing = true;
  95. }
  96. }
  97. else if ((splitted.size() == 3) && (anz_ref < 2))
  98. {
  99. References[anz_ref].image_file = FormatFileName("/sdcard" + splitted[0]);
  100. References[anz_ref].target_x = std::stod(splitted[1]);
  101. References[anz_ref].target_y = std::stod(splitted[2]);
  102. anz_ref++;
  103. }
  104. else if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  105. {
  106. if (toUpper(splitted[1]) == "TRUE")
  107. {
  108. SaveAllFiles = true;
  109. }
  110. }
  111. else if ((toUpper(splitted[0]) == "ALIGNMENTALGO") && (splitted.size() > 1))
  112. {
  113. #ifdef DEBUG_DETAIL_ON
  114. std::string zw2 = "Alignment mode selected: " + splitted[1];
  115. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  116. #endif
  117. if (toUpper(splitted[1]) == "HIGHACCURACY")
  118. {
  119. alg_algo = 1;
  120. }
  121. if (toUpper(splitted[1]) == "FAST")
  122. {
  123. alg_algo = 2;
  124. }
  125. if (toUpper(splitted[1]) == "OFF")
  126. {
  127. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  128. alg_algo = 3;
  129. }
  130. }
  131. }
  132. for (int i = 0; i < anz_ref; ++i)
  133. {
  134. References[i].search_x = suchex;
  135. References[i].search_y = suchey;
  136. References[i].fastalg_SAD_criteria = SAD_criteria;
  137. References[i].alignment_algo = alg_algo;
  138. #ifdef DEBUG_DETAIL_ON
  139. std::string zw2 = "Alignment mode written: " + std::to_string(alg_algo);
  140. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
  141. #endif
  142. }
  143. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  144. if (References[0].alignment_algo != 3)
  145. {
  146. LoadReferenceAlignmentValues();
  147. }
  148. return true;
  149. }
  150. string ClassFlowAlignment::getHTMLSingleStep(string host)
  151. {
  152. string result;
  153. result = "<p>Rotated Image: </p> <p><img src=\"" + host + "/img_tmp/rot.jpg\"></p>\n";
  154. result = result + "<p>Found Alignment: </p> <p><img src=\"" + host + "/img_tmp/rot_roi.jpg\"></p>\n";
  155. result = result + "<p>Aligned Image: </p> <p><img src=\"" + host + "/img_tmp/alg.jpg\"></p>\n";
  156. return result;
  157. }
  158. bool ClassFlowAlignment::doFlow(string time)
  159. {
  160. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  161. if (!AlgROI) // AlgROI needs to be allocated before ImageTMP to avoid heap fragmentation
  162. {
  163. AlgROI = (ImageData *)heap_caps_realloc(AlgROI, sizeof(ImageData), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  164. if (!AlgROI)
  165. {
  166. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlgROI");
  167. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  168. }
  169. }
  170. if (AlgROI)
  171. {
  172. ImageBasis->writeToMemoryAsJPG((ImageData *)AlgROI, 90);
  173. }
  174. #endif
  175. if (!ImageTMP)
  176. {
  177. ImageTMP = new CImageBasis("tmpImage", ImageBasis); // Make sure the name does not get change, it is relevant for the PSRAM allocation!
  178. if (!ImageTMP)
  179. {
  180. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate tmpImage -> Exec this round aborted!");
  181. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  182. return false;
  183. }
  184. }
  185. delete AlignAndCutImage;
  186. AlignAndCutImage = new CAlignAndCutImage("AlignAndCutImage", ImageBasis, ImageTMP);
  187. if (!AlignAndCutImage)
  188. {
  189. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlignAndCutImage -> Exec this round aborted!");
  190. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  191. return false;
  192. }
  193. CRotateImage rt("rawImage", AlignAndCutImage, ImageTMP, initialflip);
  194. if (initialflip)
  195. {
  196. int _zw = ImageBasis->height;
  197. ImageBasis->height = ImageBasis->width;
  198. ImageBasis->width = _zw;
  199. _zw = ImageTMP->width;
  200. ImageTMP->width = ImageTMP->height;
  201. ImageTMP->height = _zw;
  202. }
  203. if ((initialrotate != 0) || initialflip)
  204. {
  205. if (use_antialiasing)
  206. {
  207. rt.RotateAntiAliasing(initialrotate);
  208. }
  209. else
  210. {
  211. rt.Rotate(initialrotate);
  212. }
  213. if (SaveAllFiles)
  214. {
  215. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
  216. }
  217. }
  218. // no align algo if set to 3 = off //add disable aligment algo |01.2023
  219. if (References[0].alignment_algo != 3)
  220. {
  221. if (!AlignAndCutImage->Align(&References[0], &References[1]))
  222. {
  223. SaveReferenceAlignmentValues();
  224. }
  225. } // no align
  226. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  227. if (AlgROI)
  228. {
  229. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  230. if (References[0].alignment_algo != 3)
  231. {
  232. DrawRef(ImageTMP);
  233. }
  234. flowctrl.DigitalDrawROI(ImageTMP);
  235. flowctrl.AnalogDrawROI(ImageTMP);
  236. ImageTMP->writeToMemoryAsJPG((ImageData *)AlgROI, 90);
  237. }
  238. #endif
  239. if (SaveAllFiles)
  240. {
  241. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
  242. ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
  243. }
  244. // must be deleted to have memory space for loading tflite
  245. delete ImageTMP;
  246. ImageTMP = NULL;
  247. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  248. if (References[0].alignment_algo != 3)
  249. {
  250. LoadReferenceAlignmentValues();
  251. }
  252. return true;
  253. }
  254. void ClassFlowAlignment::SaveReferenceAlignmentValues()
  255. {
  256. FILE *pFile;
  257. std::string zwtime, zwvalue;
  258. pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  259. if (strlen(zwtime.c_str()) == 0)
  260. {
  261. time_t rawtime;
  262. struct tm *timeinfo;
  263. char buffer[80];
  264. time(&rawtime);
  265. timeinfo = localtime(&rawtime);
  266. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  267. zwtime = std::string(buffer);
  268. }
  269. fputs(zwtime.c_str(), pFile);
  270. fputs("\n", pFile);
  271. zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  272. zwvalue = zwvalue + "\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  273. zwvalue = zwvalue + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  274. fputs(zwvalue.c_str(), pFile);
  275. fputs("\n", pFile);
  276. zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  277. zwvalue = zwvalue + "\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  278. zwvalue = zwvalue + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  279. fputs(zwvalue.c_str(), pFile);
  280. fputs("\n", pFile);
  281. fclose(pFile);
  282. }
  283. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  284. {
  285. FILE *pFile;
  286. char zw[1024];
  287. string zwvalue;
  288. std::vector<string> splitted;
  289. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  290. if (pFile == NULL)
  291. return false;
  292. fgets(zw, 1024, pFile);
  293. ESP_LOGD(TAG, "%s", zw);
  294. fgets(zw, 1024, pFile);
  295. splitted = ZerlegeZeile(std::string(zw), " \t");
  296. if (splitted.size() < 6)
  297. {
  298. fclose(pFile);
  299. return false;
  300. }
  301. References[0].fastalg_x = stoi(splitted[0]);
  302. References[0].fastalg_y = stoi(splitted[1]);
  303. References[0].fastalg_SAD = stof(splitted[2]);
  304. References[0].fastalg_min = stoi(splitted[3]);
  305. References[0].fastalg_max = stoi(splitted[4]);
  306. References[0].fastalg_avg = stof(splitted[5]);
  307. fgets(zw, 1024, pFile);
  308. splitted = ZerlegeZeile(std::string(zw));
  309. if (splitted.size() < 6)
  310. {
  311. fclose(pFile);
  312. return false;
  313. }
  314. References[1].fastalg_x = stoi(splitted[0]);
  315. References[1].fastalg_y = stoi(splitted[1]);
  316. References[1].fastalg_SAD = stof(splitted[2]);
  317. References[1].fastalg_min = stoi(splitted[3]);
  318. References[1].fastalg_max = stoi(splitted[4]);
  319. References[1].fastalg_avg = stof(splitted[5]);
  320. fclose(pFile);
  321. /*#ifdef DEBUG_DETAIL_ON
  322. std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
  323. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  324. _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  325. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  326. _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
  327. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  328. _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  329. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  330. #endif*/
  331. return true;
  332. }
  333. void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
  334. {
  335. if (_zw->ImageOkay())
  336. {
  337. _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  338. _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  339. }
  340. }