1

私はWindows7でQtとOpencvを使用した顔認識に基づくプロジェクトに取り組んでいます...私は今本当に行き止まりに遭遇しました...私はいくつかの画像を使用して顔認識を訓練する必要があります...しかし私が列車の声明をコメントアウトするときはいつでも、プログラムは正常に実行されますが、私の目的を放棄することはありません...

void Dialog::on_btnAdd_2_clicked()
{
    tmrTimer->stop();

    dir=QDir(directory);
    QFileInfoList files;
    QFileInfo file;
    QString filen;


    dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    files=dir.entryInfoList();
    int nofiles;
    nofiles=files.size();
    for(int i=0;i<nofiles;i++)
    {
        file=files.at(i);
        filen=file.absoluteFilePath();


        std::string fname = filen.toStdString();

        filen=file.baseName();
        std::string filename=filen.toStdString();

        char* path = new char [fname.size()+1];
        char name[10];

        strcpy(name,filename.c_str());
        strcpy( path, fname.c_str() );

        name[1]='\0';

        ui->boxConsole->appendPlainText(path);
        ui->boxConsole->appendPlainText(name);

        cv::Mat temp=cv::imread(path,-1);
        newImages.push_back(temp);
        newLabels.push_back(atoi(name));
    }


    ui->boxConsole->appendPlainText("Training Started....!!");

    cv::Ptr<cv::FaceRecognizer> model = cv::createEigenFaceRecognizer();

    model->train(newImages,newLabels);   ///training statement

    //strcat(home,"\\data.xml");


    //model->save(home);


    ui->boxConsole->appendPlainText("Training Completed!!");
    tmrTimer->start();
}

プロジェクトを実行し、上記の機能を開始するボタンをクリックすると、プロセスがクラッシュして...

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: QtTracker3.exe
  Application Version:  0.0.0.0
  Application Timestamp:    5101dde0
  Fault Module Name:    libopencv_core242.dll
  Fault Module Version: 0.0.0.0
  Fault Module Timestamp:   50da6896
  Exception Code:   c0000005
  Exception Offset: 000a38f0
  OS Version:   6.1.7600.2.3.0.256.1
  Locale ID:    1033
  Additional Information 1: 0a9e
  Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
  Additional Information 3: 0a9e
  Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt

私はアドバイスを受けて次のようにコードを変更しました...そして別の問題に巻き込まれました...

void Dialog::on_btnAdd_2_clicked()
{
    tmrTimer->stop();

    dir=QDir(directory);
    QFileInfoList files;
    QFileInfo file;
    QString filen;

    char name[10];
    int i,j;
    std::string str;


    dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    files=dir.entryInfoList();
    int nofiles;
    nofiles=files.size();

    for(i=0;i<nofiles;i++)
        {
            file=files.at(i);
            filen=file.baseName();
            std::string filename=filen.toStdString();
            strcpy(name,filename.c_str());

            for(j=0;name[j]!='-';j++);
            name[j]='\0';

            filen=file.absoluteFilePath();
            str=filen.toStdString();

            cv::Mat offimage=cv::imread(str.c_str(),-1);

            cv::namedWindow("face",WINDOW_AUTOSIZE);     //show retrieved image in a window
            cv::imshow("face",offimage);
            cvvWaitKey(1000);

            newImages.push_back(offimage);
            newLabels.push_back(atoi(name));


        }

forループ内で使用されているポインターを削除しましたが、まったく同じ問題が解決しません...

また、[問題]タブのqt作成者が問題を報告していることもわかりました。

:-1: warning: auto-importing has been activated without --enable-auto-import specified on the command line.
4

1 に答える 1

0

コードがクラッシュした場合は、デバッガーを使用してクラッシュした場所を見つけます。理由がわからない場合は、質問にバックトレースを追加してください。

私はあなたのクラッシュがファイルをループするときのファイル名処理にあると思います。std ::stringまたはQStringの代わりにchar*を使用してワームの複数の缶を開いており、正しく実行していません。

かつては、これは非常に安全ではなく、安全でもありません(バッファオーバーフロー):

    char name[10];
    ...
    strcpy(name,filename.c_str());

ファイル名が9より長い場合、動作は未定義です(クラッシュの可能性が高い)。

この割り当ては決して解放されないため、メモリリークも発生しています。char* path = new char [fname.size()+ 1];

したがって、char*をいじる代わりにQStringを使用することを強くお勧めします。char *をOpenCVに渡す必要がある場合は、次のように行うことができます。

const QByteArray ba = filename.toLatin1(); //or toUtf8(), toLocal8Bit(), depending on the encoding you need
... functionTakingChar( ba.constData(), -1 ); // or .data(), if imread() takes a non-const char

imreadはstd::stringを取るので、:

... imread( filename.toStdString(), -1 );
于 2013-01-25T06:47:09.437 に答える