2011-12-02 18:34:08 +00:00
/*
2014-08-06 13:43:29 +00:00
Copyright ( c ) 2011 - 2014 , Mathieu Labbe - IntRoLab - Universite de Sherbrooke
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
* Redistributions of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
* Redistributions in binary form must reproduce the above copyright
notice , this list of conditions and the following disclaimer in the
documentation and / or other materials provided with the distribution .
* Neither the name of the Universite de Sherbrooke nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND
ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2014-08-11 15:49:53 +00:00
DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
2014-08-06 13:43:29 +00:00
DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2011-10-25 15:48:19 +00:00
2014-07-31 20:11:46 +00:00
# include "find_object/MainWindow.h"
# include "find_object/Camera.h"
# include "find_object/Settings.h"
# include "find_object/TcpServer.h"
# include "find_object/FindObject.h"
# include "find_object/utilite/ULogger.h"
# include "find_object/ObjWidget.h"
# include "find_object/QtOpenCV.h"
2011-10-25 15:48:19 +00:00
# include "AddObjectDialog.h"
# include "ui_mainWindow.h"
# include "KeypointItem.h"
2014-05-13 17:47:59 +00:00
# include "RectItem.h"
2011-10-25 15:48:19 +00:00
# include "ParametersToolBox.h"
2011-11-24 19:11:30 +00:00
# include "AboutDialog.h"
2012-08-28 13:44:57 +00:00
# include "rtabmap/PdfPlot.h"
2014-06-19 21:52:04 +00:00
# include "Vocabulary.h"
2014-07-31 19:02:31 +00:00
# include "ObjSignature.h"
2011-10-25 15:48:19 +00:00
# include <iostream>
# include <stdio.h>
# include "opencv2/calib3d/calib3d.hpp"
2014-04-10 18:07:34 +00:00
# include "opencv2/imgproc/imgproc.hpp"
2014-06-17 21:04:26 +00:00
# include "opencv2/gpu/gpu.hpp"
2011-10-25 15:48:19 +00:00
# include <QtCore/QTextStream>
# include <QtCore/QFile>
2012-08-28 13:44:57 +00:00
# include <QtCore/QBuffer>
2014-03-21 22:26:06 +00:00
# include <QtCore/QThread>
2014-08-06 01:17:17 +00:00
# include <QtCore/QLineF>
2011-10-25 15:48:19 +00:00
# include <QtGui/QFileDialog>
# include <QtGui/QMessageBox>
# include <QtGui/QGraphicsScene>
2011-11-09 15:07:19 +00:00
# include <QtGui/QGraphicsRectItem>
2011-11-23 16:44:14 +00:00
# include <QtGui/QSpinBox>
2011-11-26 12:45:35 +00:00
# include <QtGui/QStatusBar>
2012-01-04 17:30:36 +00:00
# include <QtGui/QProgressDialog>
2012-02-04 00:11:11 +00:00
# include <QtGui/QCloseEvent>
2012-08-28 13:44:57 +00:00
# include <QtGui/QCheckBox>
2012-08-29 19:04:14 +00:00
# include <QtGui/QScrollBar>
2014-05-21 21:31:42 +00:00
# include <QtGui/QInputDialog>
2011-10-25 15:48:19 +00:00
2012-08-28 20:06:43 +00:00
# include "utilite/UDirectory.h"
2014-08-06 13:43:29 +00:00
namespace find_object {
2011-11-23 16:44:14 +00:00
// Camera ownership transferred
2014-07-31 19:02:31 +00:00
MainWindow : : MainWindow ( FindObject * findObject , Camera * camera , QWidget * parent ) :
2011-10-25 15:48:19 +00:00
QMainWindow ( parent ) ,
2011-11-23 19:12:41 +00:00
camera_ ( camera ) ,
2014-07-31 19:02:31 +00:00
findObject_ ( findObject ) ,
2012-08-28 13:44:57 +00:00
likelihoodCurve_ ( 0 ) ,
2014-05-11 23:57:08 +00:00
inliersCurve_ ( 0 ) ,
2012-02-04 00:11:11 +00:00
lowestRefreshRate_ ( 99 ) ,
2014-05-05 23:08:01 +00:00
objectsModified_ ( false ) ,
2014-07-31 19:02:31 +00:00
tcpServer_ ( 0 )
2011-10-25 15:48:19 +00:00
{
2014-07-31 19:02:31 +00:00
Q_ASSERT ( findObject_ ! = 0 ) ;
2011-10-25 15:48:19 +00:00
ui_ = new Ui_mainWindow ( ) ;
ui_ - > setupUi ( this ) ;
2011-11-24 19:11:30 +00:00
aboutDialog_ = new AboutDialog ( this ) ;
2011-11-26 12:45:35 +00:00
this - > setStatusBar ( new QStatusBar ( ) ) ;
2011-10-25 15:48:19 +00:00
2012-08-29 19:04:14 +00:00
likelihoodCurve_ = new rtabmap : : PdfPlotCurve ( " Likelihood " , & imagesMap_ , this ) ;
2014-05-11 23:57:08 +00:00
inliersCurve_ = new rtabmap : : PdfPlotCurve ( " Inliers " , & imagesMap_ , this ) ;
likelihoodCurve_ - > setPen ( QPen ( Qt : : blue ) ) ;
inliersCurve_ - > setPen ( QPen ( Qt : : red ) ) ;
2012-08-29 19:04:14 +00:00
ui_ - > likelihoodPlot - > addCurve ( likelihoodCurve_ , false ) ;
2014-05-11 23:57:08 +00:00
ui_ - > likelihoodPlot - > addCurve ( inliersCurve_ , false ) ;
2012-08-28 13:44:57 +00:00
ui_ - > likelihoodPlot - > setGraphicsView ( true ) ;
2014-05-11 23:57:08 +00:00
ui_ - > dockWidget_statistics - > setVisible ( false ) ;
2012-08-28 13:44:57 +00:00
ui_ - > dockWidget_parameters - > setVisible ( false ) ;
ui_ - > dockWidget_plot - > setVisible ( false ) ;
2012-08-28 20:06:43 +00:00
ui_ - > widget_controls - > setVisible ( false ) ;
2012-08-28 13:44:57 +00:00
2011-10-25 15:48:19 +00:00
QByteArray geometry ;
2012-08-28 13:44:57 +00:00
QByteArray state ;
2014-07-31 19:02:31 +00:00
Settings : : loadWindowSettings ( geometry , state ) ;
2011-10-25 15:48:19 +00:00
this - > restoreGeometry ( geometry ) ;
2012-08-28 13:44:57 +00:00
this - > restoreState ( state ) ;
2014-05-20 15:52:25 +00:00
lastObjectsUpdateParameters_ = Settings : : getParameters ( ) ;
2011-10-25 15:48:19 +00:00
ui_ - > toolBox - > setupUi ( ) ;
2014-06-10 22:32:35 +00:00
if ( ! camera_ )
{
camera_ = new Camera ( this ) ;
}
else
{
camera_ - > setParent ( this ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_1deviceId ( ) ) - > setEnabled ( false ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_2imageWidth ( ) ) - > setEnabled ( false ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_3imageHeight ( ) ) - > setEnabled ( false ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_5mediaPath ( ) ) - > setEnabled ( false ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_6useTcpCamera ( ) ) - > setEnabled ( false ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_8port ( ) ) - > setEnabled ( false ) ;
2014-08-02 06:09:27 +00:00
ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_9queueSize ( ) ) - > setEnabled ( false ) ;
2014-06-10 22:32:35 +00:00
ui_ - > actionCamera_from_video_file - > setVisible ( false ) ;
ui_ - > actionCamera_from_TCP_IP - > setVisible ( false ) ;
ui_ - > actionCamera_from_directory_of_images - > setVisible ( false ) ;
ui_ - > actionLoad_scene_from_file - > setVisible ( false ) ;
}
2014-06-17 21:04:26 +00:00
if ( cv : : gpu : : getCudaEnabledDeviceCount ( ) = = 0 )
{
2014-10-24 02:46:03 +00:00
# ifdef WITH_NONFREE
2014-06-17 21:04:26 +00:00
ui_ - > toolBox - > updateParameter ( Settings : : kFeature2D_SURF_gpu ( ) ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kFeature2D_SURF_gpu ( ) ) - > setEnabled ( false ) ;
2014-06-19 21:52:04 +00:00
ui_ - > toolBox - > getParameterWidget ( Settings : : kFeature2D_SURF_keypointsRatio ( ) ) - > setEnabled ( false ) ;
2014-10-24 02:46:03 +00:00
# endif
ui_ - > toolBox - > updateParameter ( Settings : : kFeature2D_Fast_gpu ( ) ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kFeature2D_ORB_gpu ( ) ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kNearestNeighbor_BruteForce_gpu ( ) ) ;
2014-06-19 18:55:46 +00:00
ui_ - > toolBox - > getParameterWidget ( Settings : : kFeature2D_Fast_gpu ( ) ) - > setEnabled ( false ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kFeature2D_Fast_keypointsRatio ( ) ) - > setEnabled ( false ) ;
ui_ - > toolBox - > getParameterWidget ( Settings : : kFeature2D_ORB_gpu ( ) ) - > setEnabled ( false ) ;
2014-10-24 02:46:03 +00:00
ui_ - > toolBox - > getParameterWidget ( Settings : : kNearestNeighbor_BruteForce_gpu ( ) ) - > setEnabled ( false ) ;
2014-06-17 21:04:26 +00:00
}
2012-08-28 20:06:43 +00:00
connect ( ( QDoubleSpinBox * ) ui_ - > toolBox - > getParameterWidget ( Settings : : kCamera_4imageRate ( ) ) ,
2011-11-23 16:44:14 +00:00
SIGNAL ( editingFinished ( ) ) ,
camera_ ,
SLOT ( updateImageRate ( ) ) ) ;
2014-05-11 23:57:08 +00:00
ui_ - > menuView - > addAction ( ui_ - > dockWidget_statistics - > toggleViewAction ( ) ) ;
2011-10-25 15:48:19 +00:00
ui_ - > menuView - > addAction ( ui_ - > dockWidget_parameters - > toggleViewAction ( ) ) ;
ui_ - > menuView - > addAction ( ui_ - > dockWidget_objects - > toggleViewAction ( ) ) ;
2012-08-28 13:44:57 +00:00
ui_ - > menuView - > addAction ( ui_ - > dockWidget_plot - > toggleViewAction ( ) ) ;
2014-05-05 23:08:01 +00:00
connect ( ui_ - > toolBox , SIGNAL ( parametersChanged ( const QStringList & ) ) , this , SLOT ( notifyParametersChanged ( const QStringList & ) ) ) ;
2011-10-25 15:48:19 +00:00
2014-08-06 01:17:17 +00:00
ui_ - > imageView_source - > setGraphicsViewMode ( true ) ;
2012-08-29 19:25:49 +00:00
ui_ - > imageView_source - > setTextLabel ( tr ( " Press \" space \" to start the camera... " ) ) ;
2012-08-28 13:44:57 +00:00
ui_ - > imageView_source - > setMirrorView ( Settings : : getGeneral_mirrorView ( ) ) ;
connect ( ( QCheckBox * ) ui_ - > toolBox - > getParameterWidget ( Settings : : kGeneral_mirrorView ( ) ) ,
SIGNAL ( stateChanged ( int ) ) ,
this ,
SLOT ( updateMirrorView ( ) ) ) ;
2011-10-25 15:48:19 +00:00
2012-08-28 20:06:43 +00:00
ui_ - > widget_controls - > setVisible ( Settings : : getGeneral_controlsShown ( ) ) ;
connect ( ( QCheckBox * ) ui_ - > toolBox - > getParameterWidget ( Settings : : kGeneral_controlsShown ( ) ) ,
SIGNAL ( stateChanged ( int ) ) ,
this ,
SLOT ( showHideControls ( ) ) ) ;
2012-08-28 13:44:57 +00:00
//buttons
2011-10-25 15:48:19 +00:00
connect ( ui_ - > pushButton_restoreDefaults , SIGNAL ( clicked ( ) ) , ui_ - > toolBox , SLOT ( resetCurrentPage ( ) ) ) ;
2012-01-28 01:00:23 +00:00
connect ( ui_ - > pushButton_updateObjects , SIGNAL ( clicked ( ) ) , this , SLOT ( updateObjects ( ) ) ) ;
2012-08-28 13:44:57 +00:00
connect ( ui_ - > horizontalSlider_objectsSize , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( updateObjectsSize ( ) ) ) ;
2011-10-25 15:48:19 +00:00
ui_ - > actionStop_camera - > setEnabled ( false ) ;
2012-01-30 03:16:30 +00:00
ui_ - > actionPause_camera - > setEnabled ( false ) ;
2011-10-25 15:48:19 +00:00
ui_ - > actionSave_objects - > setEnabled ( false ) ;
// Actions
2012-02-04 22:15:32 +00:00
connect ( ui_ - > actionAdd_object_from_scene , SIGNAL ( triggered ( ) ) , this , SLOT ( addObjectFromScene ( ) ) ) ;
2012-01-30 00:54:23 +00:00
connect ( ui_ - > actionAdd_objects_from_files , SIGNAL ( triggered ( ) ) , this , SLOT ( addObjectsFromFiles ( ) ) ) ;
connect ( ui_ - > actionLoad_scene_from_file , SIGNAL ( triggered ( ) ) , this , SLOT ( loadSceneFromFile ( ) ) ) ;
2011-11-23 19:12:41 +00:00
connect ( ui_ - > actionStart_camera , SIGNAL ( triggered ( ) ) , this , SLOT ( startProcessing ( ) ) ) ;
2011-11-23 18:58:55 +00:00
connect ( ui_ - > actionStop_camera , SIGNAL ( triggered ( ) ) , this , SLOT ( stopProcessing ( ) ) ) ;
2012-01-30 03:16:30 +00:00
connect ( ui_ - > actionPause_camera , SIGNAL ( triggered ( ) ) , this , SLOT ( pauseProcessing ( ) ) ) ;
2011-10-25 15:48:19 +00:00
connect ( ui_ - > actionExit , SIGNAL ( triggered ( ) ) , this , SLOT ( close ( ) ) ) ;
connect ( ui_ - > actionSave_objects , SIGNAL ( triggered ( ) ) , this , SLOT ( saveObjects ( ) ) ) ;
connect ( ui_ - > actionLoad_objects , SIGNAL ( triggered ( ) ) , this , SLOT ( loadObjects ( ) ) ) ;
2012-02-04 22:15:32 +00:00
connect ( ui_ - > actionCamera_from_video_file , SIGNAL ( triggered ( ) ) , this , SLOT ( setupCameraFromVideoFile ( ) ) ) ;
2012-08-28 20:06:43 +00:00
connect ( ui_ - > actionCamera_from_directory_of_images , SIGNAL ( triggered ( ) ) , this , SLOT ( setupCameraFromImagesDirectory ( ) ) ) ;
2014-05-21 21:31:42 +00:00
connect ( ui_ - > actionCamera_from_TCP_IP , SIGNAL ( triggered ( ) ) , this , SLOT ( setupCameraFromTcpIp ( ) ) ) ;
2011-11-24 19:11:30 +00:00
connect ( ui_ - > actionAbout , SIGNAL ( triggered ( ) ) , aboutDialog_ , SLOT ( exec ( ) ) ) ;
2012-01-12 22:21:56 +00:00
connect ( ui_ - > actionRestore_all_default_settings , SIGNAL ( triggered ( ) ) , ui_ - > toolBox , SLOT ( resetAllPages ( ) ) ) ;
2012-02-04 00:11:11 +00:00
connect ( ui_ - > actionRemove_all_objects , SIGNAL ( triggered ( ) ) , this , SLOT ( removeAllObjects ( ) ) ) ;
2014-03-24 21:11:02 +00:00
connect ( ui_ - > actionSave_settings , SIGNAL ( triggered ( ) ) , this , SLOT ( saveSettings ( ) ) ) ;
connect ( ui_ - > actionLoad_settings , SIGNAL ( triggered ( ) ) , this , SLOT ( loadSettings ( ) ) ) ;
2011-11-23 19:12:41 +00:00
2012-08-28 20:06:43 +00:00
connect ( ui_ - > pushButton_play , SIGNAL ( clicked ( ) ) , this , SLOT ( startProcessing ( ) ) ) ;
connect ( ui_ - > pushButton_stop , SIGNAL ( clicked ( ) ) , this , SLOT ( stopProcessing ( ) ) ) ;
connect ( ui_ - > pushButton_pause , SIGNAL ( clicked ( ) ) , this , SLOT ( pauseProcessing ( ) ) ) ;
connect ( ui_ - > horizontalSlider_frames , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( moveCameraFrame ( int ) ) ) ;
connect ( ui_ - > horizontalSlider_frames , SIGNAL ( valueChanged ( int ) ) , ui_ - > label_frame , SLOT ( setNum ( int ) ) ) ;
ui_ - > pushButton_play - > setVisible ( true ) ;
ui_ - > pushButton_pause - > setVisible ( false ) ;
2014-05-21 21:31:42 +00:00
ui_ - > pushButton_stop - > setEnabled ( false ) ;
2012-08-28 20:06:43 +00:00
ui_ - > horizontalSlider_frames - > setEnabled ( false ) ;
2012-09-21 14:59:09 +00:00
ui_ - > label_frame - > setVisible ( false ) ;
2012-08-28 20:06:43 +00:00
2012-04-04 18:15:51 +00:00
ui_ - > objects_area - > addAction ( ui_ - > actionAdd_object_from_scene ) ;
ui_ - > objects_area - > addAction ( ui_ - > actionAdd_objects_from_files ) ;
ui_ - > objects_area - > setContextMenuPolicy ( Qt : : ActionsContextMenu ) ;
2012-02-04 22:15:32 +00:00
ui_ - > actionStart_camera - > setShortcut ( Qt : : Key_Space ) ;
ui_ - > actionPause_camera - > setShortcut ( Qt : : Key_Space ) ;
2012-08-28 20:06:43 +00:00
ui_ - > actionCamera_from_video_file - > setChecked ( ! Settings : : getCamera_5mediaPath ( ) . isEmpty ( ) & & ! UDirectory : : exists ( Settings : : getCamera_5mediaPath ( ) . toStdString ( ) ) ) ;
ui_ - > actionCamera_from_directory_of_images - > setChecked ( ! Settings : : getCamera_5mediaPath ( ) . isEmpty ( ) & & UDirectory : : exists ( Settings : : getCamera_5mediaPath ( ) . toStdString ( ) ) ) ;
2014-05-21 21:31:42 +00:00
ui_ - > actionCamera_from_TCP_IP - > setChecked ( Settings : : getCamera_6useTcpCamera ( ) ) ;
2012-01-30 03:16:30 +00:00
2014-05-05 23:08:01 +00:00
ui_ - > label_ipAddress - > setTextInteractionFlags ( Qt : : TextSelectableByMouse ) ;
ui_ - > label_port - > setTextInteractionFlags ( Qt : : TextSelectableByMouse ) ;
setupTCPServer ( ) ;
2014-07-31 19:02:31 +00:00
if ( findObject_ - > objects ( ) . size ( ) )
{
// show objects already loaded in FindObject
for ( QMap < int , ObjSignature * > : : const_iterator iter = findObject_ - > objects ( ) . constBegin ( ) ;
iter ! = findObject_ - > objects ( ) . constEnd ( ) ;
+ + iter )
{
ObjWidget * obj = new ObjWidget ( iter . key ( ) , iter . value ( ) - > keypoints ( ) , cvtCvMat2QImage ( iter . value ( ) - > image ( ) ) ) ;
objWidgets_ . insert ( obj - > id ( ) , obj ) ;
this - > showObject ( obj ) ;
}
}
2012-01-30 00:54:23 +00:00
if ( Settings : : getGeneral_autoStartCamera ( ) )
{
// Set 1 msec to see state on the status bar.
QTimer : : singleShot ( 1 , this , SLOT ( startProcessing ( ) ) ) ;
}
2011-10-25 15:48:19 +00:00
}
MainWindow : : ~ MainWindow ( )
{
2011-11-23 16:44:14 +00:00
disconnect ( camera_ , SIGNAL ( imageReceived ( const cv : : Mat & ) ) , this , SLOT ( update ( const cv : : Mat & ) ) ) ;
2014-08-03 22:40:37 +00:00
disconnect ( camera_ , SIGNAL ( finished ( ) ) , this , SLOT ( stopProcessing ( ) ) ) ;
2011-11-23 16:44:14 +00:00
camera_ - > stop ( ) ;
2014-07-31 19:02:31 +00:00
qDeleteAll ( objWidgets_ ) ;
objWidgets_ . clear ( ) ;
2011-10-25 15:48:19 +00:00
delete ui_ ;
2014-07-31 19:02:31 +00:00
delete findObject_ ;
2011-10-25 15:48:19 +00:00
}
void MainWindow : : closeEvent ( QCloseEvent * event )
{
2012-02-04 00:11:11 +00:00
bool quit = true ;
this - > stopProcessing ( ) ;
2014-07-31 19:02:31 +00:00
if ( objectsModified_ & & this - > isVisible ( ) & & objWidgets_ . size ( ) )
2012-02-04 00:11:11 +00:00
{
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 )
{
2014-07-31 19:02:31 +00:00
Settings : : saveWindowSettings ( this - > saveGeometry ( ) , this - > saveState ( ) ) ;
2012-02-04 00:11:11 +00:00
event - > accept ( ) ;
}
else
{
event - > ignore ( ) ;
}
2011-10-25 15:48:19 +00:00
}
2014-05-05 23:08:01 +00:00
void MainWindow : : setupTCPServer ( )
{
if ( tcpServer_ )
{
tcpServer_ - > close ( ) ;
delete tcpServer_ ;
}
tcpServer_ = new TcpServer ( Settings : : getGeneral_port ( ) , this ) ;
2014-08-06 14:09:55 +00:00
connect ( this , SIGNAL ( objectsFound ( find_object : : DetectionInfo ) ) , tcpServer_ , SLOT ( publishDetectionInfo ( find_object : : DetectionInfo ) ) ) ;
2014-05-05 23:08:01 +00:00
ui_ - > label_ipAddress - > setText ( tcpServer_ - > getHostAddress ( ) . toString ( ) ) ;
ui_ - > label_port - > setNum ( tcpServer_ - > getPort ( ) ) ;
2014-08-02 06:09:27 +00:00
UINFO ( " Detection sent on port: %d (IP=%s) " , tcpServer_ - > getPort ( ) , tcpServer_ - > getHostAddress ( ) . toString ( ) . toStdString ( ) . c_str ( ) ) ;
2014-05-05 23:08:01 +00:00
}
2012-02-04 22:48:35 +00:00
void MainWindow : : setSourceImageText ( const QString & text )
{
ui_ - > imageView_source - > setTextLabel ( text ) ;
}
2014-03-24 21:11:02 +00:00
void MainWindow : : loadSettings ( )
{
QString path = QFileDialog : : getOpenFileName ( this , tr ( " Load settings... " ) , Settings : : workingDirectory ( ) , " *.ini " ) ;
if ( ! path . isEmpty ( ) )
{
if ( QFileInfo ( path ) . suffix ( ) . compare ( " ini " ) ! = 0 )
{
path . append ( " .ini " ) ;
}
loadSettings ( path ) ;
}
}
void MainWindow : : saveSettings ( )
{
QString path = QFileDialog : : getSaveFileName ( this , tr ( " Save settings... " ) , Settings : : workingDirectory ( ) , " *.ini " ) ;
if ( ! path . isEmpty ( ) )
{
if ( QFileInfo ( path ) . suffix ( ) . compare ( " ini " ) ! = 0 )
{
path . append ( " .ini " ) ;
}
saveSettings ( path ) ;
}
}
bool MainWindow : : loadSettings ( const QString & path )
{
if ( ! path . isEmpty ( ) & & QFileInfo ( path ) . suffix ( ) . compare ( " ini " ) = = 0 )
{
QByteArray geometry ;
QByteArray state ;
2014-07-31 19:02:31 +00:00
Settings : : loadSettings ( path ) ;
Settings : : loadWindowSettings ( geometry , state , path ) ;
2014-03-24 21:11:02 +00:00
this - > restoreGeometry ( geometry ) ;
this - > restoreState ( state ) ;
//update parameters tool box
const ParametersMap & parameters = Settings : : getParameters ( ) ;
for ( ParametersMap : : const_iterator iter = parameters . begin ( ) ; iter ! = parameters . constEnd ( ) ; + + iter )
{
ui_ - > toolBox - > updateParameter ( iter . key ( ) ) ;
}
return true ;
}
2014-07-31 19:02:31 +00:00
UINFO ( " Path \" %s \" not valid (should be *.ini) " , path . toStdString ( ) . c_str ( ) ) ;
2014-03-24 21:11:02 +00:00
return false ;
}
bool MainWindow : : saveSettings ( const QString & path )
{
if ( ! path . isEmpty ( ) & & QFileInfo ( path ) . suffix ( ) . compare ( " ini " ) = = 0 )
{
2014-07-31 19:02:31 +00:00
Settings : : saveSettings ( path ) ;
Settings : : saveWindowSettings ( this - > saveGeometry ( ) , this - > saveState ( ) , path ) ;
2014-03-24 21:11:02 +00:00
return true ;
}
2014-07-31 19:02:31 +00:00
UINFO ( " Path \" %s \" not valid (should be *.ini) " , path . toStdString ( ) . c_str ( ) ) ;
2014-03-24 21:11:02 +00:00
return false ;
}
2012-02-04 22:38:28 +00:00
int MainWindow : : loadObjects ( const QString & dirPath )
2011-10-25 15:48:19 +00:00
{
2012-02-04 22:38:28 +00:00
int loadedObjects = 0 ;
2014-05-11 23:57:08 +00:00
QString formats = Settings : : getGeneral_imageFormats ( ) . remove ( ' * ' ) . remove ( ' . ' ) ;
UDirectory dir ( dirPath . toStdString ( ) , formats . toStdString ( ) ) ;
if ( dir . isValid ( ) )
2011-10-25 15:48:19 +00:00
{
2014-05-11 23:57:08 +00:00
const std : : list < std : : string > & names = dir . getFileNames ( ) ; // sorted in natural order
for ( std : : list < std : : string > : : const_iterator iter = names . begin ( ) ; iter ! = names . end ( ) ; + + iter )
2011-10-25 15:48:19 +00:00
{
2014-07-31 19:02:31 +00:00
if ( this - > addObjectFromFile ( ( dirPath . toStdString ( ) + dir . separator ( ) + * iter ) . c_str ( ) ) )
{
+ + loadedObjects ;
}
2012-02-04 00:11:11 +00:00
}
2014-07-31 19:02:31 +00:00
if ( loadedObjects )
2012-02-04 00:11:11 +00:00
{
this - > updateObjects ( ) ;
2011-10-25 15:48:19 +00:00
}
2011-11-23 18:58:55 +00:00
}
2012-02-04 22:38:28 +00:00
return loadedObjects ;
2011-11-23 18:58:55 +00:00
}
2014-07-31 19:02:31 +00:00
int MainWindow : : saveObjects ( const QString & dirPath )
2011-11-23 18:58:55 +00:00
{
2014-07-31 19:02:31 +00:00
int count = 0 ;
2012-02-04 00:11:11 +00:00
QDir dir ( dirPath ) ;
if ( dir . exists ( ) )
2011-11-23 18:58:55 +00:00
{
2014-07-31 19:02:31 +00:00
for ( QMap < int , ObjWidget * > : : iterator iter = objWidgets_ . begin ( ) ; iter ! = objWidgets_ . end ( ) ; + + iter )
2012-02-04 00:11:11 +00:00
{
2014-07-31 19:02:31 +00:00
if ( iter . value ( ) - > pixmap ( ) . save ( QString ( " %1/%2.png " ) . arg ( dirPath ) . arg ( iter . key ( ) ) ) )
{
+ + count ;
}
else
{
UERROR ( " Failed to save object %d " , iter . key ( ) ) ;
}
2012-02-04 00:11:11 +00:00
}
2014-05-11 23:57:08 +00:00
objectsModified_ = false ;
2011-11-23 18:58:55 +00:00
}
2014-07-31 19:02:31 +00:00
return count ;
2011-11-23 18:58:55 +00:00
}
void MainWindow : : loadObjects ( )
{
2012-05-07 22:36:59 +00:00
QString dirPath = QFileDialog : : getExistingDirectory ( this , tr ( " Loading objects... Select a directory. " ) , Settings : : workingDirectory ( ) ) ;
2012-02-04 00:11:11 +00:00
if ( ! dirPath . isEmpty ( ) )
2011-11-23 18:58:55 +00:00
{
2014-07-31 19:02:31 +00:00
int count = loadObjects ( dirPath ) ;
if ( count )
{
QMessageBox : : information ( this , tr ( " Loading... " ) , tr ( " %1 objects loaded from \" %2 \" . " ) . arg ( count ) . arg ( dirPath ) ) ;
}
else
{
QMessageBox : : information ( this , tr ( " Loading... " ) , tr ( " No objects loaded from \" %1 \" ! " ) . arg ( dirPath ) ) ;
}
2011-10-25 15:48:19 +00:00
}
}
2012-02-04 00:11:11 +00:00
bool MainWindow : : saveObjects ( )
2011-10-25 15:48:19 +00:00
{
2012-05-07 22:36:59 +00:00
QString dirPath = QFileDialog : : getExistingDirectory ( this , tr ( " Saving objects... Select a directory. " ) , Settings : : workingDirectory ( ) ) ;
2012-02-04 00:11:11 +00:00
if ( ! dirPath . isEmpty ( ) )
2011-10-25 15:48:19 +00:00
{
2014-07-31 19:02:31 +00:00
int count = saveObjects ( dirPath ) ;
if ( count )
{
QMessageBox : : information ( this , tr ( " Saving... " ) , tr ( " %1 objects saved to \" %2 \" . " ) . arg ( count ) . arg ( dirPath ) ) ;
}
else
{
QMessageBox : : warning ( this , tr ( " Saving... " ) , tr ( " No objects saved to %1! " ) . arg ( dirPath ) ) ;
}
return count > 0 ;
2011-10-25 15:48:19 +00:00
}
2012-02-04 00:11:11 +00:00
return false ;
2011-10-25 15:48:19 +00:00
}
2014-08-06 14:09:55 +00:00
void MainWindow : : removeObject ( find_object : : ObjWidget * object )
2011-10-25 15:48:19 +00:00
{
if ( object )
{
2014-07-31 19:02:31 +00:00
objWidgets_ . remove ( object - > id ( ) ) ;
if ( objWidgets_ . size ( ) = = 0 )
{
ui_ - > actionSave_objects - > setEnabled ( false ) ;
}
findObject_ - > removeObject ( object - > id ( ) ) ;
2011-10-25 15:48:19 +00:00
object - > deleteLater ( ) ;
2014-08-02 06:09:27 +00:00
if ( Settings : : getGeneral_autoUpdateObjects ( ) )
{
this - > updateVocabulary ( ) ;
}
2014-07-31 19:02:31 +00:00
if ( ! camera_ - > isRunning ( ) & & ! sceneImage_ . empty ( ) )
2012-02-04 22:15:32 +00:00
{
2014-08-03 21:35:28 +00:00
this - > update ( sceneImage_ ) ;
2012-02-04 22:15:32 +00:00
}
2011-10-25 15:48:19 +00:00
}
}
2012-02-04 00:11:11 +00:00
void MainWindow : : removeAllObjects ( )
{
2014-07-31 19:02:31 +00:00
qDeleteAll ( objWidgets_ ) ;
objWidgets_ . clear ( ) ;
ui_ - > actionSave_objects - > setEnabled ( false ) ;
findObject_ - > removeAllObjects ( ) ;
if ( ! camera_ - > isRunning ( ) & & ! sceneImage_ . empty ( ) )
2012-02-04 00:11:11 +00:00
{
2014-08-03 21:35:28 +00:00
this - > update ( sceneImage_ ) ;
2012-02-04 00:11:11 +00:00
}
}
2012-08-28 13:44:57 +00:00
void MainWindow : : updateObjectsSize ( )
{
2014-07-31 19:02:31 +00:00
for ( QMap < int , ObjWidget * > : : iterator iter = objWidgets_ . begin ( ) ; iter ! = objWidgets_ . end ( ) ; + + iter )
2012-08-28 13:44:57 +00:00
{
2014-07-31 19:02:31 +00:00
updateObjectSize ( iter . value ( ) ) ;
2012-08-28 13:44:57 +00:00
}
}
void MainWindow : : updateObjectSize ( ObjWidget * obj )
{
if ( obj )
{
int value = ui_ - > horizontalSlider_objectsSize - > value ( ) ;
if ( ( obj - > pixmap ( ) . width ( ) * value ) / 100 > 4 & & ( obj - > pixmap ( ) . height ( ) * value ) / 100 > 4 )
{
obj - > setVisible ( true ) ;
obj - > setMinimumSize ( ( obj - > pixmap ( ) . width ( ) * value ) / 100 , ( obj - > pixmap ( ) . height ( ) ) * value / 100 ) ;
}
else
{
obj - > setVisible ( false ) ;
}
}
}
void MainWindow : : updateMirrorView ( )
{
bool mirrorView = Settings : : getGeneral_mirrorView ( ) ;
ui_ - > imageView_source - > setMirrorView ( mirrorView ) ;
2014-07-31 19:02:31 +00:00
for ( QMap < int , ObjWidget * > : : iterator iter = objWidgets_ . begin ( ) ; iter ! = objWidgets_ . end ( ) ; + + iter )
2012-08-28 13:44:57 +00:00
{
2014-07-31 19:02:31 +00:00
iter . value ( ) - > setMirrorView ( mirrorView ) ;
2012-08-28 13:44:57 +00:00
}
}
2012-08-28 20:06:43 +00:00
void MainWindow : : showHideControls ( )
{
ui_ - > widget_controls - > setVisible ( Settings : : getGeneral_controlsShown ( ) ) ;
}
2012-02-04 22:15:32 +00:00
void MainWindow : : addObjectFromScene ( )
2011-10-25 15:48:19 +00:00
{
2011-11-23 19:12:41 +00:00
disconnect ( camera_ , SIGNAL ( imageReceived ( const cv : : Mat & ) ) , this , SLOT ( update ( const cv : : Mat & ) ) ) ;
2014-08-03 22:40:37 +00:00
disconnect ( camera_ , SIGNAL ( finished ( ) ) , this , SLOT ( stopProcessing ( ) ) ) ;
2012-02-04 22:15:32 +00:00
AddObjectDialog * dialog ;
bool resumeCamera = camera_ - > isRunning ( ) ;
2014-07-31 19:02:31 +00:00
if ( camera_ - > isRunning ( ) | | sceneImage_ . empty ( ) )
2011-10-25 15:48:19 +00:00
{
2012-05-07 22:36:59 +00:00
dialog = new AddObjectDialog ( camera_ , cv : : Mat ( ) , ui_ - > imageView_source - > isMirrorView ( ) , this ) ;
2011-10-25 15:48:19 +00:00
}
2012-02-04 22:15:32 +00:00
else
{
2014-07-31 19:02:31 +00:00
dialog = new AddObjectDialog ( 0 , sceneImage_ , ui_ - > imageView_source - > isMirrorView ( ) , this ) ;
2012-02-04 22:15:32 +00:00
}
if ( dialog - > exec ( ) = = QDialog : : Accepted )
{
2014-07-31 19:02:31 +00:00
ObjWidget * obj = 0 ;
ObjSignature * signature = 0 ;
dialog - > retrieveObject ( & obj , & signature ) ;
Q_ASSERT ( obj ! = 0 & & signature ! = 0 ) ;
findObject_ - > addObject ( signature ) ;
obj - > setId ( signature - > id ( ) ) ;
objWidgets_ . insert ( obj - > id ( ) , obj ) ;
ui_ - > actionSave_objects - > setEnabled ( true ) ;
showObject ( obj ) ;
QLabel * detectorDescriptorType = qFindChild < QLabel * > ( this , QString ( " %1type " ) . arg ( obj - > id ( ) ) ) ;
detectorDescriptorType - > setText ( QString ( " %1/%2 " ) . arg ( signature - > detectorType ( ) ) . arg ( signature - > descriptorType ( ) ) ) ;
updateVocabulary ( ) ;
objectsModified_ = true ;
2012-02-04 22:15:32 +00:00
}
2014-07-31 19:02:31 +00:00
if ( resumeCamera | | sceneImage_ . empty ( ) )
2012-02-04 22:15:32 +00:00
{
this - > startProcessing ( ) ;
}
else
{
2014-07-24 13:02:31 +00:00
connect ( camera_ , SIGNAL ( imageReceived ( const cv : : Mat & ) ) , this , SLOT ( update ( const cv : : Mat & ) ) , Qt : : UniqueConnection ) ;
2014-08-03 22:40:37 +00:00
connect ( camera_ , SIGNAL ( finished ( ) ) , this , SLOT ( stopProcessing ( ) ) , Qt : : UniqueConnection ) ;
2014-08-03 21:35:28 +00:00
if ( ! sceneImage_ . empty ( ) )
{
this - > update ( sceneImage_ ) ;
}
2012-02-04 22:15:32 +00:00
}
delete dialog ;
2011-10-25 15:48:19 +00:00
}
2012-01-30 00:54:23 +00:00
void MainWindow : : addObjectsFromFiles ( )
{
2012-02-04 22:15:32 +00:00
QStringList fileNames = QFileDialog : : getOpenFileNames ( this , tr ( " Add objects... " ) , Settings : : workingDirectory ( ) , tr ( " Image Files (%1) " ) . arg ( Settings : : getGeneral_imageFormats ( ) ) ) ;
2012-01-30 00:54:23 +00:00
if ( fileNames . size ( ) )
{
for ( int i = 0 ; i < fileNames . size ( ) ; + + i )
{
2012-02-04 00:11:11 +00:00
this - > addObjectFromFile ( fileNames . at ( i ) ) ;
}
objectsModified_ = true ;
updateObjects ( ) ;
}
}
2014-07-31 19:02:31 +00:00
bool MainWindow : : addObjectFromFile ( const QString & filePath )
2012-02-04 00:11:11 +00:00
{
2014-07-31 19:02:31 +00:00
const ObjSignature * s = findObject_ - > addObject ( filePath ) ;
if ( s )
2012-02-04 00:11:11 +00:00
{
2014-07-31 19:02:31 +00:00
ObjWidget * obj = new ObjWidget ( s - > id ( ) , std : : vector < cv : : KeyPoint > ( ) , cvtCvMat2QImage ( s - > image ( ) ) ) ;
objWidgets_ . insert ( obj - > id ( ) , obj ) ;
ui_ - > actionSave_objects - > setEnabled ( true ) ;
this - > showObject ( obj ) ;
return true ;
}
else
{
QMessageBox : : critical ( this , tr ( " Error adding object " ) , tr ( " Failed to add object from \" %1 \" " ) . arg ( filePath ) ) ;
return false ;
2012-01-30 00:54:23 +00:00
}
}
void MainWindow : : loadSceneFromFile ( )
{
2012-02-04 22:15:32 +00:00
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Load scene... " ) , Settings : : workingDirectory ( ) , tr ( " Image Files (%1) " ) . arg ( Settings : : getGeneral_imageFormats ( ) ) ) ;
2012-01-30 00:54:23 +00:00
if ( ! fileName . isEmpty ( ) )
{
2012-05-07 22:36:59 +00:00
cv : : Mat img = cv : : imread ( fileName . toStdString ( ) . c_str ( ) ) ;
if ( ! img . empty ( ) )
2012-01-30 00:54:23 +00:00
{
2012-05-07 22:36:59 +00:00
this - > update ( img ) ;
2012-01-30 00:54:23 +00:00
ui_ - > label_timeRefreshRate - > setVisible ( false ) ;
}
}
}
2012-01-30 03:16:30 +00:00
void MainWindow : : setupCameraFromVideoFile ( )
{
2012-02-04 22:15:32 +00:00
if ( ! ui_ - > actionCamera_from_video_file - > isChecked ( ) )
2012-01-30 03:16:30 +00:00
{
2012-08-28 20:06:43 +00:00
Settings : : setCamera_5mediaPath ( " " ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_5mediaPath ( ) ) ;
2012-02-04 22:15:32 +00:00
}
else
{
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Setup camera from video file... " ) , Settings : : workingDirectory ( ) , tr ( " Video Files (%1) " ) . arg ( Settings : : getGeneral_videoFormats ( ) ) ) ;
if ( ! fileName . isEmpty ( ) )
2012-01-30 03:16:30 +00:00
{
2014-08-03 21:35:28 +00:00
Settings : : setCamera_6useTcpCamera ( false ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_6useTcpCamera ( ) ) ;
2012-08-28 20:06:43 +00:00
Settings : : setCamera_5mediaPath ( fileName ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_5mediaPath ( ) ) ;
2012-02-04 22:15:32 +00:00
if ( camera_ - > isRunning ( ) )
{
this - > stopProcessing ( ) ;
this - > startProcessing ( ) ;
}
2012-08-28 20:06:43 +00:00
Settings : : setGeneral_controlsShown ( true ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kGeneral_controlsShown ( ) ) ;
2012-01-30 03:16:30 +00:00
}
}
2012-08-28 20:06:43 +00:00
ui_ - > actionCamera_from_video_file - > setChecked ( ! Settings : : getCamera_5mediaPath ( ) . isEmpty ( ) ) ;
ui_ - > actionCamera_from_directory_of_images - > setChecked ( false ) ;
2014-05-21 21:31:42 +00:00
ui_ - > actionCamera_from_TCP_IP - > setChecked ( false ) ;
2012-08-28 20:06:43 +00:00
}
void MainWindow : : setupCameraFromImagesDirectory ( )
{
if ( ! ui_ - > actionCamera_from_directory_of_images - > isChecked ( ) )
{
Settings : : setCamera_5mediaPath ( " " ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_5mediaPath ( ) ) ;
}
else
{
QString directory = QFileDialog : : getExistingDirectory ( this , tr ( " Setup camera from directory of images... " ) , Settings : : workingDirectory ( ) ) ;
if ( ! directory . isEmpty ( ) )
{
2014-08-03 21:35:28 +00:00
Settings : : setCamera_6useTcpCamera ( false ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_6useTcpCamera ( ) ) ;
2012-08-28 20:06:43 +00:00
Settings : : setCamera_5mediaPath ( directory ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_5mediaPath ( ) ) ;
if ( camera_ - > isRunning ( ) )
{
this - > stopProcessing ( ) ;
this - > startProcessing ( ) ;
}
Settings : : setGeneral_controlsShown ( true ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kGeneral_controlsShown ( ) ) ;
}
}
ui_ - > actionCamera_from_directory_of_images - > setChecked ( ! Settings : : getCamera_5mediaPath ( ) . isEmpty ( ) ) ;
ui_ - > actionCamera_from_video_file - > setChecked ( false ) ;
2014-05-21 21:31:42 +00:00
ui_ - > actionCamera_from_TCP_IP - > setChecked ( false ) ;
}
void MainWindow : : setupCameraFromTcpIp ( )
{
if ( ! ui_ - > actionCamera_from_TCP_IP - > isChecked ( ) )
{
Settings : : setCamera_6useTcpCamera ( false ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_6useTcpCamera ( ) ) ;
}
else
{
2014-08-02 06:09:27 +00:00
bool ok ;
int port = QInputDialog : : getInteger ( this , tr ( " Server port... " ) , " Port: " , Settings : : getCamera_8port ( ) , 1 , USHRT_MAX , 1 , & ok ) ;
2014-05-21 21:31:42 +00:00
2014-08-02 06:09:27 +00:00
if ( ok )
{
int queue = QInputDialog : : getInteger ( this , tr ( " Queue size... " ) , " Images buffer size (0 means infinite): " , Settings : : getCamera_9queueSize ( ) , 0 , 2147483647 , 1 , & ok ) ;
if ( ok )
2014-05-21 21:31:42 +00:00
{
Settings : : setCamera_6useTcpCamera ( true ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_6useTcpCamera ( ) ) ;
Settings : : setCamera_8port ( port ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_8port ( ) ) ;
2014-08-02 06:09:27 +00:00
Settings : : setCamera_9queueSize ( queue ) ;
ui_ - > toolBox - > updateParameter ( Settings : : kCamera_9queueSize ( ) ) ;
2014-05-21 21:31:42 +00:00
if ( camera_ - > isRunning ( ) )
{
this - > stopProcessing ( ) ;
}
this - > startProcessing ( ) ;
}
}
}
ui_ - > actionCamera_from_directory_of_images - > setChecked ( false ) ;
ui_ - > actionCamera_from_video_file - > setChecked ( false ) ;
ui_ - > actionCamera_from_TCP_IP - > setChecked ( Settings : : getCamera_6useTcpCamera ( ) ) ;
2012-01-30 03:16:30 +00:00
}
2011-11-15 16:37:40 +00:00
void MainWindow : : showObject ( ObjWidget * obj )
2011-10-25 15:48:19 +00:00
{
if ( obj )
{
obj - > setGraphicsViewMode ( false ) ;
2012-01-28 01:00:23 +00:00
obj - > setMirrorView ( ui_ - > imageView_source - > isMirrorView ( ) ) ;
2011-11-15 16:37:40 +00:00
QList < ObjWidget * > objs = ui_ - > objects_area - > findChildren < ObjWidget * > ( ) ;
2011-10-25 15:48:19 +00:00
QVBoxLayout * vLayout = new QVBoxLayout ( ) ;
2012-01-30 03:16:30 +00:00
ui_ - > toolBox - > updateParameter ( Settings : : kGeneral_nextObjID ( ) ) ;
2011-10-25 15:48:19 +00:00
2014-07-31 19:02:31 +00:00
QLabel * title = new QLabel ( QString ( " %1 (%2) " ) . arg ( obj - > id ( ) ) . arg ( obj - > keypoints ( ) . size ( ) ) , this ) ;
QLabel * detectorDescriptorType = new QLabel ( QString ( " %1/%2 " ) . arg ( " detector " ) . arg ( " descriptor " ) , this ) ;
2012-01-28 01:00:23 +00:00
QLabel * detectedLabel = new QLabel ( this ) ;
title - > setObjectName ( QString ( " %1title " ) . arg ( obj - > id ( ) ) ) ;
detectorDescriptorType - > setObjectName ( QString ( " %1type " ) . arg ( obj - > id ( ) ) ) ;
2011-10-25 15:48:19 +00:00
detectedLabel - > setObjectName ( QString ( " %1detection " ) . arg ( obj - > id ( ) ) ) ;
QHBoxLayout * hLayout = new QHBoxLayout ( ) ;
hLayout - > addWidget ( title ) ;
2012-08-28 13:44:57 +00:00
hLayout - > addStretch ( 1 ) ;
2011-10-25 15:48:19 +00:00
hLayout - > addWidget ( detectorDescriptorType ) ;
hLayout - > addStretch ( 1 ) ;
2012-08-28 13:44:57 +00:00
hLayout - > addWidget ( detectedLabel ) ;
2011-10-25 15:48:19 +00:00
vLayout - > addLayout ( hLayout ) ;
vLayout - > addWidget ( obj ) ;
2014-07-31 19:02:31 +00:00
obj - > setDeletable ( true ) ;
2014-08-06 14:09:55 +00:00
connect ( obj , SIGNAL ( removalTriggered ( find_object : : ObjWidget * ) ) , this , SLOT ( removeObject ( find_object : : ObjWidget * ) ) ) ;
2011-10-25 15:48:19 +00:00
connect ( obj , SIGNAL ( destroyed ( QObject * ) ) , title , SLOT ( deleteLater ( ) ) ) ;
connect ( obj , SIGNAL ( destroyed ( QObject * ) ) , detectedLabel , SLOT ( deleteLater ( ) ) ) ;
connect ( obj , SIGNAL ( destroyed ( QObject * ) ) , detectorDescriptorType , SLOT ( deleteLater ( ) ) ) ;
connect ( obj , SIGNAL ( destroyed ( QObject * ) ) , vLayout , SLOT ( deleteLater ( ) ) ) ;
ui_ - > verticalLayout_objects - > insertLayout ( ui_ - > verticalLayout_objects - > count ( ) - 1 , vLayout ) ;
2012-08-28 13:44:57 +00:00
QByteArray ba ;
QBuffer buffer ( & ba ) ;
buffer . open ( QIODevice : : WriteOnly ) ;
obj - > pixmap ( ) . scaledToWidth ( 128 ) . save ( & buffer , " JPEG " ) ; // writes image into JPEG format
imagesMap_ . insert ( obj - > id ( ) , ba ) ;
updateObjectSize ( obj ) ;
2011-10-25 15:48:19 +00:00
}
}
2014-07-31 19:02:31 +00:00
void MainWindow : : updateObjects ( )
2014-05-13 17:47:59 +00:00
{
2014-07-31 19:02:31 +00:00
this - > statusBar ( ) - > showMessage ( tr ( " Updating %1 objects... " ) . arg ( findObject_ - > objects ( ) . size ( ) ) ) ;
QApplication : : processEvents ( ) ;
2014-05-13 17:47:59 +00:00
2014-07-31 19:02:31 +00:00
findObject_ - > updateObjects ( ) ;
2014-05-13 17:47:59 +00:00
2014-07-31 19:02:31 +00:00
updateVocabulary ( ) ;
2014-05-11 23:57:08 +00:00
2014-07-31 19:02:31 +00:00
QList < ObjSignature * > signatures = findObject_ - > objects ( ) . values ( ) ;
for ( int i = 0 ; i < signatures . size ( ) ; + + i )
2014-05-11 23:57:08 +00:00
{
2014-07-31 19:02:31 +00:00
objWidgets_ . value ( signatures [ i ] - > id ( ) ) - > setData ( signatures [ i ] - > keypoints ( ) , cvtCvMat2QImage ( signatures [ i ] - > image ( ) ) ) ;
2014-05-13 17:47:59 +00:00
2014-07-31 19:02:31 +00:00
//update object labels
QLabel * title = qFindChild < QLabel * > ( this , QString ( " %1title " ) . arg ( signatures [ i ] - > id ( ) ) ) ;
title - > setText ( QString ( " %1 (%2) " ) . arg ( signatures [ i ] - > id ( ) ) . arg ( QString : : number ( signatures [ i ] - > keypoints ( ) . size ( ) ) ) ) ;
QLabel * detectorDescriptorType = qFindChild < QLabel * > ( this , QString ( " %1type " ) . arg ( signatures [ i ] - > id ( ) ) ) ;
detectorDescriptorType - > setText ( QString ( " %1/%2 " ) . arg ( signatures [ i ] - > detectorType ( ) ) . arg ( signatures [ i ] - > descriptorType ( ) ) ) ;
2014-05-11 23:57:08 +00:00
}
2014-07-31 19:02:31 +00:00
if ( ! camera_ - > isRunning ( ) & & ! sceneImage_ . empty ( ) )
2012-01-28 01:00:23 +00:00
{
2014-08-03 21:35:28 +00:00
this - > update ( sceneImage_ ) ;
2012-01-28 01:00:23 +00:00
}
this - > statusBar ( ) - > clearMessage ( ) ;
}
2014-07-31 19:02:31 +00:00
void MainWindow : : updateVocabulary ( )
2011-10-25 15:48:19 +00:00
{
2014-07-31 19:02:31 +00:00
this - > statusBar ( ) - > showMessage ( tr ( " Updating vocabulary... " ) ) ;
QApplication : : processEvents ( ) ;
2011-10-25 15:48:19 +00:00
2014-07-31 19:02:31 +00:00
QTime time ;
2014-08-22 22:36:09 +00:00
time . start ( ) ;
2014-07-31 19:02:31 +00:00
findObject_ - > updateVocabulary ( ) ;
2011-10-25 15:48:19 +00:00
2014-07-31 19:02:31 +00:00
if ( findObject_ - > vocabulary ( ) - > size ( ) )
2011-10-25 15:48:19 +00:00
{
2014-07-31 19:02:31 +00:00
ui_ - > label_timeIndexing - > setNum ( time . elapsed ( ) ) ;
ui_ - > label_vocabularySize - > setNum ( findObject_ - > vocabulary ( ) - > size ( ) ) ;
2011-10-25 15:48:19 +00:00
}
2014-05-20 15:52:25 +00:00
lastObjectsUpdateParameters_ = Settings : : getParameters ( ) ;
2014-07-31 19:02:31 +00:00
this - > statusBar ( ) - > clearMessage ( ) ;
2011-10-25 15:48:19 +00:00
}
2011-11-23 18:58:55 +00:00
void MainWindow : : startProcessing ( )
2011-10-25 15:48:19 +00:00
{
2014-07-31 19:02:31 +00:00
UINFO ( " Starting camera... " ) ;
2012-02-04 22:15:32 +00:00
bool updateStatusMessage = this - > statusBar ( ) - > currentMessage ( ) . isEmpty ( ) ;
if ( updateStatusMessage )
{
this - > statusBar ( ) - > showMessage ( tr ( " Starting camera... " ) ) ;
}
2011-11-23 16:44:14 +00:00
if ( camera_ - > start ( ) )
2011-10-25 15:48:19 +00:00
{
2014-07-24 13:02:31 +00:00
connect ( camera_ , SIGNAL ( imageReceived ( const cv : : Mat & ) ) , this , SLOT ( update ( const cv : : Mat & ) ) , Qt : : UniqueConnection ) ;
2014-08-03 22:40:37 +00:00
connect ( camera_ , SIGNAL ( finished ( ) ) , this , SLOT ( stopProcessing ( ) ) , Qt : : UniqueConnection ) ;
2011-10-25 15:48:19 +00:00
ui_ - > actionStop_camera - > setEnabled ( true ) ;
2012-01-30 03:16:30 +00:00
ui_ - > actionPause_camera - > setEnabled ( true ) ;
2011-10-25 15:48:19 +00:00
ui_ - > actionStart_camera - > setEnabled ( false ) ;
2012-01-30 00:54:23 +00:00
ui_ - > actionLoad_scene_from_file - > setEnabled ( false ) ;
2012-08-31 14:35:43 +00:00
ui_ - > actionCamera_from_directory_of_images - > setEnabled ( false ) ;
ui_ - > actionCamera_from_video_file - > setEnabled ( false ) ;
2014-05-21 21:31:42 +00:00
ui_ - > actionCamera_from_TCP_IP - > setEnabled ( false ) ;
2012-01-30 00:54:23 +00:00
ui_ - > label_timeRefreshRate - > setVisible ( true ) ;
2012-08-28 20:06:43 +00:00
//update control bar
ui_ - > pushButton_play - > setVisible ( false ) ;
ui_ - > pushButton_pause - > setVisible ( true ) ;
ui_ - > pushButton_stop - > setEnabled ( true ) ;
int totalFrames = camera_ - > getTotalFrames ( ) ;
2012-09-21 14:59:09 +00:00
if ( totalFrames > 0 )
2012-08-28 20:06:43 +00:00
{
ui_ - > label_frame - > setVisible ( true ) ;
ui_ - > horizontalSlider_frames - > setEnabled ( true ) ;
ui_ - > horizontalSlider_frames - > setMaximum ( totalFrames - 1 ) ;
}
2014-08-02 06:09:27 +00:00
//update camera port if TCP is used
ui_ - > label_port_image - > setText ( " - " ) ;
if ( Settings : : getCamera_6useTcpCamera ( ) & & camera_ - > getPort ( ) )
{
ui_ - > label_port_image - > setNum ( camera_ - > getPort ( ) ) ;
}
2012-02-04 22:15:32 +00:00
if ( updateStatusMessage )
{
this - > statusBar ( ) - > showMessage ( tr ( " Camera started. " ) , 2000 ) ;
}
2011-10-25 15:48:19 +00:00
}
else
{
2012-02-04 22:15:32 +00:00
if ( updateStatusMessage )
{
this - > statusBar ( ) - > clearMessage ( ) ;
}
2014-07-31 19:02:31 +00:00
if ( Settings : : getCamera_6useTcpCamera ( ) )
2014-07-25 19:31:34 +00:00
{
2014-08-02 06:09:27 +00:00
QMessageBox : : critical ( this , tr ( " Camera error " ) , tr ( " Camera initialization failed! (with port %1) " ) . arg ( Settings : : getCamera_8port ( ) ) ) ;
2014-07-25 19:31:34 +00:00
}
2012-01-30 03:16:30 +00:00
else
{
2014-07-31 19:02:31 +00:00
QMessageBox : : critical ( this , tr ( " Camera error " ) , tr ( " Camera initialization failed! (with device %1) " ) . arg ( Settings : : getCamera_1deviceId ( ) ) ) ;
2012-01-30 03:16:30 +00:00
}
2011-10-25 15:48:19 +00:00
}
}
2011-11-23 18:58:55 +00:00
void MainWindow : : stopProcessing ( )
2011-10-25 15:48:19 +00:00
{
if ( camera_ )
{
2011-11-23 16:44:14 +00:00
disconnect ( camera_ , SIGNAL ( imageReceived ( const cv : : Mat & ) ) , this , SLOT ( update ( const cv : : Mat & ) ) ) ;
2014-08-03 22:40:37 +00:00
disconnect ( camera_ , SIGNAL ( finished ( ) ) , this , SLOT ( stopProcessing ( ) ) ) ;
2011-11-23 16:44:14 +00:00
camera_ - > stop ( ) ;
2011-10-25 15:48:19 +00:00
}
ui_ - > actionStop_camera - > setEnabled ( false ) ;
2012-01-30 03:16:30 +00:00
ui_ - > actionPause_camera - > setEnabled ( false ) ;
2011-10-25 15:48:19 +00:00
ui_ - > actionStart_camera - > setEnabled ( true ) ;
2012-01-30 00:54:23 +00:00
ui_ - > actionLoad_scene_from_file - > setEnabled ( true ) ;
2012-08-31 14:35:43 +00:00
ui_ - > actionCamera_from_directory_of_images - > setEnabled ( true ) ;
ui_ - > actionCamera_from_video_file - > setEnabled ( true ) ;
2014-05-21 21:31:42 +00:00
ui_ - > actionCamera_from_TCP_IP - > setEnabled ( true ) ;
2012-08-28 20:06:43 +00:00
ui_ - > pushButton_play - > setVisible ( true ) ;
ui_ - > pushButton_pause - > setVisible ( false ) ;
ui_ - > pushButton_stop - > setEnabled ( false ) ;
ui_ - > horizontalSlider_frames - > setEnabled ( false ) ;
ui_ - > horizontalSlider_frames - > setValue ( 0 ) ;
ui_ - > label_frame - > setVisible ( false ) ;
2014-08-02 06:09:27 +00:00
ui_ - > label_port_image - > setText ( " - " ) ;
2011-10-25 15:48:19 +00:00
}
2012-01-30 03:16:30 +00:00
void MainWindow : : pauseProcessing ( )
{
ui_ - > actionStop_camera - > setEnabled ( true ) ;
ui_ - > actionPause_camera - > setEnabled ( true ) ;
ui_ - > actionStart_camera - > setEnabled ( false ) ;
if ( camera_ - > isRunning ( ) )
{
2012-08-28 20:06:43 +00:00
ui_ - > pushButton_play - > setVisible ( true ) ;
ui_ - > pushButton_pause - > setVisible ( false ) ;
2012-01-30 03:16:30 +00:00
camera_ - > pause ( ) ;
}
else
{
2012-08-28 20:06:43 +00:00
ui_ - > pushButton_play - > setVisible ( false ) ;
ui_ - > pushButton_pause - > setVisible ( true ) ;
2012-01-30 03:16:30 +00:00
camera_ - > start ( ) ;
}
}
2012-08-28 20:06:43 +00:00
void MainWindow : : moveCameraFrame ( int frame )
{
if ( ui_ - > horizontalSlider_frames - > isEnabled ( ) )
{
camera_ - > moveToFrame ( frame ) ;
if ( ! camera_ - > isRunning ( ) )
{
camera_ - > takeImage ( ) ;
}
}
}
2014-05-13 17:47:59 +00:00
void MainWindow : : rectHovered ( int objId )
{
if ( objId > = 0 & & Settings : : getGeneral_autoScroll ( ) )
{
QLabel * label = ui_ - > dockWidget_objects - > findChild < QLabel * > ( QString ( " %1title " ) . arg ( objId ) ) ;
if ( label )
{
ui_ - > objects_area - > verticalScrollBar ( ) - > setValue ( label - > pos ( ) . y ( ) ) ;
}
}
}
2011-11-23 16:44:14 +00:00
void MainWindow : : update ( const cv : : Mat & image )
2011-10-25 15:48:19 +00:00
{
2014-08-02 06:09:27 +00:00
if ( image . empty ( ) )
{
2014-08-03 21:35:28 +00:00
UWARN ( " The image received is empty... " ) ;
2014-08-02 06:09:27 +00:00
return ;
}
2014-08-03 21:35:28 +00:00
sceneImage_ = image . clone ( ) ;
2014-08-02 06:09:27 +00:00
2011-10-25 15:48:19 +00:00
// reset objects color
2014-07-31 19:02:31 +00:00
for ( QMap < int , ObjWidget * > : : iterator iter = objWidgets_ . begin ( ) ; iter ! = objWidgets_ . end ( ) ; + + iter )
2011-10-25 15:48:19 +00:00
{
2014-07-31 19:02:31 +00:00
iter . value ( ) - > resetKptsColor ( ) ;
2011-10-25 15:48:19 +00:00
}
2014-06-19 17:43:34 +00:00
2014-08-04 01:33:52 +00:00
DetectionInfo info ;
if ( findObject_ - > detect ( sceneImage_ , info ) )
2014-07-31 19:02:31 +00:00
{
2014-08-04 01:33:52 +00:00
ui_ - > label_timeDetection - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeKeypointDetection , 0 ) ) ;
2014-08-22 22:36:09 +00:00
ui_ - > label_timeSkewAffine - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeSkewAffine , 0 ) ) ;
2014-08-04 01:33:52 +00:00
ui_ - > label_timeExtraction - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeDescriptorExtraction , 0 ) ) ;
ui_ - > imageView_source - > setData ( info . sceneKeypoints_ , cvtCvMat2QImage ( sceneImage_ ) ) ;
2014-08-22 22:36:09 +00:00
if ( ! findObject_ - > vocabulary ( ) - > size ( ) )
{
ui_ - > label_timeIndexing - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeIndexing , 0 ) ) ;
}
2014-08-04 01:33:52 +00:00
ui_ - > label_timeMatching - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeMatching , 0 ) ) ;
ui_ - > label_timeHomographies - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeHomography , 0 ) ) ;
2014-07-31 19:02:31 +00:00
ui_ - > label_vocabularySize - > setNum ( findObject_ - > vocabulary ( ) - > size ( ) ) ;
2011-11-23 16:44:14 +00:00
2014-07-31 19:02:31 +00:00
// Colorize features matched
2014-08-04 01:33:52 +00:00
const QMap < int , QMultiMap < int , int > > & matches = info . matches_ ;
2014-07-31 19:02:31 +00:00
QMap < int , int > scores ;
int maxScoreId = - 1 ;
int maxScore = 0 ;
for ( QMap < int , QMultiMap < int , int > > : : const_iterator jter = matches . constBegin ( ) ; jter ! = matches . end ( ) ; + + jter )
2011-11-23 16:44:14 +00:00
{
2014-07-31 19:02:31 +00:00
scores . insert ( jter . key ( ) , jter . value ( ) . size ( ) ) ;
if ( maxScoreId = = - 1 | | maxScore < jter . value ( ) . size ( ) )
2014-05-13 17:47:59 +00:00
{
2014-07-31 19:02:31 +00:00
maxScoreId = jter . key ( ) ;
maxScore = jter . value ( ) . size ( ) ;
2014-05-13 17:47:59 +00:00
}
2014-07-31 19:02:31 +00:00
int id = jter . key ( ) ;
QLabel * label = ui_ - > dockWidget_objects - > findChild < QLabel * > ( QString ( " %1detection " ) . arg ( id ) ) ;
if ( ! Settings : : getHomography_homographyComputed ( ) )
2012-01-05 00:31:29 +00:00
{
2014-07-31 19:02:31 +00:00
label - > setText ( QString ( " %1 matches " ) . arg ( jter . value ( ) . size ( ) ) ) ;
2011-10-25 15:48:19 +00:00
2014-07-31 19:02:31 +00:00
ObjWidget * obj = objWidgets_ . value ( id ) ;
Q_ASSERT ( obj ! = 0 ) ;
2014-03-21 22:26:06 +00:00
2014-07-31 19:02:31 +00:00
for ( QMultiMap < int , int > : : const_iterator iter = jter . value ( ) . constBegin ( ) ; iter ! = jter . value ( ) . constEnd ( ) ; + + iter )
2014-05-11 23:57:08 +00:00
{
2014-08-03 22:23:02 +00:00
obj - > setKptColor ( iter . key ( ) , obj - > color ( ) ) ;
ui_ - > imageView_source - > setKptColor ( iter . value ( ) , obj - > color ( ) ) ;
2014-05-11 23:57:08 +00:00
}
2012-08-28 13:44:57 +00:00
}
2014-08-04 01:33:52 +00:00
else if ( ! info . objDetected_ . contains ( id ) )
2012-01-04 17:30:36 +00:00
{
2014-07-31 19:02:31 +00:00
// Homography could not be computed...
QLabel * label = ui_ - > dockWidget_objects - > findChild < QLabel * > ( QString ( " %1detection " ) . arg ( id ) ) ;
2014-08-04 01:33:52 +00:00
QMultiMap < int , int > rejectedInliers = info . rejectedInliers_ . value ( id ) ;
QMultiMap < int , int > rejectedOutliers = info . rejectedOutliers_ . value ( id ) ;
2014-08-06 01:17:17 +00:00
int rejectedCode = info . rejectedCodes_ . value ( id ) ;
if ( rejectedCode = = DetectionInfo : : kRejectedLowMatches )
2012-01-04 17:30:36 +00:00
{
2014-07-31 19:02:31 +00:00
label - > setText ( QString ( " Too low matches (%1) " ) . arg ( jter . value ( ) . size ( ) ) ) ;
2014-03-21 22:26:06 +00:00
}
2014-08-06 01:17:17 +00:00
else if ( rejectedCode = = DetectionInfo : : kRejectedAllInliers )
2014-03-21 22:26:06 +00:00
{
2014-07-31 19:02:31 +00:00
label - > setText ( QString ( " Ignored, all inliers (%1 in %2 out) " ) . arg ( rejectedInliers . size ( ) ) . arg ( rejectedOutliers . size ( ) ) ) ;
2012-01-04 17:30:36 +00:00
}
2014-08-06 01:17:17 +00:00
else if ( rejectedCode = = DetectionInfo : : kRejectedNotValid )
{
label - > setText ( QString ( " Not valid homography (%1 in %2 out) " ) . arg ( rejectedInliers . size ( ) ) . arg ( rejectedOutliers . size ( ) ) ) ;
}
else if ( rejectedCode = = DetectionInfo : : kRejectedLowInliers )
2012-01-04 17:30:36 +00:00
{
2014-07-31 19:02:31 +00:00
label - > setText ( QString ( " Too low inliers (%1 in %2 out) " ) . arg ( rejectedInliers . size ( ) ) . arg ( rejectedOutliers . size ( ) ) ) ;
2012-01-04 17:30:36 +00:00
}
2014-08-06 01:17:17 +00:00
else if ( rejectedCode = = DetectionInfo : : kRejectedCornersOutside )
{
label - > setText ( QString ( " Corners not visible (%1 in %2 out) " ) . arg ( rejectedInliers . size ( ) ) . arg ( rejectedOutliers . size ( ) ) ) ;
}
else if ( rejectedCode = = DetectionInfo : : kRejectedByAngle )
{
label - > setText ( QString ( " Angle too small (%1 in %2 out) " ) . arg ( rejectedInliers . size ( ) ) . arg ( rejectedOutliers . size ( ) ) ) ;
}
2012-08-28 13:44:57 +00:00
}
2014-07-31 19:02:31 +00:00
}
2012-08-28 13:44:57 +00:00
2014-08-06 01:17:17 +00:00
if ( camera_ - > isRunning ( ) & & Settings : : getGeneral_autoPauseOnDetection ( ) & & info . objDetected_ . size ( ) )
{
this - > pauseProcessing ( ) ;
}
2014-07-31 19:02:31 +00:00
// Add homography rectangles when homographies are computed
2014-08-04 01:33:52 +00:00
QMultiMap < int , QMultiMap < int , int > > : : const_iterator inliersIter = info . objDetectedInliers_ . constBegin ( ) ;
QMultiMap < int , QMultiMap < int , int > > : : const_iterator outliersIter = info . objDetectedOutliers_ . constBegin ( ) ;
for ( QMultiMap < int , QTransform > : : iterator iter = info . objDetected_ . begin ( ) ;
iter ! = info . objDetected_ . end ( ) ;
+ + iter , + + inliersIter , + + outliersIter )
2014-07-31 19:02:31 +00:00
{
int id = iter . key ( ) ;
ObjWidget * obj = objWidgets_ . value ( id ) ;
Q_ASSERT ( obj ! = 0 ) ;
// COLORIZE (should be done in the GUI thread)
2014-08-04 01:33:52 +00:00
QTransform hTransform = iter . value ( ) ;
2014-07-31 19:02:31 +00:00
QRect rect = obj - > pixmap ( ) . rect ( ) ;
// add rectangle
2014-08-03 22:23:02 +00:00
QPen rectPen ( obj - > color ( ) ) ;
2014-07-31 19:02:31 +00:00
rectPen . setWidth ( Settings : : getHomography_rectBorderWidth ( ) ) ;
RectItem * rectItemScene = new RectItem ( id , rect ) ;
connect ( rectItemScene , SIGNAL ( hovered ( int ) ) , this , SLOT ( rectHovered ( int ) ) ) ;
rectItemScene - > setPen ( rectPen ) ;
rectItemScene - > setTransform ( hTransform ) ;
ui_ - > imageView_source - > addRect ( rectItemScene ) ;
QGraphicsRectItem * rectItemObj = new QGraphicsRectItem ( rect ) ;
rectItemObj - > setPen ( rectPen ) ;
obj - > addRect ( rectItemObj ) ;
for ( QMultiMap < int , int > : : const_iterator iter = inliersIter . value ( ) . constBegin ( ) ; iter ! = inliersIter . value ( ) . constEnd ( ) ; + + iter )
2012-08-28 13:44:57 +00:00
{
2014-08-03 22:23:02 +00:00
obj - > setKptColor ( iter . key ( ) , obj - > color ( ) ) ;
ui_ - > imageView_source - > setKptColor ( iter . value ( ) , obj - > color ( ) ) ;
2014-07-31 19:02:31 +00:00
}
2011-10-25 15:48:19 +00:00
2014-07-31 19:02:31 +00:00
QLabel * label = ui_ - > dockWidget_objects - > findChild < QLabel * > ( QString ( " %1detection " ) . arg ( id ) ) ;
2014-08-04 01:33:52 +00:00
if ( info . objDetected_ . count ( id ) > 1 )
2014-07-31 19:02:31 +00:00
{
// if a homography is already found, set the objects count
2014-08-04 01:33:52 +00:00
label - > setText ( QString ( " %1 objects found " ) . arg ( info . objDetected_ . count ( id ) ) ) ;
2014-03-21 22:26:06 +00:00
}
else
{
2014-07-31 19:02:31 +00:00
label - > setText ( QString ( " %1 in %2 out " ) . arg ( inliersIter . value ( ) . size ( ) ) . arg ( outliersIter . value ( ) . size ( ) ) ) ;
2014-03-21 22:26:06 +00:00
}
2014-07-31 19:02:31 +00:00
}
2012-08-28 13:44:57 +00:00
2014-07-31 19:02:31 +00:00
//update likelihood plot
likelihoodCurve_ - > setData ( scores , QMap < int , int > ( ) ) ;
QMap < int , int > inlierScores ;
for ( QMap < int , int > : : iterator iter = scores . begin ( ) ; iter ! = scores . end ( ) ; + + iter )
{
2014-08-04 01:33:52 +00:00
QList < QMultiMap < int , int > > values = info . objDetectedInliers_ . values ( iter . key ( ) ) ;
2014-07-31 19:02:31 +00:00
int maxValue = 0 ;
if ( values . size ( ) )
2014-03-21 22:26:06 +00:00
{
2014-07-31 19:02:31 +00:00
maxValue = values [ 0 ] . size ( ) ;
for ( int i = 1 ; i < values . size ( ) ; + + i )
2012-08-29 19:04:14 +00:00
{
2014-07-31 19:02:31 +00:00
if ( maxValue < values [ i ] . size ( ) )
2014-05-11 23:57:08 +00:00
{
2014-07-31 19:02:31 +00:00
maxValue = values [ i ] . size ( ) ;
2014-05-11 23:57:08 +00:00
}
2012-08-29 19:04:14 +00:00
}
2011-10-25 15:48:19 +00:00
}
2014-07-31 19:02:31 +00:00
inlierScores . insert ( iter . key ( ) , maxValue ) ;
}
inliersCurve_ - > setData ( inlierScores , QMap < int , int > ( ) ) ;
if ( ui_ - > likelihoodPlot - > isVisible ( ) )
{
ui_ - > likelihoodPlot - > update ( ) ;
}
2012-08-28 13:44:57 +00:00
2014-08-04 01:33:52 +00:00
ui_ - > label_minMatchedDistance - > setNum ( info . minMatchedDistance_ ) ;
ui_ - > label_maxMatchedDistance - > setNum ( info . maxMatchedDistance_ ) ;
2011-11-23 18:58:55 +00:00
2014-07-31 19:02:31 +00:00
//Scroll objects slider to the best score
if ( maxScoreId > = 0 & & Settings : : getGeneral_autoScroll ( ) )
{
QLabel * label = ui_ - > dockWidget_objects - > findChild < QLabel * > ( QString ( " %1title " ) . arg ( maxScoreId ) ) ;
if ( label )
2014-05-21 21:31:42 +00:00
{
2014-07-31 19:02:31 +00:00
ui_ - > objects_area - > verticalScrollBar ( ) - > setValue ( label - > pos ( ) . y ( ) ) ;
2014-05-21 21:31:42 +00:00
}
2014-07-31 19:02:31 +00:00
}
2014-05-22 17:47:00 +00:00
2014-07-31 19:02:31 +00:00
// Emit homographies
2014-08-04 01:33:52 +00:00
if ( info . objDetected_ . size ( ) > 1 )
2014-07-31 19:02:31 +00:00
{
UINFO ( " (%s) %d objects detected! " ,
QTime : : currentTime ( ) . toString ( " HH:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ,
2014-08-04 01:33:52 +00:00
( int ) info . objDetected_ . size ( ) ) ;
2011-11-23 16:44:14 +00:00
}
2014-08-04 01:33:52 +00:00
else if ( info . objDetected_ . size ( ) = = 1 )
2011-11-23 16:44:14 +00:00
{
2014-07-31 19:02:31 +00:00
UINFO ( " (%s) Object %d detected! " ,
QTime : : currentTime ( ) . toString ( " HH:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ,
2014-08-04 01:33:52 +00:00
( int ) info . objDetected_ . begin ( ) . key ( ) ) ;
2014-07-31 19:02:31 +00:00
}
else if ( Settings : : getGeneral_sendNoObjDetectedEvents ( ) )
{
UINFO ( " (%s) No objects detected. " ,
QTime : : currentTime ( ) . toString ( " HH:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2011-11-23 16:44:14 +00:00
}
2011-10-25 15:48:19 +00:00
2014-08-04 01:33:52 +00:00
if ( info . objDetected_ . size ( ) > 0 | | Settings : : getGeneral_sendNoObjDetectedEvents ( ) )
2011-11-23 16:44:14 +00:00
{
2014-08-04 01:33:52 +00:00
Q_EMIT objectsFound ( info ) ;
2011-10-25 15:48:19 +00:00
}
2014-08-04 01:33:52 +00:00
ui_ - > label_objectsDetected - > setNum ( info . objDetected_ . size ( ) ) ;
2014-07-31 19:02:31 +00:00
}
else
{
2014-08-22 22:36:09 +00:00
if ( findObject_ - > vocabulary ( ) - > size ( ) )
{
this - > statusBar ( ) - > showMessage ( tr ( " Cannot search, objects must be updated! " ) ) ;
}
ui_ - > label_timeDetection - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeKeypointDetection , 0 ) ) ;
ui_ - > label_timeSkewAffine - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeSkewAffine , 0 ) ) ;
ui_ - > label_timeExtraction - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeDescriptorExtraction , 0 ) ) ;
2014-08-04 01:33:52 +00:00
ui_ - > imageView_source - > setData ( info . sceneKeypoints_ , cvtCvMat2QImage ( sceneImage_ ) ) ;
2014-07-31 19:02:31 +00:00
}
2011-11-23 16:44:14 +00:00
2014-07-31 19:02:31 +00:00
//Update object pictures
for ( QMap < int , ObjWidget * > : : iterator iter = objWidgets_ . begin ( ) ; iter ! = objWidgets_ . end ( ) ; + + iter )
{
iter . value ( ) - > update ( ) ;
2011-10-25 15:48:19 +00:00
}
2014-07-31 19:02:31 +00:00
2014-08-04 01:33:52 +00:00
ui_ - > label_nfeatures - > setNum ( ( int ) info . sceneKeypoints_ . size ( ) ) ;
2014-07-31 19:02:31 +00:00
ui_ - > imageView_source - > update ( ) ;
2011-10-25 15:48:19 +00:00
ui_ - > label_detectorDescriptorType - > setText ( QString ( " %1/%2 " ) . arg ( Settings : : currentDetectorType ( ) ) . arg ( Settings : : currentDescriptorType ( ) ) ) ;
2011-11-23 19:12:41 +00:00
2012-08-28 20:06:43 +00:00
//update slider
if ( ui_ - > horizontalSlider_frames - > isEnabled ( ) )
{
ui_ - > horizontalSlider_frames - > blockSignals ( true ) ;
ui_ - > horizontalSlider_frames - > setValue ( camera_ - > getCurrentFrameIndex ( ) - 1 ) ;
ui_ - > label_frame - > setNum ( camera_ - > getCurrentFrameIndex ( ) - 1 ) ;
ui_ - > horizontalSlider_frames - > blockSignals ( false ) ;
}
2014-08-04 01:33:52 +00:00
ui_ - > label_timeTotal - > setNum ( info . timeStamps_ . value ( DetectionInfo : : kTimeTotal , 0 ) ) ;
2012-08-28 20:06:43 +00:00
int refreshRate = qRound ( 1000.0f / float ( updateRate_ . restart ( ) ) ) ;
2011-11-23 19:12:41 +00:00
if ( refreshRate > 0 & & refreshRate < lowestRefreshRate_ )
{
lowestRefreshRate_ = refreshRate ;
}
// Refresh the label only after each 1000 ms
if ( refreshStartTime_ . elapsed ( ) > 1000 )
{
2012-08-28 20:06:43 +00:00
if ( Settings : : getCamera_4imageRate ( ) > 0.0 )
2011-11-23 22:12:44 +00:00
{
2012-08-28 13:44:57 +00:00
ui_ - > label_timeRefreshRate - > setText ( QString ( " (%1 Hz - %2 Hz) " ) . arg ( QString : : number ( Settings : : getCamera_4imageRate ( ) ) ) . arg ( QString : : number ( lowestRefreshRate_ ) ) ) ;
2011-11-23 22:12:44 +00:00
}
else
{
ui_ - > label_timeRefreshRate - > setText ( QString ( " (%2 Hz) " ) . arg ( QString : : number ( lowestRefreshRate_ ) ) ) ;
}
2011-11-23 19:12:41 +00:00
lowestRefreshRate_ = 99 ;
refreshStartTime_ . start ( ) ;
}
2011-10-25 15:48:19 +00:00
}
2012-01-28 01:00:23 +00:00
2012-08-29 19:04:14 +00:00
void MainWindow : : notifyParametersChanged ( const QStringList & paramChanged )
2012-01-28 01:00:23 +00:00
{
2012-08-29 19:04:14 +00:00
//Selective update (to not update all objects for a simple camera's parameter modification)
bool detectorDescriptorParamsChanged = false ;
bool nearestNeighborParamsChanged = false ;
2014-08-03 21:35:28 +00:00
bool parameterChanged = false ;
2012-08-29 19:04:14 +00:00
for ( QStringList : : const_iterator iter = paramChanged . begin ( ) ; iter ! = paramChanged . end ( ) ; + + iter )
{
2014-05-20 15:52:25 +00:00
if ( lastObjectsUpdateParameters_ . value ( * iter ) ! = Settings : : getParameter ( * iter ) )
2012-08-29 19:04:14 +00:00
{
2014-08-03 21:35:28 +00:00
lastObjectsUpdateParameters_ [ * iter ] = Settings : : getParameter ( * iter ) ;
parameterChanged = true ;
UINFO ( " Parameter changed: %s -> \" %s \" " , iter - > toStdString ( ) . c_str ( ) , Settings : : getParameter ( * iter ) . toString ( ) . toStdString ( ) . c_str ( ) ) ;
if ( iter - > contains ( " Feature2D " ) )
2014-05-20 15:52:25 +00:00
{
detectorDescriptorParamsChanged = true ;
}
2014-08-03 21:35:28 +00:00
else if ( ( iter - > contains ( " NearestNeighbor " ) & & Settings : : getGeneral_invertedSearch ( ) ) | |
2014-05-20 15:52:25 +00:00
iter - > compare ( Settings : : kGeneral_invertedSearch ( ) ) = = 0 | |
( iter - > compare ( Settings : : kGeneral_vocabularyIncremental ( ) ) = = 0 & & Settings : : getGeneral_invertedSearch ( ) ) | |
2014-08-03 21:35:28 +00:00
( iter - > compare ( Settings : : kGeneral_threads ( ) ) = = 0 & & ! Settings : : getGeneral_invertedSearch ( ) ) )
2014-05-20 15:52:25 +00:00
{
nearestNeighborParamsChanged = true ;
}
2014-05-05 23:08:01 +00:00
2014-08-03 21:35:28 +00:00
if ( iter - > compare ( Settings : : kGeneral_port ( ) ) = = 0 & &
Settings : : getGeneral_port ( ) ! = ui_ - > label_port - > text ( ) . toInt ( ) & &
Settings : : getGeneral_port ( ) ! = 0 )
{
setupTCPServer ( ) ;
}
2014-05-05 23:08:01 +00:00
}
2012-08-29 19:04:14 +00:00
}
2014-06-19 17:43:34 +00:00
if ( detectorDescriptorParamsChanged )
{
//Re-init detector and extractor
2014-07-31 19:02:31 +00:00
findObject_ - > updateDetectorExtractor ( ) ;
2014-06-19 17:43:34 +00:00
}
2012-02-05 00:30:52 +00:00
if ( Settings : : getGeneral_autoUpdateObjects ( ) )
{
2012-08-29 19:04:14 +00:00
if ( detectorDescriptorParamsChanged )
{
this - > updateObjects ( ) ;
}
else if ( nearestNeighborParamsChanged )
{
2014-07-31 19:02:31 +00:00
this - > updateVocabulary ( ) ;
2012-08-29 19:04:14 +00:00
}
2012-02-05 00:30:52 +00:00
}
2014-07-31 19:02:31 +00:00
else if ( objWidgets_ . size ( ) & & ( detectorDescriptorParamsChanged | | nearestNeighborParamsChanged ) )
2012-01-28 01:00:23 +00:00
{
this - > statusBar ( ) - > showMessage ( tr ( " A parameter has changed... \" Update objects \" may be required. " ) ) ;
}
2014-08-03 21:35:28 +00:00
if ( parameterChanged & & ! camera_ - > isRunning ( ) & & ! sceneImage_ . empty ( ) )
2012-01-30 00:54:23 +00:00
{
2014-08-03 21:35:28 +00:00
this - > update ( sceneImage_ ) ;
2012-01-30 00:54:23 +00:00
ui_ - > label_timeRefreshRate - > setVisible ( false ) ;
}
2012-01-30 03:16:30 +00:00
2014-08-04 01:33:52 +00:00
ui_ - > actionCamera_from_video_file - > setChecked ( ! Settings : : getCamera_5mediaPath ( ) . isEmpty ( ) & & ! UDirectory : : exists ( Settings : : getCamera_5mediaPath ( ) . toStdString ( ) ) & & ! Settings : : getCamera_6useTcpCamera ( ) ) ;
ui_ - > actionCamera_from_directory_of_images - > setChecked ( ! Settings : : getCamera_5mediaPath ( ) . isEmpty ( ) & & UDirectory : : exists ( Settings : : getCamera_5mediaPath ( ) . toStdString ( ) ) & & ! Settings : : getCamera_6useTcpCamera ( ) ) ;
2014-05-21 21:31:42 +00:00
ui_ - > actionCamera_from_TCP_IP - > setChecked ( Settings : : getCamera_6useTcpCamera ( ) ) ;
2012-01-28 01:00:23 +00:00
}
2014-08-06 13:43:29 +00:00
} // namespace find_object