ClassFlowAlignment.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "defines.h"
  2. #include "ClassFlowAlignment.h"
  3. #include "ClassControllCamera.h"
  4. #include "ClassFlowTakeImage.h"
  5. #include "ClassFlow.h"
  6. #include "MainFlowControl.h"
  7. #include "CRotateImage.h"
  8. #include "esp_log.h"
  9. #include "ClassLogFile.h"
  10. #include "psram.h"
  11. static const char *TAG = "ALIGN";
  12. void ClassFlowAlignment::SetInitialParameter(void)
  13. {
  14. anz_ref = 0;
  15. Camera.ImageInitialRotate = 0;
  16. Camera.ImageAntialiasing = false;
  17. Camera.ImageInitialFlip = false;
  18. namerawimage = "/sdcard/img_tmp/raw.jpg";
  19. FileStoreRefAlignment = "/sdcard/config/align.txt";
  20. ListFlowControll = NULL;
  21. AlignAndCutImage = NULL;
  22. ImageBasis = NULL;
  23. ImageTMP = NULL;
  24. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  25. AlgROI = (ImageData *)malloc_psram_heap(std::string(TAG) + "->AlgROI", sizeof(ImageData), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  26. #endif
  27. previousElement = NULL;
  28. disabled = false;
  29. SAD_criteria = 0.05;
  30. }
  31. ClassFlowAlignment::ClassFlowAlignment(std::vector<ClassFlow *> *lfc)
  32. {
  33. SetInitialParameter();
  34. ListFlowControll = lfc;
  35. for (int i = 0; i < ListFlowControll->size(); ++i)
  36. {
  37. if (((*ListFlowControll)[i])->name().compare("ClassFlowTakeImage") == 0)
  38. {
  39. ImageBasis = ((ClassFlowTakeImage *)(*ListFlowControll)[i])->rawImage;
  40. }
  41. }
  42. // the function take pictures does not exist --> must be created first ONLY FOR TEST PURPOSES
  43. if (!ImageBasis)
  44. {
  45. ESP_LOGD(TAG, "CImageBasis had to be created");
  46. ImageBasis = new CImageBasis("ImageBasis", namerawimage);
  47. }
  48. }
  49. bool ClassFlowAlignment::ReadParameter(FILE *pfile, std::string &aktparamgraph)
  50. {
  51. aktparamgraph = trim_string_left_right(aktparamgraph);
  52. if (aktparamgraph.size() == 0)
  53. {
  54. if (!GetNextParagraph(pfile, aktparamgraph))
  55. {
  56. return false;
  57. }
  58. }
  59. if ((to_upper(aktparamgraph).compare("[ALIGNMENT]") != 0) && (to_upper(aktparamgraph).compare(";[ALIGNMENT]") != 0))
  60. {
  61. // Paragraph does not fit Alignment
  62. return false;
  63. }
  64. int suchex = 20;
  65. int suchey = 20;
  66. int maxangle = 45;
  67. int alg_algo = 0; // default=0; 1 =HIGHACCURACY; 2= FAST; 3= OFF //add disable aligment algo |01.2023
  68. std::vector<std::string> splitted;
  69. while (getNextLine(pfile, &aktparamgraph) && !isNewParagraph(aktparamgraph))
  70. {
  71. splitted = split_line(aktparamgraph);
  72. if (splitted.size() > 1)
  73. {
  74. std::string _param = to_upper(splitted[0]);
  75. if (_param == "FLIPIMAGESIZE")
  76. {
  77. Camera.ImageInitialFlip = alphanumeric_to_boolean(splitted[1]);
  78. }
  79. else if (_param == "INITIALROTATE")
  80. {
  81. if (is_string_numeric(splitted[1]))
  82. {
  83. Camera.ImageInitialRotate = std::stod(splitted[1]);
  84. }
  85. }
  86. else if (_param == "SEARCHFIELDX")
  87. {
  88. if (is_string_numeric(splitted[1]))
  89. {
  90. suchex = clip_int(std::stoi(splitted[1]), 320, 0);
  91. }
  92. }
  93. else if (_param == "SEARCHFIELDY")
  94. {
  95. if (is_string_numeric(splitted[1]))
  96. {
  97. suchey = clip_int(std::stoi(splitted[1]), 240, 0);
  98. }
  99. }
  100. else if (_param == "SEARCHMAXANGLE")
  101. {
  102. if (is_string_numeric(splitted[1]))
  103. {
  104. maxangle = clip_int(std::stoi(splitted[1]), 180, 0);
  105. }
  106. }
  107. else if (_param == "ANTIALIASING")
  108. {
  109. Camera.ImageAntialiasing = alphanumeric_to_boolean(splitted[1]);
  110. }
  111. else if (_param == "ALIGNMENTALGO")
  112. {
  113. if (to_upper(splitted[1]) == "HIGHACCURACY")
  114. {
  115. alg_algo = 1;
  116. }
  117. else if (to_upper(splitted[1]) == "FAST")
  118. {
  119. alg_algo = 2;
  120. }
  121. else if (to_upper(splitted[1]) == "OFF")
  122. {
  123. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  124. alg_algo = 3;
  125. }
  126. }
  127. else if ((splitted.size() == 3) && (anz_ref < 2))
  128. {
  129. if (is_string_numeric(splitted[1]) && is_string_numeric(splitted[2]))
  130. {
  131. References[anz_ref].image_file = format_filename("/sdcard" + splitted[0]);
  132. References[anz_ref].target_x = std::stod(splitted[1]);
  133. References[anz_ref].target_y = std::stod(splitted[2]);
  134. anz_ref++;
  135. }
  136. else
  137. {
  138. References[anz_ref].image_file = format_filename("/sdcard" + splitted[0]);
  139. References[anz_ref].target_x = 10;
  140. References[anz_ref].target_y = 10;
  141. anz_ref++;
  142. }
  143. }
  144. }
  145. }
  146. for (int i = 0; i < anz_ref; ++i)
  147. {
  148. References[i].search_x = suchex;
  149. References[i].search_y = suchey;
  150. References[i].search_max_angle = (float)maxangle;
  151. References[i].fastalg_SAD_criteria = SAD_criteria;
  152. References[i].alignment_algo = alg_algo;
  153. }
  154. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  155. if (References[0].alignment_algo != 3)
  156. {
  157. return LoadReferenceAlignmentValues();
  158. }
  159. return true;
  160. }
  161. std::string ClassFlowAlignment::getHTMLSingleStep(std::string host)
  162. {
  163. std::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(std::string time)
  170. {
  171. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  172. // AlgROI needs to be allocated before ImageTMP to avoid heap fragmentation
  173. if (!AlgROI)
  174. {
  175. AlgROI = (ImageData *)heap_caps_realloc(AlgROI, sizeof(ImageData), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  176. if (!AlgROI)
  177. {
  178. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlgROI");
  179. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  180. }
  181. }
  182. if (AlgROI)
  183. {
  184. ImageBasis->writeToMemoryAsJPG((ImageData *)AlgROI, 90);
  185. }
  186. #endif
  187. if (!ImageTMP)
  188. {
  189. ImageTMP = new CImageBasis("TempImage", ImageBasis); // Make sure the name does not get change, it is relevant for the PSRAM allocation!
  190. if (!ImageTMP)
  191. {
  192. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate TempImage -> Exec this round aborted!");
  193. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  194. return false;
  195. }
  196. }
  197. delete AlignAndCutImage;
  198. AlignAndCutImage = new CAlignAndCutImage("AlignAndCutImage", ImageBasis, ImageTMP);
  199. if (!AlignAndCutImage)
  200. {
  201. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlignAndCutImage -> Exec this round aborted!");
  202. LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
  203. return false;
  204. }
  205. CRotateImage rt("rawImage", AlignAndCutImage, ImageTMP, Camera.ImageInitialFlip);
  206. if (Camera.ImageInitialFlip)
  207. {
  208. int temp_value = ImageBasis->height;
  209. ImageBasis->height = ImageBasis->width;
  210. ImageBasis->width = temp_value;
  211. temp_value = ImageTMP->width;
  212. ImageTMP->width = ImageTMP->height;
  213. ImageTMP->height = temp_value;
  214. }
  215. if (((Camera.ImageInitialRotate > 0) && (Camera.ImageInitialRotate < 360)) || Camera.ImageInitialFlip)
  216. {
  217. if (Camera.ImageAntialiasing)
  218. {
  219. rt.RotateAntiAliasing(Camera.ImageInitialRotate);
  220. }
  221. else
  222. {
  223. rt.Rotate(Camera.ImageInitialRotate);
  224. }
  225. if (Camera.SaveAllFiles)
  226. {
  227. AlignAndCutImage->SaveToFile(format_filename("/sdcard/img_tmp/rot.jpg"));
  228. }
  229. }
  230. // no align algo if set to 3 = off //add disable aligment algo |01.2023
  231. if (References[0].alignment_algo != 3)
  232. {
  233. int res = AlignAndCutImage->Align(&References[0], &References[1]);
  234. if (res >= Alignment_OK)
  235. {
  236. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Alignment OK");
  237. if (res == Fast_Alignment_OK)
  238. {
  239. SaveReferenceAlignmentValues();
  240. }
  241. flowctrl.AlignmentOk = true;
  242. }
  243. else
  244. {
  245. // Alignment failed
  246. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Alignment failed");
  247. flowctrl.AlignmentOk = false;
  248. }
  249. }
  250. else
  251. {
  252. flowctrl.AlignmentOk = true;
  253. }
  254. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  255. if (AlgROI)
  256. {
  257. // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
  258. if (References[0].alignment_algo != 3)
  259. {
  260. DrawRef(ImageTMP);
  261. }
  262. flowctrl.DigitDrawROI(ImageTMP);
  263. flowctrl.AnalogDrawROI(ImageTMP);
  264. ImageTMP->writeToMemoryAsJPG((ImageData *)AlgROI, 90);
  265. }
  266. #endif
  267. if (Camera.SaveAllFiles)
  268. {
  269. AlignAndCutImage->SaveToFile(format_filename("/sdcard/img_tmp/alg.jpg"));
  270. ImageTMP->SaveToFile(format_filename("/sdcard/img_tmp/alg_roi.jpg"));
  271. }
  272. // must be deleted to have memory space for loading tflite
  273. delete ImageTMP;
  274. ImageTMP = NULL;
  275. return true;
  276. }
  277. void ClassFlowAlignment::SaveReferenceAlignmentValues(void)
  278. {
  279. FILE *pFile = fopen(FileStoreRefAlignment.c_str(), "w");
  280. std::string temp_time;
  281. if (strlen(temp_time.c_str()) == 0)
  282. {
  283. time_t rawtime;
  284. struct tm *timeinfo;
  285. char buffer[80];
  286. time(&rawtime);
  287. timeinfo = localtime(&rawtime);
  288. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  289. temp_time = std::string(buffer);
  290. }
  291. fputs(temp_time.c_str(), pFile);
  292. fputs("\n", pFile);
  293. std::string temp_value = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
  294. temp_value = temp_value + "\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
  295. temp_value = temp_value + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
  296. fputs(temp_value.c_str(), pFile);
  297. fputs("\n", pFile);
  298. temp_value = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
  299. temp_value = temp_value + "\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
  300. temp_value = temp_value + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
  301. fputs(temp_value.c_str(), pFile);
  302. fputs("\n", pFile);
  303. fclose(pFile);
  304. }
  305. bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
  306. {
  307. FILE *pFile = fopen(FileStoreRefAlignment.c_str(), "r");
  308. if (pFile == NULL)
  309. {
  310. return false;
  311. }
  312. char temp_bufer[1024];
  313. // erste Zeile: 2025-03-16T18:50:22
  314. if (!fgets(temp_bufer, 1024, pFile))
  315. {
  316. fclose(pFile);
  317. ESP_LOGE(TAG, "/sdcard/config/align.txt empty!");
  318. return false;
  319. }
  320. // zweite Zeile: 177 342 -0.000000 6144 1611659784 0.000000
  321. if (!fgets(temp_bufer, 1024, pFile))
  322. {
  323. fclose(pFile);
  324. ESP_LOGE(TAG, "/sdcard/config/align.txt empty!");
  325. return false;
  326. }
  327. std::vector<std::string> splitted;
  328. splitted = split_line(temp_bufer, "\t");
  329. if (splitted.size() < 6)
  330. {
  331. fclose(pFile);
  332. ESP_LOGE(TAG, "/sdcard/config/align.txt wrong format!");
  333. return false;
  334. }
  335. if (is_string_numeric(splitted[0]) && is_string_numeric(splitted[1]) && is_string_numeric(splitted[2]) &&
  336. is_string_numeric(splitted[3]) && is_string_numeric(splitted[4]) && is_string_numeric(splitted[5]))
  337. {
  338. References[0].fastalg_x = stoi(splitted[0]);
  339. References[0].fastalg_y = stoi(splitted[1]);
  340. References[0].fastalg_SAD = stof(splitted[2]);
  341. References[0].fastalg_min = stoi(splitted[3]);
  342. References[0].fastalg_max = stoi(splitted[4]);
  343. References[0].fastalg_avg = stof(splitted[5]);
  344. }
  345. else
  346. {
  347. fclose(pFile);
  348. ESP_LOGE(TAG, "/sdcard/config/align.txt wrong format!");
  349. return false;
  350. }
  351. // dritte Zeile: 398 145 -0.000000 6144 1611659784 0.000000
  352. if (!fgets(temp_bufer, 1024, pFile))
  353. {
  354. fclose(pFile);
  355. ESP_LOGE(TAG, "/sdcard/config/align.txt empty!");
  356. return false;
  357. }
  358. splitted = split_line(temp_bufer, "\t");
  359. if (splitted.size() < 6)
  360. {
  361. fclose(pFile);
  362. ESP_LOGE(TAG, "/sdcard/config/align.txt wrong format!");
  363. return false;
  364. }
  365. if (is_string_numeric(splitted[0]) && is_string_numeric(splitted[1]) && is_string_numeric(splitted[2]) &&
  366. is_string_numeric(splitted[3]) && is_string_numeric(splitted[4]) && is_string_numeric(splitted[5]))
  367. {
  368. References[1].fastalg_x = stoi(splitted[0]);
  369. References[1].fastalg_y = stoi(splitted[1]);
  370. References[1].fastalg_SAD = stof(splitted[2]);
  371. References[1].fastalg_min = stoi(splitted[3]);
  372. References[1].fastalg_max = stoi(splitted[4]);
  373. References[1].fastalg_avg = stof(splitted[5]);
  374. }
  375. else
  376. {
  377. fclose(pFile);
  378. ESP_LOGE(TAG, "/sdcard/config/align.txt wrong format!");
  379. return false;
  380. }
  381. fclose(pFile);
  382. return true;
  383. }
  384. void ClassFlowAlignment::DrawRef(CImageBasis *Image)
  385. {
  386. if (Image->ImageOkay())
  387. {
  388. Image->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
  389. Image->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
  390. }
  391. }