ClassFlowAlignment.cpp 13 KB

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