I am working on a project based on facerecognition using Qt and Opencv in Windows 7. I have come across a really dead end now…I need to train a face recognizer using some images…but whenever I comment out the train statement, the program runs fine but not witholding my purpose…
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();
}
When I run the project and click a button that initiate the above function the process crashes saying…
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
i modified the code as follows taking advices…and got in to another problem…
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));
}
i removed the pointers used within the for loop and stil the very same problem persists…
I also found that qt creator in its issues tab reported an issue:
:-1: warning: auto-importing has been activated without --enable-auto-import specified on the command line.
When your code crashes, use a debugger to find out where it crashes. If you don’t understand the reason, add the backtrace to the question.
I assume your crash is in the filename handling when looping over the files.
You’re opening multiple cans of worms by using char* instead of the std::string or QString and not doing it properly.
For once, this is highly unsafe and also insecure (buffer overflow):
If the file name is longer than 9, the behavior is undefined (crash, most likely).
You’re also leaking memory, as this allocation is never freed: char* path = new char [fname.size()+1];
So I strongly suggest to use QString instead of fiddling with char*. If you need to pass a char* to OpenCV, you can do it like this:
imread takes a std::string, so: