-1

出力として7つのxmlファイルを生成する7つのテキストファイルをトレーニングしようとしています。したがって、7回機能する「forループ」を試しました。

OpenCV を使用した顔認識 Online Face Recognitionについては、次のリンクを参照しました。

コードを実行するとエラーが発生します: 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;
    }
}
4

1 に答える 1