ClassFlowAlignment.cpp 13 KB

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