Fixed issue 2 : save/load from directory, ask user to save objects on quit
Added new action "Remove all objects" git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@78 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
parent
cd6b7e8680
commit
aa1d2f67e9
@ -28,12 +28,14 @@
|
|||||||
#include <QtGui/QSpinBox>
|
#include <QtGui/QSpinBox>
|
||||||
#include <QtGui/QStatusBar>
|
#include <QtGui/QStatusBar>
|
||||||
#include <QtGui/QProgressDialog>
|
#include <QtGui/QProgressDialog>
|
||||||
|
#include <QtGui/QCloseEvent>
|
||||||
|
|
||||||
// Camera ownership transferred
|
// Camera ownership transferred
|
||||||
MainWindow::MainWindow(Camera * camera, QWidget * parent) :
|
MainWindow::MainWindow(Camera * camera, QWidget * parent) :
|
||||||
QMainWindow(parent),
|
QMainWindow(parent),
|
||||||
camera_(camera),
|
camera_(camera),
|
||||||
lowestRefreshRate_(99)
|
lowestRefreshRate_(99),
|
||||||
|
objectsModified_(false)
|
||||||
{
|
{
|
||||||
ui_ = new Ui_mainWindow();
|
ui_ = new Ui_mainWindow();
|
||||||
ui_->setupUi(this);
|
ui_->setupUi(this);
|
||||||
@ -88,6 +90,7 @@ MainWindow::MainWindow(Camera * camera, QWidget * parent) :
|
|||||||
connect(ui_->actionSetup_camera_from_video_file_2, SIGNAL(triggered()), this, SLOT(setupCameraFromVideoFile()));
|
connect(ui_->actionSetup_camera_from_video_file_2, SIGNAL(triggered()), this, SLOT(setupCameraFromVideoFile()));
|
||||||
connect(ui_->actionAbout, SIGNAL(triggered()), aboutDialog_ , SLOT(exec()));
|
connect(ui_->actionAbout, SIGNAL(triggered()), aboutDialog_ , SLOT(exec()));
|
||||||
connect(ui_->actionRestore_all_default_settings, SIGNAL(triggered()), ui_->toolBox, SLOT(resetAllPages()));
|
connect(ui_->actionRestore_all_default_settings, SIGNAL(triggered()), ui_->toolBox, SLOT(resetAllPages()));
|
||||||
|
connect(ui_->actionRemove_all_objects, SIGNAL(triggered()), this, SLOT(removeAllObjects()));
|
||||||
|
|
||||||
ui_->actionSetup_camera_from_video_file->setCheckable(true);
|
ui_->actionSetup_camera_from_video_file->setCheckable(true);
|
||||||
ui_->actionSetup_camera_from_video_file_2->setCheckable(true);
|
ui_->actionSetup_camera_from_video_file_2->setCheckable(true);
|
||||||
@ -112,9 +115,34 @@ MainWindow::~MainWindow()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::closeEvent(QCloseEvent * event)
|
void MainWindow::closeEvent(QCloseEvent * event)
|
||||||
|
{
|
||||||
|
bool quit = true;
|
||||||
|
this->stopProcessing();
|
||||||
|
if(objectsModified_ && this->isVisible() && objects_.size())
|
||||||
|
{
|
||||||
|
int ret = QMessageBox::question(this, tr("Save new objects"), tr("Do you want to save added objects?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
|
||||||
|
switch(ret)
|
||||||
|
{
|
||||||
|
case QMessageBox::Yes:
|
||||||
|
quit = this->saveObjects();
|
||||||
|
break;
|
||||||
|
case QMessageBox::Cancel:
|
||||||
|
quit = false;
|
||||||
|
break;
|
||||||
|
case QMessageBox::No:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(quit)
|
||||||
{
|
{
|
||||||
Settings::saveSettings(Settings::iniDefaultPath(), this->saveGeometry());
|
Settings::saveSettings(Settings::iniDefaultPath(), this->saveGeometry());
|
||||||
QMainWindow::closeEvent(event);
|
event->accept();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
event->ignore();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ParametersToolBox * MainWindow::parametersToolBox() const
|
ParametersToolBox * MainWindow::parametersToolBox() const
|
||||||
@ -122,72 +150,54 @@ ParametersToolBox * MainWindow::parametersToolBox() const
|
|||||||
return ui_->toolBox;
|
return ui_->toolBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MainWindow::loadObjects(const QString & fileName)
|
void MainWindow::loadObjects(const QString & dirPath)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QDir dir(dirPath);
|
||||||
if(file.open(QIODevice::ReadOnly))
|
if(dir.exists())
|
||||||
{
|
{
|
||||||
QDataStream in(&file);
|
QStringList filters;
|
||||||
while(!in.atEnd())
|
filters << "*.png" << "*.jpg" << "*.bmp" << "*.tiff";
|
||||||
|
QFileInfoList list = dir.entryInfoList(filters, QDir::Files, QDir::Name);
|
||||||
|
for(int i=0; i<list.size(); ++i)
|
||||||
{
|
{
|
||||||
ObjWidget * obj = new ObjWidget();
|
this->addObjectFromFile(list.at(i).filePath());
|
||||||
obj->load(in);
|
}
|
||||||
bool alreadyLoaded = false;
|
if(list.size())
|
||||||
for(int i=0; i<objects_.size(); ++i)
|
|
||||||
{
|
{
|
||||||
if(objects_.at(i)->id() == obj->id())
|
this->updateObjects();
|
||||||
{
|
|
||||||
alreadyLoaded = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!alreadyLoaded)
|
|
||||||
{
|
|
||||||
objects_.append(obj);
|
|
||||||
showObject(obj);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
delete obj;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::saveObjects(const QString & fileName)
|
void MainWindow::saveObjects(const QString & dirPath)
|
||||||
|
{
|
||||||
|
QDir dir(dirPath);
|
||||||
|
if(dir.exists())
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
|
||||||
file.open(QIODevice::WriteOnly);
|
|
||||||
QDataStream out(&file);
|
|
||||||
for(int i=0; i<objects_.size(); ++i)
|
for(int i=0; i<objects_.size(); ++i)
|
||||||
{
|
{
|
||||||
objects_.at(i)->save(out);
|
objects_.at(i)->image().save(QString("%1/%2.bmp").arg(dirPath).arg(objects_.at(i)->id()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::loadObjects()
|
void MainWindow::loadObjects()
|
||||||
{
|
{
|
||||||
QString fileName = QFileDialog::getOpenFileName(this, tr("Load objects..."), Settings::workingDirectory(), "*.obj");
|
QString dirPath = QFileDialog::getExistingDirectory(this, tr("Load objects..."), Settings::workingDirectory());
|
||||||
if(!fileName.isEmpty())
|
if(!dirPath.isEmpty())
|
||||||
{
|
{
|
||||||
loadObjects(fileName);
|
loadObjects(dirPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void MainWindow::saveObjects()
|
bool MainWindow::saveObjects()
|
||||||
{
|
{
|
||||||
QString fileName = QFileDialog::getSaveFileName(this, tr("Save objects..."), (Settings::workingDirectory() + "/") +Settings::currentDetectorType()+Settings::currentDescriptorType()+QString("%1.obj").arg(objects_.size()), "*.obj");
|
QString dirPath = QFileDialog::getExistingDirectory(this, tr("Save objects..."), Settings::workingDirectory());
|
||||||
if(!fileName.isEmpty())
|
if(!dirPath.isEmpty())
|
||||||
{
|
{
|
||||||
if(!fileName.endsWith(".obj"))
|
saveObjects(dirPath);
|
||||||
{
|
return true;
|
||||||
fileName.append(".obj");//default
|
|
||||||
}
|
|
||||||
saveObjects(fileName);
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::removeObject(ObjWidget * object)
|
void MainWindow::removeObject(ObjWidget * object)
|
||||||
@ -200,6 +210,15 @@ void MainWindow::removeObject(ObjWidget * object)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::removeAllObjects()
|
||||||
|
{
|
||||||
|
QList<ObjWidget*> obj = objects_;
|
||||||
|
for(int i=0; i<obj.size(); ++i)
|
||||||
|
{
|
||||||
|
removeObject(obj[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::addObject()
|
void MainWindow::addObject()
|
||||||
{
|
{
|
||||||
disconnect(camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(update(const cv::Mat &)));
|
disconnect(camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(update(const cv::Mat &)));
|
||||||
@ -207,6 +226,8 @@ void MainWindow::addObject()
|
|||||||
if(dialog.exec() == QDialog::Accepted)
|
if(dialog.exec() == QDialog::Accepted)
|
||||||
{
|
{
|
||||||
showObject(objects_.last());
|
showObject(objects_.last());
|
||||||
|
updateData();
|
||||||
|
objectsModified_ = true;
|
||||||
}
|
}
|
||||||
this->startProcessing();
|
this->startProcessing();
|
||||||
}
|
}
|
||||||
@ -218,16 +239,58 @@ void MainWindow::addObjectsFromFiles()
|
|||||||
{
|
{
|
||||||
for(int i=0; i<fileNames.size(); ++i)
|
for(int i=0; i<fileNames.size(); ++i)
|
||||||
{
|
{
|
||||||
IplImage * img = cvLoadImage(fileNames[i].toStdString().c_str(), CV_LOAD_IMAGE_GRAYSCALE);
|
this->addObjectFromFile(fileNames.at(i));
|
||||||
|
}
|
||||||
|
objectsModified_ = true;
|
||||||
|
updateObjects();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::addObjectFromFile(const QString & filePath)
|
||||||
|
{
|
||||||
|
if(!filePath.isNull())
|
||||||
|
{
|
||||||
|
IplImage * img = cvLoadImage(filePath.toStdString().c_str(), CV_LOAD_IMAGE_GRAYSCALE);
|
||||||
if(img)
|
if(img)
|
||||||
{
|
{
|
||||||
objects_.append(new ObjWidget(0, std::vector<cv::KeyPoint>(), cv::Mat(), img, "", ""));
|
int id = 0;
|
||||||
|
QFileInfo file(filePath);
|
||||||
|
QStringList list = file.fileName().split('.');
|
||||||
|
if(list.size())
|
||||||
|
{
|
||||||
|
printf("asdfddsa %s\n", list.front().toStdString().c_str());
|
||||||
|
bool ok = false;
|
||||||
|
id = list.front().toInt(&ok);
|
||||||
|
if(ok)
|
||||||
|
{
|
||||||
|
printf("id=%d\n", id);
|
||||||
|
for(int i=0; i<objects_.size(); ++i)
|
||||||
|
{
|
||||||
|
if(objects_.at(i)->id() == id)
|
||||||
|
{
|
||||||
|
if(this->isVisible())
|
||||||
|
{
|
||||||
|
QMessageBox::warning(this, tr("Warning"), tr("Object %1 already added, a new ID will be generated.").arg(id));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("WARNING: Object %d already added, a new ID will be generated.", id);
|
||||||
|
}
|
||||||
|
id = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
id = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
objects_.append(new ObjWidget(id, std::vector<cv::KeyPoint>(), cv::Mat(), img, "", ""));
|
||||||
this->showObject(objects_.last());
|
this->showObject(objects_.last());
|
||||||
cvReleaseImage(&img);
|
cvReleaseImage(&img);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateObjects();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::loadSceneFromFile()
|
void MainWindow::loadSceneFromFile()
|
||||||
@ -303,8 +366,6 @@ void MainWindow::showObject(ObjWidget * obj)
|
|||||||
connect(obj, SIGNAL(destroyed(QObject *)), detectorDescriptorType, SLOT(deleteLater()));
|
connect(obj, SIGNAL(destroyed(QObject *)), detectorDescriptorType, SLOT(deleteLater()));
|
||||||
connect(obj, SIGNAL(destroyed(QObject *)), vLayout, SLOT(deleteLater()));
|
connect(obj, SIGNAL(destroyed(QObject *)), vLayout, SLOT(deleteLater()));
|
||||||
ui_->verticalLayout_objects->insertLayout(ui_->verticalLayout_objects->count()-1, vLayout);
|
ui_->verticalLayout_objects->insertLayout(ui_->verticalLayout_objects->count()-1, vLayout);
|
||||||
|
|
||||||
this->updateData();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -29,8 +29,8 @@ public:
|
|||||||
MainWindow(Camera * camera = 0, QWidget * parent = 0);
|
MainWindow(Camera * camera = 0, QWidget * parent = 0);
|
||||||
virtual ~MainWindow();
|
virtual ~MainWindow();
|
||||||
|
|
||||||
bool loadObjects(const QString & fileName);
|
void loadObjects(const QString & dirPath);
|
||||||
void saveObjects(const QString & fileName);
|
void saveObjects(const QString & dirPath);
|
||||||
|
|
||||||
ParametersToolBox * parametersToolBox() const;
|
ParametersToolBox * parametersToolBox() const;
|
||||||
|
|
||||||
@ -44,12 +44,13 @@ public slots:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void loadObjects();
|
void loadObjects();
|
||||||
void saveObjects();
|
bool saveObjects();
|
||||||
void addObject();
|
void addObject();
|
||||||
void addObjectsFromFiles();
|
void addObjectsFromFiles();
|
||||||
void loadSceneFromFile();
|
void loadSceneFromFile();
|
||||||
void setupCameraFromVideoFile();
|
void setupCameraFromVideoFile();
|
||||||
void removeObject(ObjWidget * object);
|
void removeObject(ObjWidget * object);
|
||||||
|
void removeAllObjects();
|
||||||
void update(const cv::Mat & image);
|
void update(const cv::Mat & image);
|
||||||
void updateObjects();
|
void updateObjects();
|
||||||
void notifyParametersChanged();
|
void notifyParametersChanged();
|
||||||
@ -58,6 +59,7 @@ signals:
|
|||||||
void objectsFound(const QMap<int, QPair<QRect, QTransform> > &);
|
void objectsFound(const QMap<int, QPair<QRect, QTransform> > &);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void addObjectFromFile(const QString & filePath);
|
||||||
void showObject(ObjWidget * obj);
|
void showObject(ObjWidget * obj);
|
||||||
void updateData();
|
void updateData();
|
||||||
|
|
||||||
@ -71,7 +73,7 @@ private:
|
|||||||
QTime updateRate_;
|
QTime updateRate_;
|
||||||
QTime refreshStartTime_;
|
QTime refreshStartTime_;
|
||||||
int lowestRefreshRate_;
|
int lowestRefreshRate_;
|
||||||
|
bool objectsModified_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* MainWindow_H_ */
|
#endif /* MainWindow_H_ */
|
||||||
|
|||||||
@ -160,6 +160,8 @@
|
|||||||
<addaction name="actionSetup_camera_from_video_file_2"/>
|
<addaction name="actionSetup_camera_from_video_file_2"/>
|
||||||
<addaction name="separator"/>
|
<addaction name="separator"/>
|
||||||
<addaction name="actionRestore_all_default_settings"/>
|
<addaction name="actionRestore_all_default_settings"/>
|
||||||
|
<addaction name="separator"/>
|
||||||
|
<addaction name="actionRemove_all_objects"/>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QMenu" name="menuView">
|
<widget class="QMenu" name="menuView">
|
||||||
<property name="title">
|
<property name="title">
|
||||||
@ -387,7 +389,7 @@
|
|||||||
<property name="minimumSize">
|
<property name="minimumSize">
|
||||||
<size>
|
<size>
|
||||||
<width>300</width>
|
<width>300</width>
|
||||||
<height>162</height>
|
<height>196</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
@ -514,6 +516,11 @@
|
|||||||
<string>Setup camera from video file...</string>
|
<string>Setup camera from video file...</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
|
<action name="actionRemove_all_objects">
|
||||||
|
<property name="text">
|
||||||
|
<string>Remove all objects</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
</widget>
|
</widget>
|
||||||
<customwidgets>
|
<customwidgets>
|
||||||
<customwidget>
|
<customwidget>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user