ClassFlowAlignment.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. initalrotate = 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]) == "INITALROTATE") || (toUpper(splitted[0]) == "INITIALROTATE")) && (splitted.size() > 1))
  76. {
  77. this->initalrotate = 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. <<<<<<< HEAD
  163. ImageTMP = new CImageBasis("tmpImage", ImageBasis); // Make sure the name does not get change, it is relevant for the PSRAM allocation!
  164. =======
  165. ImageTMP = new CImageBasis("ImageTMP", ImageBasis);
  166. >>>>>>> master
  167. if (!ImageTMP)
  168. {
  169. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate tmpImage -> Exec this round aborted!");
  170. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  171. return false;
  172. }
  173. }
  174. delete AlignAndCutImage;
  175. AlignAndCutImage = new CAlignAndCutImage("AlignAndCutImage", ImageBasis, ImageTMP);
  176. if (!AlignAndCutImage)
  177. {
  178. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlignAndCutImage -> Exec this round aborted!");
  179. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  180. return false;
  181. }
  182. CRotateImage rt("rawImage", AlignAndCutImage, ImageTMP, initialflip);
  183. if (initialflip)
  184. {
  185. int _zw = ImageBasis->height;
  186. ImageBasis->height = ImageBasis->width;
  187. ImageBasis->width = _zw;
  188. _zw = ImageTMP->width;
  189. ImageTMP->width = ImageTMP->height;
  190. ImageTMP->height = _zw;
  191. }
  192. if (initialmirror)
  193. {
  194. ESP_LOGD(TAG, "do mirror");
  195. rt.Mirror();
  196. if (SaveAllFiles)
  197. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/mirror.jpg"));
  198. }
  199. if ((initalrotate != 0) || initialflip)
  200. {
  201. if (use_antialiasing)
  202. rt.RotateAntiAliasing(initalrotate);
  203. else
  204. rt.Rotate(initalrotate);
  205. if (SaveAllFiles)
  206. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
  207. }
  208. //no align algo if set to 3 = off //add disable aligment algo |01.2023
  209. if(References[0].alignment_algo != 3){
  210. if (!AlignAndCutImage->Align(&References[0], &References[1]))
  211. {
  212. SaveReferenceAlignmentValues();
  213. }
  214. }// no align
  215. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  216. if (AlgROI) {
  217. //no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  218. if(References[0].alignment_algo != 3){
  219. DrawRef(ImageTMP);
  220. }
  221. flowctrl.DigitalDrawROI(ImageTMP);
  222. flowctrl.AnalogDrawROI(ImageTMP);
  223. ImageTMP->writeToMemoryAsJPG((ImageData*)AlgROI, 90);
  224. }
  225. #endif
  226. if (SaveAllFiles)
  227. {
  228. AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
  229. ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
  230. }
  231. // must be deleted to have memory space for loading tflite
  232. delete ImageTMP;
  233. ImageTMP = NULL;
  234. //no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  235. if(References[0].alignment_algo != 3){
  236. LoadReferenceAlignmentValues();
  237. }
  238. return true;
  239. }
  240. void ClassFlowAlignment::SaveReferenceAlignmentValues()
  241. {
  242. FILE* pFile;
  243. std::string zwtime, zwvalue;
  244. pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  245. if (strlen(zwtime.c_str()) == 0)
  246. {
  247. time_t rawtime;
  248. struct tm* timeinfo;
  249. char buffer[80];
  250. time(&rawtime);
  251. timeinfo = localtime(&rawtime);
  252. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  253. zwtime = std::string(buffer);
  254. }
  255. fputs(zwtime.c_str(), pFile);
  256. fputs("\n", pFile);
  257. zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  258. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_SAD)+ "\t" +std::to_string(References[0].fastalg_min);
  259. zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_max)+ "\t" +std::to_string(References[0].fastalg_avg);
  260. fputs(zwvalue.c_str(), pFile);
  261. fputs("\n", pFile);
  262. zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  263. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_SAD)+ "\t" +std::to_string(References[1].fastalg_min);
  264. zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_max)+ "\t" +std::to_string(References[1].fastalg_avg);
  265. fputs(zwvalue.c_str(), pFile);
  266. fputs("\n", pFile);
  267. fclose(pFile);
  268. }
  269. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  270. {
  271. FILE* pFile;
  272. char zw[1024];
  273. string zwvalue;
  274. std::vector<string> splitted;
  275. pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  276. if (pFile == NULL)
  277. return false;
  278. fgets(zw, 1024, pFile);
  279. ESP_LOGD(TAG, "%s", zw);
  280. fgets(zw, 1024, pFile);
  281. splitted = ZerlegeZeile(std::string(zw), " \t");
  282. if (splitted.size() < 6)
  283. {
  284. fclose(pFile);
  285. return false;
  286. }
  287. References[0].fastalg_x = stoi(splitted[0]);
  288. References[0].fastalg_y = stoi(splitted[1]);
  289. References[0].fastalg_SAD = stof(splitted[2]);
  290. References[0].fastalg_min = stoi(splitted[3]);
  291. References[0].fastalg_max = stoi(splitted[4]);
  292. References[0].fastalg_avg = stof(splitted[5]);
  293. fgets(zw, 1024, pFile);
  294. splitted = ZerlegeZeile(std::string(zw));
  295. if (splitted.size() < 6)
  296. {
  297. fclose(pFile);
  298. return false;
  299. }
  300. References[1].fastalg_x = stoi(splitted[0]);
  301. References[1].fastalg_y = stoi(splitted[1]);
  302. References[1].fastalg_SAD = stof(splitted[2]);
  303. References[1].fastalg_min = stoi(splitted[3]);
  304. References[1].fastalg_max = stoi(splitted[4]);
  305. References[1].fastalg_avg = stof(splitted[5]);
  306. fclose(pFile);
  307. /*#ifdef DEBUG_DETAIL_ON
  308. std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
  309. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  310. _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  311. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  312. _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
  313. _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  314. _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  315. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
  316. #endif*/
  317. return true;
  318. }
  319. void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
  320. {
  321. if (_zw->ImageOkay())
  322. {
  323. _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  324. _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  325. }
  326. }