| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- #include "ClassFlowAlignment.h"
- #include "ClassFlowMakeImage.h"
- #include "ClassFlow.h"
- #include "server_tflite.h"
- #include "CRotateImage.h"
- #include "esp_log.h"
- #include "ClassLogFile.h"
- #include "../../include/defines.h"
- static const char *TAG = "ALIGN";
- // #define DEBUG_DETAIL_ON
- void ClassFlowAlignment::SetInitialParameter(void)
- {
- initalrotate = 0;
- anz_ref = 0;
- initialmirror = false;
- use_antialiasing = false;
- initialflip = false;
- SaveAllFiles = false;
- namerawimage = "/sdcard/img_tmp/raw.jpg";
- FileStoreRefAlignment = "/sdcard/config/align.txt";
- ListFlowControll = NULL;
- AlignAndCutImage = NULL;
- ImageBasis = NULL;
- ImageTMP = NULL;
- #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
- AlgROI = (ImageData*)heap_caps_malloc(sizeof(ImageData), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
- #endif
- previousElement = NULL;
- disabled = false;
- SAD_criteria = 0.05;
- }
- ClassFlowAlignment::ClassFlowAlignment(std::vector<ClassFlow*>* lfc)
- {
- SetInitialParameter();
- ListFlowControll = lfc;
- for (int i = 0; i < ListFlowControll->size(); ++i)
- {
- if (((*ListFlowControll)[i])->name().compare("ClassFlowMakeImage") == 0)
- {
- ImageBasis = ((ClassFlowMakeImage*) (*ListFlowControll)[i])->rawImage;
- }
- }
- if (!ImageBasis) // the function take pictures does not exist --> must be created first ONLY FOR TEST PURPOSES
- {
- ESP_LOGD(TAG, "CImageBasis had to be created");
- ImageBasis = new CImageBasis(namerawimage);
- }
- }
- bool ClassFlowAlignment::ReadParameter(FILE* pfile, string& aktparamgraph)
- {
- std::vector<string> splitted;
- int suchex = 40;
- int suchey = 40;
- int alg_algo = 0;
- aktparamgraph = trim(aktparamgraph);
- if (aktparamgraph.size() == 0)
- if (!this->GetNextParagraph(pfile, aktparamgraph))
- return false;
- if (aktparamgraph.compare("[Alignment]") != 0) //Paragraph does not fit MakeImage
- return false;
- while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
- {
- splitted = ZerlegeZeile(aktparamgraph);
- if ((toUpper(splitted[0]) == "FLIPIMAGESIZE") && (splitted.size() > 1))
- {
- if (toUpper(splitted[1]) == "TRUE")
- initialflip = true;
- }
- if ((toUpper(splitted[0]) == "INITIALMIRROR") && (splitted.size() > 1))
- {
- if (toUpper(splitted[1]) == "TRUE")
- initialmirror = true;
- }
- if (((toUpper(splitted[0]) == "INITALROTATE") || (toUpper(splitted[0]) == "INITIALROTATE")) && (splitted.size() > 1))
- {
- this->initalrotate = std::stod(splitted[1]);
- }
- if ((toUpper(splitted[0]) == "SEARCHFIELDX") && (splitted.size() > 1))
- {
- suchex = std::stod(splitted[1]);
- }
- if ((toUpper(splitted[0]) == "SEARCHFIELDY") && (splitted.size() > 1))
- {
- suchey = std::stod(splitted[1]);
- }
- if ((toUpper(splitted[0]) == "ANTIALIASING") && (splitted.size() > 1))
- {
- if (toUpper(splitted[1]) == "TRUE")
- use_antialiasing = true;
- }
- if ((splitted.size() == 3) && (anz_ref < 2))
- {
- References[anz_ref].image_file = FormatFileName("/sdcard" + splitted[0]);
- References[anz_ref].target_x = std::stod(splitted[1]);
- References[anz_ref].target_y = std::stod(splitted[2]);
- anz_ref++;
- }
- if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
- {
- if (toUpper(splitted[1]) == "TRUE")
- SaveAllFiles = true;
- }
- if ((toUpper(splitted[0]) == "ALIGNMENTALGO") && (splitted.size() > 1))
- {
- #ifdef DEBUG_DETAIL_ON
- std::string zw2 = "Alignment mode selected: " + splitted[1];
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
- #endif
- if (toUpper(splitted[1]) == "HIGHACCURACY")
- alg_algo = 1;
- if (toUpper(splitted[1]) == "FAST")
- alg_algo = 2;
- }
- }
- for (int i = 0; i < anz_ref; ++i)
- {
- References[i].search_x = suchex;
- References[i].search_y = suchey;
- References[i].fastalg_SAD_criteria = SAD_criteria;
- References[i].alignment_algo = alg_algo;
- #ifdef DEBUG_DETAIL_ON
- std::string zw2 = "Alignment mode written: " + std::to_string(alg_algo);
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw2);
- #endif
- }
- LoadReferenceAlignmentValues();
-
- return true;
- }
- string ClassFlowAlignment::getHTMLSingleStep(string host)
- {
- string result;
- result = "<p>Rotated Image: </p> <p><img src=\"" + host + "/img_tmp/rot.jpg\"></p>\n";
- result = result + "<p>Found Alignment: </p> <p><img src=\"" + host + "/img_tmp/rot_roi.jpg\"></p>\n";
- result = result + "<p>Aligned Image: </p> <p><img src=\"" + host + "/img_tmp/alg.jpg\"></p>\n";
- return result;
- }
- bool ClassFlowAlignment::doFlow(string time)
- {
- #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
- if (!AlgROI) // AlgROI needs to be allocated before ImageTMP to avoid heap fragmentation
- {
- AlgROI = (ImageData*)heap_caps_realloc(AlgROI, sizeof(ImageData), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
- if (!AlgROI)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlgROI");
- LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
- }
- }
- if (AlgROI)
- {
- ImageBasis->writeToMemoryAsJPG((ImageData*)AlgROI, 90);
- }
- #endif
- if (!ImageTMP)
- {
- ImageTMP = new CImageBasis(ImageBasis);
- if (!ImageTMP)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate ImageTMP -> Exec this round aborted!");
- LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
- return false;
- }
- }
- delete AlignAndCutImage;
- AlignAndCutImage = new CAlignAndCutImage(ImageBasis, ImageTMP);
- if (!AlignAndCutImage)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can't allocate AlignAndCutImage -> Exec this round aborted!");
- LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
- return false;
- }
- CRotateImage rt(AlignAndCutImage, ImageTMP, initialflip);
- if (initialflip)
- {
- int _zw = ImageBasis->height;
- ImageBasis->height = ImageBasis->width;
- ImageBasis->width = _zw;
- }
- if (initialmirror)
- {
- ESP_LOGD(TAG, "do mirror");
- rt.Mirror();
-
- if (SaveAllFiles)
- AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/mirror.jpg"));
- }
-
- if ((initalrotate != 0) || initialflip)
- {
- if (use_antialiasing)
- rt.RotateAntiAliasing(initalrotate);
- else
- rt.Rotate(initalrotate);
-
- if (SaveAllFiles)
- AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/rot.jpg"));
- }
- if (!AlignAndCutImage->Align(&References[0], &References[1]))
- {
- SaveReferenceAlignmentValues();
- }
- #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
- if (AlgROI) {
- DrawRef(ImageTMP);
- tfliteflow.DigitalDrawROI(ImageTMP);
- tfliteflow.AnalogDrawROI(ImageTMP);
- ImageTMP->writeToMemoryAsJPG((ImageData*)AlgROI, 90);
- }
- #endif
-
- if (SaveAllFiles)
- {
- if (initialflip)
- {
- int _zw = ImageTMP->width;
- ImageTMP->width = ImageTMP->height;
- ImageTMP->height = _zw;
- }
- AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg"));
- ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg"));
- }
- // must be deleted to have memory space for loading tflite
- delete ImageTMP;
- ImageTMP = NULL;
- LoadReferenceAlignmentValues();
- return true;
- }
- void ClassFlowAlignment::SaveReferenceAlignmentValues()
- {
- FILE* pFile;
- std::string zwtime, zwvalue;
- pFile = fopen(FileStoreRefAlignment.c_str(), "w");
- if (strlen(zwtime.c_str()) == 0)
- {
- time_t rawtime;
- struct tm* timeinfo;
- char buffer[80];
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
- zwtime = std::string(buffer);
- }
- fputs(zwtime.c_str(), pFile);
- fputs("\n", pFile);
- zwvalue = std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_y);
- zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_SAD)+ "\t" +std::to_string(References[0].fastalg_min);
- zwvalue = zwvalue + "\t" +std::to_string(References[0].fastalg_max)+ "\t" +std::to_string(References[0].fastalg_avg);
- fputs(zwvalue.c_str(), pFile);
- fputs("\n", pFile);
- zwvalue = std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_y);
- zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_SAD)+ "\t" +std::to_string(References[1].fastalg_min);
- zwvalue = zwvalue + "\t" +std::to_string(References[1].fastalg_max)+ "\t" +std::to_string(References[1].fastalg_avg);
- fputs(zwvalue.c_str(), pFile);
- fputs("\n", pFile);
- fclose(pFile);
- }
- bool ClassFlowAlignment::LoadReferenceAlignmentValues(void)
- {
- FILE* pFile;
- char zw[1024];
- string zwvalue;
- std::vector<string> splitted;
- pFile = fopen(FileStoreRefAlignment.c_str(), "r");
- if (pFile == NULL)
- return false;
- fgets(zw, 1024, pFile);
- ESP_LOGD(TAG, "%s", zw);
- fgets(zw, 1024, pFile);
- splitted = ZerlegeZeile(std::string(zw), " \t");
- if (splitted.size() < 6)
- {
- fclose(pFile);
- return false;
- }
- References[0].fastalg_x = stoi(splitted[0]);
- References[0].fastalg_y = stoi(splitted[1]);
- References[0].fastalg_SAD = stof(splitted[2]);
- References[0].fastalg_min = stoi(splitted[3]);
- References[0].fastalg_max = stoi(splitted[4]);
- References[0].fastalg_avg = stof(splitted[5]);
- fgets(zw, 1024, pFile);
- splitted = ZerlegeZeile(std::string(zw));
- if (splitted.size() < 6)
- {
- fclose(pFile);
- return false;
- }
- References[1].fastalg_x = stoi(splitted[0]);
- References[1].fastalg_y = stoi(splitted[1]);
- References[1].fastalg_SAD = stof(splitted[2]);
- References[1].fastalg_min = stoi(splitted[3]);
- References[1].fastalg_max = stoi(splitted[4]);
- References[1].fastalg_avg = stof(splitted[5]);
- fclose(pFile);
- /*#ifdef DEBUG_DETAIL_ON
- std::string _zw = "\tLoadReferences[0]\tx,y:\t" + std::to_string(References[0].fastalg_x) + "\t" + std::to_string(References[0].fastalg_x);
- _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[0].fastalg_SAD) + "\t" + std::to_string(References[0].fastalg_min);
- _zw = _zw + "\t" + std::to_string(References[0].fastalg_max) + "\t" + std::to_string(References[0].fastalg_avg);
- LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
- _zw = "\tLoadReferences[1]\tx,y:\t" + std::to_string(References[1].fastalg_x) + "\t" + std::to_string(References[1].fastalg_x);
- _zw = _zw + "\tSAD, min, max, avg:\t" + std::to_string(References[1].fastalg_SAD) + "\t" + std::to_string(References[1].fastalg_min);
- _zw = _zw + "\t" + std::to_string(References[1].fastalg_max) + "\t" + std::to_string(References[1].fastalg_avg);
- LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", _zw);
- #endif*/
- return true;
- }
- void ClassFlowAlignment::DrawRef(CImageBasis *_zw)
- {
- if (_zw->ImageOkay())
- {
- _zw->drawRect(References[0].target_x, References[0].target_y, References[0].width, References[0].height, 255, 0, 0, 2);
- _zw->drawRect(References[1].target_x, References[1].target_y, References[1].width, References[1].height, 255, 0, 0, 2);
- }
- }
|