ClassFlowAlignment.cpp 13 KB

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