From dda824f2e7ea634917e74a56eb806c09d00866fe Mon Sep 17 00:00:00 2001 From: Thomas Boy <thomas-boy@idiv.de> Date: Mon, 27 Aug 2018 15:21:22 +0200 Subject: [PATCH] try next bug fix, huge work around for corrupted file test --- Demo.cpp | 484 +++++++++++++++++++++------------------ package_bgs/ttoolbox.cpp | 53 +++++ package_bgs/ttoolbox.h | 12 + 3 files changed, 323 insertions(+), 226 deletions(-) diff --git a/Demo.cpp b/Demo.cpp index 609ea42..6ab12a8 100644 --- a/Demo.cpp +++ b/Demo.cpp @@ -15,7 +15,7 @@ based on original demo.cpp #define PROCESS_CENTER_VERSION_MAJOR 0 #define PROCESS_CENTER_VERSION_MINOR 9 -#define PROCESS_CENTER_VERSION_MINOR_FIXES 4 +#define PROCESS_CENTER_VERSION_MINOR_FIXES 7 //opencv #include <opencv2/opencv.hpp> @@ -46,6 +46,7 @@ bool cmdOptionExists(char** begin, char** end, const std::string& option) int main(int argc, char * argv[]) { + std::cout << "using processcenter " <<PROCESS_CENTER_VERSION_MAJOR <<"."<< PROCESS_CENTER_VERSION_MINOR << endl; std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION <<"."<<PROCESS_CENTER_VERSION_MINOR_FIXES<< std::endl; //!** parse programm input****************/ @@ -224,6 +225,14 @@ int main(int argc, char * argv[]) //std::string fileName = getFileName(begin); std::string staticFile = inputDir+"/bk.jpg"; cout <<"a) load first static background pic :"<< staticFile<<endl; + + //we check for corrupt image + if(TToolBox::checkFileCorrupted(staticFile)) + { + cout<<"error file: "<< staticFile<<" is corrupted, will abort"<<endl; + return EXIT_FAILURE; + } + img_input = imread(staticFile.c_str(), CV_LOAD_IMAGE_COLOR); if(img_input.data ) { @@ -271,6 +280,14 @@ int main(int argc, char * argv[]) fileName = inputDir + TToolBox::getFileName(index); myRandomTrainList.push_back(fileName); + //we check for corrupt image + if(TToolBox::checkFileCorrupted(fileName)) + { + cout<<"error file: "<< fileName<<" is corrupted, will ignore it and draw new"<<endl; + i--; + continue; //we jump over the rest //TODO bad style + } + cout <<"\t"<<i <<"\t of \t"<<amountTrainingSteps<<" rnd file :"<< fileName<<endl; img_input = imread(fileName.c_str(), CV_LOAD_IMAGE_COLOR); @@ -316,6 +333,7 @@ int main(int argc, char * argv[]) vector<string> myFileErrorList;//list for pure io error vector<string> myFileListNoContour; //list for no contours found; vector<string> myFileListAfterContourSelection; //list for no contours found after selection; + vector<string> myFileListCorrupted; //list all corrupted jpg files for(frameCounter=0;frameCounter<amountFiles;frameCounter++) { @@ -328,10 +346,10 @@ int main(int argc, char * argv[]) //we define the type better to prevent error fileName = std::string(inputDir + TToolBox::getFileName(frameCounter)); -// //check the string -// cout <<"open fileName: "<< fileName<<endl; -// if(fileName.empty()) -// cerr <<"error: empty string fileName"<<endl; + // //check the string + // cout <<"open fileName: "<< fileName<<endl; + // if(fileName.empty()) + // cerr <<"error: empty string fileName"<<endl; cout <<"\t"<<frameCounter<<"\tof \t"<<amountFiles<<" load file :"<< fileName<<endl; @@ -341,292 +359,306 @@ int main(int argc, char * argv[]) cout << " d 3: "<< fileName.size()<< " size: "<< fileName<<endl; cout << " d 3: "<< fileNameCV.size()<< " size:"<< fileNameCV.c_str(); - img_input = imread(fileNameCV, CV_LOAD_IMAGE_COLOR); - - cout << " e "<<endl; - - if(img_input.data ) + //we check if the image in NOT corrupted + if(!TToolBox::checkFileCorrupted(fileName)) { - //! we cut out a smaller ROI, - //! step 1) - img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius); + //we load the file + img_input = imread(fileNameCV, CV_LOAD_IMAGE_COLOR); - //! normal bgs processing - //! step 2) - cv::Mat img_mask; - cv::Mat img_bkgmodel; - - bgs->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask image + cout << " e "<<endl; - cout << " f "<<endl; - - //! step 3) we make we apply a edge detection - //TODO make this in a function, how many times is this listed ?? - //TODO read from tapter config - //TODO all parameter from applyCannyEdgeAndCalcCountours also !! - double threshholdMin = 150; - double threshholdMax = 200; - int apertureSize = 3; - std::vector<vector<Point> > contours = TToolBox::applyCannyEdgeAndCalcCountours(img_mask,threshholdMin,threshholdMax,apertureSize); - - //define what we will write down - std::vector<vector<Point> > contourSelection; - vector<Point2f> massCenters; - vector<Point> conHull; - Point2f muConvexHullMassCenter(0.0,0.0); - - if(!contours.empty()) + if(img_input.data ) { - // //TODO: we need to order the points to get a nice polygon, otherwise not usefull - // //we also try to find the aproximate polygon***************** - //// vector<Point> aproxiCurve; + //! we cut out a smaller ROI, + //! step 1) + img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius); + + //! normal bgs processing + //! step 2) + cv::Mat img_mask; + cv::Mat img_bkgmodel; + + bgs->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask image + + cout << " f "<<endl; + + //! step 3) we make we apply a edge detection + //TODO make this in a function, how many times is this listed ?? + //TODO read from tapter config + //TODO all parameter from applyCannyEdgeAndCalcCountours also !! + double threshholdMin = 150; + double threshholdMax = 200; + int apertureSize = 3; + std::vector<vector<Point> > contours = TToolBox::applyCannyEdgeAndCalcCountours(img_mask,threshholdMin,threshholdMax,apertureSize); + + //define what we will write down + std::vector<vector<Point> > contourSelection; + vector<Point2f> massCenters; + vector<Point> conHull; + Point2f muConvexHullMassCenter(0.0,0.0); + + if(!contours.empty()) + { + // //TODO: we need to order the points to get a nice polygon, otherwise not usefull + // //we also try to find the aproximate polygon***************** + //// vector<Point> aproxiCurve; - //// // //see https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a167a8e0a3a3d86e84b70e33483af4466 - //// // if(aproxiCurve::checkVector(10,CV_32F)==-1) - //// // cout<<"error wrong format of vector"<<endl; + //// // //see https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a167a8e0a3a3d86e84b70e33483af4466 + //// // if(aproxiCurve::checkVector(10,CV_32F)==-1) + //// // cout<<"error wrong format of vector"<<endl; - //// //calc 0.1 percent of arc length of convex hull - //// double epsilon = 0.1 * cv::arcLength(hullComplete,true); + //// //calc 0.1 percent of arc length of convex hull + //// double epsilon = 0.1 * cv::arcLength(hullComplete,true); - //// //see https://docs.opencv.org/2.4.13.2/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#approxpolydp - //// cv::approxPolyDP(allContourPoints,aproxiCurve,epsilon,false); + //// //see https://docs.opencv.org/2.4.13.2/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#approxpolydp + //// cv::approxPolyDP(allContourPoints,aproxiCurve,epsilon,false); - ////#ifdef MC_SHOW_STEP_ANALYSE - //// if(aproxiCurve.size()>=2)//we only draw if we have at least a line - //// { - //// Mat imgPolyApr = Mat::zeros( img_input.size(), CV_8UC3 ); - //// imgPolyApr = Scalar(255,255,255); //fille the picture - //// Scalar colorC( 0,0,255,255 );//red + ////#ifdef MC_SHOW_STEP_ANALYSE + //// if(aproxiCurve.size()>=2)//we only draw if we have at least a line + //// { + //// Mat imgPolyApr = Mat::zeros( img_input.size(), CV_8UC3 ); + //// imgPolyApr = Scalar(255,255,255); //fille the picture + //// Scalar colorC( 0,0,255,255 );//red - //// //for(imgPolyApr) + //// //for(imgPolyApr) - //// polylines(imgPolyApr, aproxiCurve, true, colorC, 1, 8); + //// polylines(imgPolyApr, aproxiCurve, true, colorC, 1, 8); - //// cv::String outpath3= "/homes/tb55xemi/work/bugTrainingSet/testRec/rec04379437pp/result/"; - //// std::ostringstream convert3; - //// convert3 << outpath3 << frameCounter <<"_appr_poly.jpg"; - //// cv::imwrite(convert3.str().c_str(), imgPolyApr); - //// } - //// else - //// cout<<"approximate poly has not enough points will skip file: "<<frameCounter<<endl; + //// cv::String outpath3= "/homes/tb55xemi/work/bugTrainingSet/testRec/rec04379437pp/result/"; + //// std::ostringstream convert3; + //// convert3 << outpath3 << frameCounter <<"_appr_poly.jpg"; + //// cv::imwrite(convert3.str().c_str(), imgPolyApr); + //// } + //// else + //// cout<<"approximate poly has not enough points will skip file: "<<frameCounter<<endl; - ////#endif + ////#endif - //! step 4) we make a selection out of all counters with area sizes****************************** + //! step 4) we make a selection out of all counters with area sizes****************************** - //we calc all min rotated rectangles for all contour from candy egde detect - //we exlcude very small one and very big ones - vector<RotatedRect> minRect( contours.size() ); + //we calc all min rotated rectangles for all contour from candy egde detect + //we exlcude very small one and very big ones + vector<RotatedRect> minRect( contours.size() ); - //calc boxes around the contours - for( size_t i = 0; i < contours.size(); i++ ) - minRect[i] = minAreaRect( Mat(contours[i]) ); //may use boundingRect ?? to use fix non rotated rectangles ? + //calc boxes around the contours + for( size_t i = 0; i < contours.size(); i++ ) + minRect[i] = minAreaRect( Mat(contours[i]) ); //may use boundingRect ?? to use fix non rotated rectangles ? - vector<Point2f> recCenterPoints; - float areaMinThreshold = 150; - float areaMaxThreshold = 300000; //old max threshold was to small //TODO apply moving filter ??, an more adaptive approach + vector<Point2f> recCenterPoints; + float areaMinThreshold = 150; + float areaMaxThreshold = 300000; //old max threshold was to small //TODO apply moving filter ??, an more adaptive approach - cout << " g "<<endl; + cout << " g "<<endl; - //iterate all rectangles - for( size_t i = 0; i< minRect.size(); i++ ) - { - Point2f rect_points[4]; - //get all points of the retange - minRect[i].points( rect_points ); - - //construct contour based on rectangle points because contour != rectangle with points - vector<Point> contourRect; - for(int j=0;j<4;j++) - contourRect.push_back(rect_points[j]); - - //calc the area of the contour - double area0 = contourArea(contourRect); - //over stepp all small areas - if(area0<areaMinThreshold||area0>areaMaxThreshold) + //iterate all rectangles + for( size_t i = 0; i< minRect.size(); i++ ) { - //cout<<i<<":area0:"<<area0<<" dismissed "<<endl; - //skipe if the area is too small - continue; - } - else - { - //cout<<i<<":area0:"<<area0<<" choose "<<endl; - - //get the center of this rectangle - Point2f center = minRect[i].center; - recCenterPoints.push_back(center); + Point2f rect_points[4]; + //get all points of the retange + minRect[i].points( rect_points ); + + //construct contour based on rectangle points because contour != rectangle with points + vector<Point> contourRect; + for(int j=0;j<4;j++) + contourRect.push_back(rect_points[j]); + + //calc the area of the contour + double area0 = contourArea(contourRect); + //over stepp all small areas + if(area0<areaMinThreshold||area0>areaMaxThreshold) + { + //cout<<i<<":area0:"<<area0<<" dismissed "<<endl; + //skipe if the area is too small + continue; + } + else + { + //cout<<i<<":area0:"<<area0<<" choose "<<endl; - //we also add the this contour to a selection - contourSelection.push_back(contours[i]); + //get the center of this rectangle + Point2f center = minRect[i].center; + recCenterPoints.push_back(center); - } - }//end iterate all min rectangle + //we also add the this contour to a selection + contourSelection.push_back(contours[i]); - if(!contourSelection.empty()) - { - //! step 5) we calc the moments from the contour selection - vector<Moments> mu(contourSelection.size() ); - for( size_t i = 0; i < contourSelection.size(); i++ ) - { - mu[i] = moments( contourSelection[i], false ); - } + } + }//end iterate all min rectangle - // Get the mass centers: - massCenters = vector<Point2f>( contourSelection.size() ); - for( size_t i = 0; i < contourSelection.size(); i++ ) + if(!contourSelection.empty()) { - massCenters[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); - } + //! step 5) we calc the moments from the contour selection + vector<Moments> mu(contourSelection.size() ); + for( size_t i = 0; i < contourSelection.size(); i++ ) + { + mu[i] = moments( contourSelection[i], false ); + } - //! step 6) calc convex hull of all points of the contour selection ********* - //TODO produce center of convex hull of all polygones - //TODO double check if ne contour sharing a center point ? nearby ?? + // Get the mass centers: + massCenters = vector<Point2f>( contourSelection.size() ); + for( size_t i = 0; i < contourSelection.size(); i++ ) + { + massCenters[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); + } - //we merge all points - vector<Point> allContourPoints; - for (size_t cC = 0; cC < contourSelection.size(); ++cC) - for(size_t cP =0; cP < contourSelection[cC].size(); cP++) + //! step 6) calc convex hull of all points of the contour selection ********* + //TODO produce center of convex hull of all polygones + //TODO double check if ne contour sharing a center point ? nearby ?? + + //we merge all points + vector<Point> allContourPoints; + for (size_t cC = 0; cC < contourSelection.size(); ++cC) + for(size_t cP =0; cP < contourSelection[cC].size(); cP++) + { + Point currentContourPixel = contourSelection[cC][cP]; + allContourPoints.push_back(currentContourPixel); + } + + // calc the hull ****************** + conHull = vector<Point>(allContourPoints.size()); + convexHull( Mat(allContourPoints), conHull, false ); + // Point roiCenter; + // float roiRadius; + + //calc the min circle around + //minEnclosingCircle(conHull,roiCenter,roiRadius); + //we calc the mass center of the convex hull + + ///we calc the mass center of the convex hull + Moments muConvexHull; + if(!conHull.empty()) { - Point currentContourPixel = contourSelection[cC][cP]; - allContourPoints.push_back(currentContourPixel); + muConvexHull = moments(conHull, true ); + muConvexHullMassCenter= Point2f( muConvexHull.m10/muConvexHull.m00 , muConvexHull.m01/muConvexHull.m00 ); } - // calc the hull ****************** - conHull = vector<Point>(allContourPoints.size()); - convexHull( Mat(allContourPoints), conHull, false ); - // Point roiCenter; - // float roiRadius; + cout << " h "<<endl; - //calc the min circle around - //minEnclosingCircle(conHull,roiCenter,roiRadius); - //we calc the mass center of the convex hull - ///we calc the mass center of the convex hull - Moments muConvexHull; - if(!conHull.empty()) + }//end after selection is empty { - muConvexHull = moments(conHull, true ); - muConvexHullMassCenter= Point2f( muConvexHull.m10/muConvexHull.m00 , muConvexHull.m01/muConvexHull.m00 ); + cerr<<"error, no contour found after selection in file: "<< fileName<<endl; + myFileListAfterContourSelection.push_back(fileName); } - - cout << " h "<<endl; - - - }//end after selection is empty + }//end if contours are empty, to canny edge found nothing + else { - cerr<<"error, no contour found after selection in file: "<< fileName<<endl; - myFileListAfterContourSelection.push_back(fileName); - } - }//end if contours are empty, to canny edge found nothing - else - { - cerr<<"error, no contour found at all in file: "<< fileName<<endl; - myFileListNoContour.push_back(fileName); + cerr<<"error, no contour found at all in file: "<< fileName<<endl; + myFileListNoContour.push_back(fileName); - } + } #ifdef MC_SHOW_STEP_ANALYSE - if(!conHull.empty())//if we any elements, we process further - { - Mat imgConvexHull = Mat::zeros( img_input.size(), CV_8UC3 ); - imgConvexHull = Scalar(255,255,255); //fille the picture + if(!conHull.empty())//if we any elements, we process further + { + Mat imgConvexHull = Mat::zeros( img_input.size(), CV_8UC3 ); + imgConvexHull = Scalar(255,255,255); //fille the picture - Scalar colorB( 0,0,255,255 );//red - polylines(imgConvexHull, hullComplete, true, colorB, 1, 8); + Scalar colorB( 0,0,255,255 );//red + polylines(imgConvexHull, hullComplete, true, colorB, 1, 8); - //draw circle around - //circle( imgConvexHull, roiCenter, (int) roiRadius, colorB, 2, 8, 0 ); + //draw circle around + //circle( imgConvexHull, roiCenter, (int) roiRadius, colorB, 2, 8, 0 ); - //we draw it - cv::String outpath2= outputDir; - std::ostringstream convert2; - convert2 << outpath2 <<TToolBox::mNzero(frameCounter) <<"_convex_hull.jpg"; - cv::imwrite(convert2.str().c_str(), imgConvexHull); - } - else - cout<<"convex hull has no points will skip file: "<<i<<endl; + //we draw it + cv::String outpath2= outputDir; + std::ostringstream convert2; + convert2 << outpath2 <<TToolBox::mNzero(frameCounter) <<"_convex_hull.jpg"; + cv::imwrite(convert2.str().c_str(), imgConvexHull); + } + else + cout<<"convex hull has no points will skip file: "<<i<<endl; #endif - cout << " i "<<endl; - //cout<<"we try to write file: "<< i <<endl; - //! step 8: we write down all our results in yml file - std::string nameOutPutFileData = outputDir + TToolBox::mNzero(frameCounter) + ".yml"; + cout << " i "<<endl; + //cout<<"we try to write file: "<< i <<endl; + //! step 8: we write down all our results in yml file + std::string nameOutPutFileData = outputDir + TToolBox::mNzero(frameCounter) + ".yml"; - cout << " j "<<endl; + cout << " j "<<endl; -// //check the string -// cout <<"write output nameOutPutFileData: "<< nameOutPutFileData<<endl; -// if(nameOutPutFileData.empty()) -// cerr <<"error: empty string nameOutPutFileData"<<endl; + // //check the string + // cout <<"write output nameOutPutFileData: "<< nameOutPutFileData<<endl; + // if(nameOutPutFileData.empty()) + // cerr <<"error: empty string nameOutPutFileData"<<endl; - FileStorage fs(nameOutPutFileData.c_str(), FileStorage::WRITE); - fs << "masscenters" << massCenters; - fs << "polygonselection"<< contourSelection; - fs << "convexhull"<<conHull; - fs << "masscenterconvexhull"<<muConvexHullMassCenter; - fs.release(); + FileStorage fs(nameOutPutFileData.c_str(), FileStorage::WRITE); + fs << "masscenters" << massCenters; + fs << "polygonselection"<< contourSelection; + fs << "convexhull"<<conHull; + fs << "masscenterconvexhull"<<muConvexHullMassCenter; + fs.release(); - cout << " k "<<endl; + cout << " k "<<endl; - //! we write from time to time a dbg picture - if(frameCounter%everyPic==0) - { - Scalar colorRed( 0,0,255,255 );//red - RNG rng(4344234); - Mat imgDebugPaint2 = Mat::zeros( img_input.size(), CV_8UC3 ); - imgDebugPaint2 = Scalar(255,255,255); //fill the picture white - - //we write all polyies of the selection and the mass centers with a random color - for( size_t i = 0; i< contourSelection.size(); i++ ) + //! we write from time to time a dbg picture + if(frameCounter%everyPic==0) { - //random color - Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); - //contour - drawContours( imgDebugPaint2, contourSelection, i, color, 1, LINE_AA); - //draw the center - circle( imgDebugPaint2, massCenters[i], 4, color, -1, 8, 0 ); - } + Scalar colorRed( 0,0,255,255 );//red + RNG rng(4344234); + Mat imgDebugPaint2 = Mat::zeros( img_input.size(), CV_8UC3 ); + imgDebugPaint2 = Scalar(255,255,255); //fill the picture white - //we write the convex hull - if(!conHull.empty()) - { - //the poly - polylines(imgDebugPaint2, conHull, true, colorRed, 1, 8); - //the center - circle( imgDebugPaint2,muConvexHullMassCenter, 4, colorRed, -1, 8, 0 ); - } + //we write all polyies of the selection and the mass centers with a random color + for( size_t i = 0; i< contourSelection.size(); i++ ) + { + //random color + Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); + //contour + drawContours( imgDebugPaint2, contourSelection, i, color, 1, LINE_AA); + //draw the center + circle( imgDebugPaint2, massCenters[i], 4, color, -1, 8, 0 ); + } - //we make a copy - Mat imgOverlay2 = img_input.clone(); - //we add a overlay of our paitings - addWeighted( imgDebugPaint2, 0.7, imgOverlay2, 0.3, 0.0, imgOverlay2); - //we write the file down - std::string nameOutPutFileDBGpic = outputDir + TToolBox::mNzero(frameCounter) + std::string(".jpg"); + //we write the convex hull + if(!conHull.empty()) + { + //the poly + polylines(imgDebugPaint2, conHull, true, colorRed, 1, 8); + //the center + circle( imgDebugPaint2,muConvexHullMassCenter, 4, colorRed, -1, 8, 0 ); + } + + //we make a copy + Mat imgOverlay2 = img_input.clone(); + //we add a overlay of our paitings + addWeighted( imgDebugPaint2, 0.7, imgOverlay2, 0.3, 0.0, imgOverlay2); + //we write the file down + std::string nameOutPutFileDBGpic = outputDir + TToolBox::mNzero(frameCounter) + std::string(".jpg"); + + // //check the string + // cout <<"write dbg file: "<< nameOutPutFileDBGpic<<endl; + // if(nameOutPutFileDBGpic.empty()) + // cerr <<"error: empty string nameOutPutFileDBGpic"<<endl; + + imwrite(nameOutPutFileDBGpic.c_str(),imgOverlay2); -// //check the string -// cout <<"write dbg file: "<< nameOutPutFileDBGpic<<endl; -// if(nameOutPutFileDBGpic.empty()) -// cerr <<"error: empty string nameOutPutFileDBGpic"<<endl; + cout << " l "<<endl; + }//end if we write a dbg picture - imwrite(nameOutPutFileDBGpic.c_str(),imgOverlay2); - cout << " l "<<endl; + }//end if the loaded picture has data + else + { + cerr<<"error loading file: "<< fileName<<", will skip this file"<<endl; + myFileErrorList.push_back(fileName); } - }//end if the loaded picture has data + }//end if not corrupted picture else { - cerr<<"error loading file: "<< fileName<<", will skip this file"<<endl; - myFileErrorList.push_back(fileName); + cout<<"error file: "<< fileName<<" is corrupted, will ignore it"<<endl; + myFileListCorrupted.push_back(fileName); + } + //we calc the time which we used for a picture clock_t end = clock(); double elapsedSecs = double(end - begin) / CLOCKS_PER_SEC; diff --git a/package_bgs/ttoolbox.cpp b/package_bgs/ttoolbox.cpp index 70f3e7b..8de7870 100644 --- a/package_bgs/ttoolbox.cpp +++ b/package_bgs/ttoolbox.cpp @@ -96,3 +96,56 @@ cv::Mat TToolBox::cropImageCircle(cv::Mat image, int x, int y, int r) return res; } + +int TToolBox::checkFileCorrupted(std::string filename) +{ + int retVal = 1;//is as long Corrupted as we dont find it in another way + char command[] = "identify "; + const char *fileNameArr = filename.c_str(); + char command2[] = " > /dev/null 2>&1 ; echo $?"; + char *cmd = new char[std::strlen(command)+std::strlen(fileNameArr) + std::strlen(command2) +1]; + //we put all peaces together + std::strcpy(cmd,command); + std::strcat(cmd,fileNameArr); + std::strcat(cmd,command2); + + //std:: cout <<"cmd is: "<< cmd<< std::endl; + //execute the command + std::string result = TToolBox::execCMD((const char* ) cmd); + + //std:: cout <<"results is: "<< result<< std::endl; + + if(!result.empty()) + { + try { + retVal = std::stoi(result); + } + catch(std::invalid_argument& e) + { + std::cerr <<"wrong argument for stoi"<<std::endl; + // if no conversion could be performed + } + catch (const std::exception& e) + { + std::cerr <<"error during use stoi"<<std::endl; + } + + }//end if + return retVal; +} + + +std::string TToolBox::execCMD(const char* cmd) { + std::array<char, 128> buffer; + std::string result; + std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); + if (!pipe) throw std::runtime_error("popen() failed!"); + while (!feof(pipe.get())) { + if (fgets(buffer.data(), 128, pipe.get()) != nullptr) + result += buffer.data(); + //std::cout<<result<<std::endl; + + } + //std::cout<<"call: " <<result<<std::endl; + return result; +} diff --git a/package_bgs/ttoolbox.h b/package_bgs/ttoolbox.h index 75bea2c..ee2ca9b 100644 --- a/package_bgs/ttoolbox.h +++ b/package_bgs/ttoolbox.h @@ -10,6 +10,11 @@ #include <time.h> /* time */ #include <ctime> #include <sstream> +#include <cstdio> +#include <memory> +#include <stdexcept> +#include <string> +#include <array> //open cv #include "opencv2/core/core.hpp" @@ -36,6 +41,13 @@ public: //! canny edge detect static std::vector<std::vector<cv::Point>> applyCannyEdgeAndCalcCountours(cv::Mat imgSource, double threshholdMin, double threshholdMax, int apertureSize); + //! will test if file is not corrupted + //! @return 1 if corrupted + //! @return 0 if not + static int checkFileCorrupted(std::string filename); + + //! will try to execute the cmd on the bash + static std::string execCMD(const char* cmd); }; -- GitLab