I am trying to create a json file from c++. The used code is the following:
Mat projected = eigenfacesExtraction(fileName);
ofstream myfile;
myfile.open ("detection.json");
myfile << "{\n";
myfile << "\t \"meta\":{\n";
myfile << "\t \"duration\":\""<<duration<<"\",\n";
myfile << "\t \"fileName\":\""<<fileName<<"\"\n";
myfile << "},\n";
myfile << "\t \"num_of_people\":\""<< faces.size()<<"\",\n";
myfile << "\t \"faces\":[\n";
myfile << "\t \"projected\":[" << projected.size <<"] ,\n";
for(int i=0; i<faces.size(); i++){
if(!faces.empty()){
if(i!= faces.size()-1){
myfile << "\t \"coord\":\" \t[" << faces[i].x << ", "<<faces[i].y<< ", "<< faces[i].width << ", "<<faces[i].height<<"],\n";
myfile << "\t \"descr\":\" \t[" << projected.at<double>(0,1) << ", "<<projected.at<double>(0,2)<< ", "<< projected.at<double>(0,3) << ", "<<projected.at<double>(0,4)<<"],\n";
}
else myfile << "\t \"coord\":\" \t[" << faces[i].x << ", "<<faces[i].y<< ", "<< faces[i].width << ", "<<faces[i].height<<"]\n";
myfile << "\t \"descr\":\" \t[" << projected.at<double>(0,1) << ", "<<projected.at<double>(0,2)<< ", "<< projected.at<double>(0,3)<< ", "<<projected.at<double>(0,4)<<"],\n";
}
}
myfile << "]\n";
myfile << "}";
myfile.close();
cout<< "Detection process exits...."<<endl;
//imwrite( "/opencvAssets/results/"+fileName, image );
imwrite("outputCapture.jpeg", image);
//waitKey(0);
//imshow("cropped image", croppedFaceImage);
My program works fine, json file is stored properly also cout prints the message, but I am receiving the following message "Segmentation fault (core dumped)". When I comment out iwrite command neither json nor cout work and I am receiving again the same message. What is the matter here?? The eigenFacesExtraction function (which may cause the problems) is the following:
Mat Detection::eigenfacesExtraction(string fileName){
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->load("eigenfaces.yml"); // Load eigenfaces parameters
Mat eigenvalues = model->getMat("eigenvalues"); // Eigen values of PCA
Mat convMat = model->getMat("eigenvectors"); //Convariance matrix which contains eigenvectors
Mat mean = model->getMat("mean"); // Mean value
convMat.convertTo(convMat, CV_32F);
mean.convertTo(mean, CV_32F);
string path = fileName;
Mat sample ,pca_ed_sample, resizedSample;
sample = imread(path, CV_LOAD_IMAGE_GRAYSCALE);
sample.convertTo(sample, CV_32F);
resize(sample,resizedSample,Size(60,60),0,0,INTER_LINEAR);
Mat nu = resizedSample.reshape(1,3600).t();
pca_ed_sample = (nu-mean)*(convMat);
return pca_ed_sample;
}