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