我正在尝试训练7个文本文件,这些文件将生成7个xml文件作为输出。因此我尝试了一个“for循环”,它将运行7次。
我已经提到以下链接进行人脸识别 Online Face Recognition using OpenCV
运行代码时遇到错误: Detect.exe中0x00cd2e34处的未处理异常:0xC0000005:访问冲突读取位置0x00000000。
感谢您在这方面的支持。谢谢!
与错误和for循环相关的代码:
void learn() {
int offset;
for(int i=0; i<=7; i++){
if (i==1){
//first text file
// load training data from Diamond.txt
nTrainFaces = loadFaceImgArray("Diamond.txt");
if( nTrainFaces <2 )
{
fprintf(stderr,
"Need 2 or more training faces\n"
"Input file contains only %d\n", nTrainFaces);
return;
}
}
if (i==2){
//the next text file
// load training data from Heart.txt
nTrainFaces = loadFaceImgArray("Heart.txt");
if( nTrainFaces <2 )
{
fprintf(stderr,
"Need 2 or more training faces\n"
"Input file contains only %d\n", nTrainFaces);
return;
}
}
if (i==3){
//the next text file
// load training data from Square.txt
nTrainFaces = loadFaceImgArray("Square.txt");
if( nTrainFaces <2 )
{
fprintf(stderr,
"Need 2 or more training faces\n"
"Input file contains only %d\n", nTrainFaces);
return;
}
}
if (i==4){
//the next text file
// load training data from Oblong.txt
nTrainFaces = loadFaceImgArray("Oblong.txt");
if( nTrainFaces <2 )
{
fprintf(stderr,
"Need 2 or more training faces\n"
"Input file contains only %d\n", nTrainFaces);
return;
}
}
if (i==5){
//the next text file
// load training data from Oval.txt
nTrainFaces = loadFaceImgArray("Oval.txt");
if( nTrainFaces <2 )
{
fprintf(stderr,
"Need 2 or more training faces\n"
"Input file contains only %d\n", nTrainFaces);
return;
}
}
if (i==6){
//the next text file
// load training data from Round.txt
nTrainFaces = loadFaceImgArray("Round.txt");
if( nTrainFaces <2 )
{
fprintf(stderr,
"Need 2 or more training faces\n"
"Input file contains only %d\n", nTrainFaces);
return;
}
}
if (i==7){
//the next text file
// load training data from Triangle.txt
nTrainFaces = loadFaceImgArray("Triangle.txt");
if( nTrainFaces <2 )
{
fprintf(stderr,
"Need 2 or more training faces\n"
"Input file contains only %d\n", nTrainFaces);
return;
}
}
// do PCA on the training faces
doPCA();
// project the training images onto the PCA subspace
projectedTrainFaceMat = cvCreateMat( nTrainFaces, nEigens, CV_32FC1 );
offset = projectedTrainFaceMat->step / sizeof(float);
for(int i=0; i<nTrainFaces; i++)
{
//int offset = i * nEigens;
cvEigenDecomposite(
faceImgArr[i],
nEigens,
eigenVectArr,
0, 0,
pAvgTrainImg,
//projectedTrainFaceMat->data.fl + i*nEigens);
projectedTrainFaceMat->data.fl + i*offset);
}
// store the recognition data as an xml file
storeTrainingData();
}
}
void doPCA() {
int i;
faceImgArr = 0;
CvTermCriteria calcLimit;
CvSize faceImgSize;
// set the number of eigenvalues to use
nEigens = nTrainFaces-1;
// allocate the eigenvector images
faceImgSize.width = faceImgArr[0]->width; // I GET THE ERROR HERE
faceImgSize.height = faceImgArr[0]->height;
eigenVectArr = (IplImage**)cvAlloc(sizeof(IplImage*) * nEigens);
for(i=0; i<nEigens; i++)
eigenVectArr[i] = cvCreateImage(faceImgSize, IPL_DEPTH_32F, 1);
// allocate the eigenvalue array
eigenValMat = cvCreateMat( 1, nEigens, CV_32FC1 );
// allocate the averaged image
pAvgTrainImg = cvCreateImage(faceImgSize, IPL_DEPTH_32F, 1);
// set the PCA termination criterion
calcLimit = cvTermCriteria( CV_TERMCRIT_ITER, nEigens, 1);
// compute average image, eigenvalues, and eigenvectors
cvCalcEigenObjects(
nTrainFaces,
(void*)faceImgArr,
(void*)eigenVectArr,
CV_EIGOBJ_NO_CALLBACK,
0,
0,
&calcLimit,
pAvgTrainImg,
eigenValMat->data.fl);
cvNormalize(eigenValMat, eigenValMat, 1, 0, CV_L1, 0);
}
void storeTrainingData() {
for(int i=0;i<=7;i++){
CvFileStorage * fileStorage;
if (i==1){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Diamond.xml", 0, CV_STORAGE_WRITE );
}
if (i==2){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Heart.xml", 0, CV_STORAGE_WRITE );
}
if (i==3){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Square.xml", 0, CV_STORAGE_WRITE );
}
if (i==4){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Oblong.xml", 0, CV_STORAGE_WRITE );
}
if (i==5){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Oval.xml", 0, CV_STORAGE_WRITE );
}
if (i==6){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Round.xml", 0, CV_STORAGE_WRITE );
}
if (i==7){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Triangle.xml", 0, CV_STORAGE_WRITE );
}
// store all the data
cvWriteInt( fileStorage, "nEigens", nEigens );
cvWriteInt( fileStorage, "nTrainFaces", nTrainFaces );
cvWrite(fileStorage, "trainPersonNumMat", personNumTruthMat, cvAttrList(0,0));
cvWrite(fileStorage, "eigenValMat", eigenValMat, cvAttrList(0,0));
cvWrite(fileStorage, "projectedTrainFaceMat", projectedTrainFaceMat, cvAttrList(0,0));
cvWrite(fileStorage, "avgTrainImg", pAvgTrainImg, cvAttrList(0,0));
for(int i=0; i<nEigens; i++)
{
char varname[200];
sprintf( varname, "eigenVect_%d", i );
cvWrite(fileStorage, varname, eigenVectArr[i], cvAttrList(0,0));
}
// release the file-storage interface
cvReleaseFileStorage( &fileStorage );
}
}
int loadTrainingData(CvMat ** pTrainPersonNumMat) {
CvFileStorage * fileStorage;
for(int i=0;i<=7;i++){
if (i==1){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Diamond.xml", 0, CV_STORAGE_READ );
if( !fileStorage )
{
fprintf(stderr, "Can't open Diamond.xml\n");
return 0;
}
}
if (i==2){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Heart.xml", 0, CV_STORAGE_READ );
if( !fileStorage )
{
fprintf(stderr, "Can't open Diamond.xml\n");
return 0;
}
}
if (i==3){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Square.xml", 0, CV_STORAGE_READ );
if( !fileStorage )
{
fprintf(stderr, "Can't open Diamond.xml\n");
return 0;
}
}
if (i==4){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Oblong.xml", 0, CV_STORAGE_READ );
if( !fileStorage )
{
fprintf(stderr, "Can't open Diamond.xml\n");
return 0;
}
}
if (i==5){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Oval.xml", 0, CV_STORAGE_READ );
if( !fileStorage )
{
fprintf(stderr, "Can't open Diamond.xml\n");
return 0;
}
}
if (i==6){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Square.xml", 0, CV_STORAGE_READ );
if( !fileStorage )
{
fprintf(stderr, "Can't open Diamond.xml\n");
return 0;
}
}
if (i==7){
// create a file-storage interface
fileStorage = cvOpenFileStorage( "Triangle.xml", 0, CV_STORAGE_READ );
if( !fileStorage )
{
fprintf(stderr, "Can't open Diamond.xml\n");
return 0;
}
}
nEigens = cvReadIntByName(fileStorage, 0, "nEigens", 0);
nTrainFaces = cvReadIntByName(fileStorage, 0, "nTrainFaces", 0);
*pTrainPersonNumMat = (CvMat *)cvReadByName(fileStorage, 0, "trainPersonNumMat", 0);
eigenValMat = (CvMat *)cvReadByName(fileStorage, 0, "eigenValMat", 0);
projectedTrainFaceMat = (CvMat *)cvReadByName(fileStorage, 0, "projectedTrainFaceMat", 0);
pAvgTrainImg = (IplImage *)cvReadByName(fileStorage, 0, "avgTrainImg", 0);
eigenVectArr = (IplImage **)cvAlloc(nTrainFaces*sizeof(IplImage *));
for(int i=0; i<nEigens; i++)
{
char varname[200];
sprintf( varname, "eigenVect_%d", i );
eigenVectArr[i] = (IplImage *)cvReadByName(fileStorage, 0, varname, 0);
}
// release the file-storage interface
cvReleaseFileStorage( &fileStorage );
return 1;
}
}
答案 0 :(得分:1)
storeTrainingData中的for循环会导致崩溃,因为当i == 0时,不会打开任何文件,而是在空句柄上调用cvWriteInt。
这应该解决它:
for(int i=1; i<=7; i++)
虽然在考虑这个问题时,它仍然效率低下。
我认为你需要研究从0开始而不是1,索引事物等等。
这里有太多的代码重复。