Added JSON optional output when in console mode (see --json option)

Added --scene option to process a single scene file
Moved some apps in tools subfolder


git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@362 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe
2014-08-01 21:11:26 +00:00
parent 43e855e822
commit 6a0136cc16
18 changed files with 482 additions and 56 deletions
+12
View File
@@ -12,6 +12,18 @@ SET(LIBRARIES
${OpenCV_LIBS}
)
IF(JSONCPP_FOUND)
SET(INCLUDE_DIRS
${INCLUDE_DIRS}
${JSONCPP_INCLUDE_DIRS}
)
SET(LIBRARIES
${LIBRARIES}
${JSONCPP_LIBRARIES}
)
ADD_DEFINITIONS("-DWITH_JSONCPP")
ENDIF(JSONCPP_FOUND)
#include files
INCLUDE_DIRECTORIES(${INCLUDE_DIRS})
+207 -33
View File
@@ -8,6 +8,10 @@
#include "find_object/TcpServer.h"
#include "find_object/utilite/ULogger.h"
#ifdef WITH_JSONCPP
#include <jsoncpp/json/writer.h>
#endif
bool running = true;
#ifdef WIN32
@@ -66,15 +70,97 @@ void showUsage()
" find_object [options]\n"
#endif
"Options:\n"
" -console Don't use the GUI (by default the camera will be\n"
" started automatically). Option -objs must also be\n"
" --console Don't use the GUI (by default the camera will be\n"
" started automatically). Option --objects must also be\n"
" used with valid objects.\n"
" -objs \"path\" Directory of the objects to detect.\n"
" -config \"path\" Path to configuration file (default: %s).\n"
" -help or --help Show usage.\n", Settings::iniDefaultPath().toStdString().c_str());
" --objects \"path\" Directory of the objects to detect.\n"
" --config \"path\" Path to configuration file (default: %s).\n"
" --scene \"path\" Path to a scene image file.\n"
#ifdef WITH_JSONCPP
" --json \"path\" Path to an output JSON file (only in --console mode with --scene).\n"
#endif
" --help Show usage.\n", Settings::iniDefaultPath().toStdString().c_str());
exit(-1);
}
void writeJSON(const FindObject & findObject, const QString & path)
{
#ifdef WITH_JSONCPP
if(!path.isEmpty())
{
Json::Value root;
Json::Value detections;
Json::Value matchesValues;
if(findObject.objectsDetected().size())
{
Q_ASSERT(objectsDetected.size() == findObject.inliers().size() &&
objectsDetected.size() == findObject.outliers().size());
const QMultiMap<int,QPair<QRect,QTransform> > & objectsDetected = findObject.objectsDetected();
QMultiMap<int, QMultiMap<int, int> >::const_iterator iterInliers = findObject.inliers().constBegin();
QMultiMap<int, QMultiMap<int, int> >::const_iterator iterOutliers = findObject.outliers().constBegin();
for(QMultiMap<int,QPair<QRect,QTransform> >::const_iterator iter = objectsDetected.constBegin();
iter!= objectsDetected.end();)
{
char index = 'a';
QMultiMap<int,QPair<QRect,QTransform> >::const_iterator jter = iter;
for(;jter != objectsDetected.constEnd() && jter.key() == iter.key(); ++jter)
{
QString name = QString("object_%1%2").arg(jter.key()).arg(objectsDetected.count(jter.key())>1?QString(index++):"");
detections.append(name.toStdString());
Json::Value homography;
homography.append(jter.value().second.m11());
homography.append(jter.value().second.m12());
homography.append(jter.value().second.m13());
homography.append(jter.value().second.m21());
homography.append(jter.value().second.m22());
homography.append(jter.value().second.m23());
homography.append(jter.value().second.m31()); // dx
homography.append(jter.value().second.m32()); // dy
homography.append(jter.value().second.m33());
root[name.toStdString()]["width"] = jter.value().first.width();
root[name.toStdString()]["height"] = jter.value().first.height();
root[name.toStdString()]["homography"] = homography;
root[name.toStdString()]["inliers"] = iterInliers.value().size();
root[name.toStdString()]["outliers"] = iterOutliers.value().size();
++iterInliers;
++iterOutliers;
}
iter = jter;
}
}
const QMap<int, QMultiMap<int, int> > & matches = findObject.matches();
for(QMap<int, QMultiMap<int, int> >::const_iterator iter = matches.constBegin();
iter != matches.end();
++iter)
{
QString name = QString("matches_%1").arg(iter.key());
root[name.toStdString()] = iter.value().size();
matchesValues.append(name.toStdString());
}
root["objects"] = detections;
root["matches"] = matchesValues;
// write in a nice readible way
Json::StyledWriter styledWriter;
//std::cout << styledWriter.write(root);
QFile file(path);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << styledWriter.write(root).c_str();
file.close();
UINFO("JSON written to \"%s\"", path.toStdString().c_str());
}
#else
UERROR("Not built with JSON support!");
#endif
}
int main(int argc, char* argv[])
{
ULogger::setType(ULogger::kTypeConsole);
@@ -87,11 +173,16 @@ int main(int argc, char* argv[])
//////////////////////////
bool guiMode = true;
QString objectsPath = "";
QString scenePath = "";
QString configPath = Settings::iniDefaultPath();
QString jsonPath;
for(int i=1; i<argc; ++i)
{
if(strcmp(argv[i], "-objs") == 0)
if(strcmp(argv[i], "-objs") == 0 ||
strcmp(argv[i], "--objs") == 0 ||
strcmp(argv[i], "-objects") == 0 ||
strcmp(argv[i], "--objects") == 0)
{
++i;
if(i < argc)
@@ -103,7 +194,7 @@ int main(int argc, char* argv[])
}
if(!QDir(objectsPath).exists())
{
UERROR("Path not valid : %s", objectsPath.toStdString().c_str());
UERROR("Objects path not valid : %s", objectsPath.toStdString().c_str());
showUsage();
}
}
@@ -113,7 +204,31 @@ int main(int argc, char* argv[])
}
continue;
}
if(strcmp(argv[i], "-config") == 0)
if(strcmp(argv[i], "-scene") == 0 ||
strcmp(argv[i], "--scene") == 0)
{
++i;
if(i < argc)
{
scenePath = argv[i];
if(scenePath.contains('~'))
{
scenePath.replace('~', QDir::homePath());
}
if(!QFile(scenePath).exists())
{
UERROR("Scene path not valid : %s", scenePath.toStdString().c_str());
showUsage();
}
}
else
{
showUsage();
}
continue;
}
if(strcmp(argv[i], "-config") == 0 ||
strcmp(argv[i], "--config") == 0)
{
++i;
if(i < argc)
@@ -135,12 +250,34 @@ int main(int argc, char* argv[])
}
continue;
}
if(strcmp(argv[i], "-console") == 0)
#ifdef WITH_JSONCPP
if(strcmp(argv[i], "-json") == 0 ||
strcmp(argv[i], "--json") == 0)
{
++i;
if(i < argc)
{
jsonPath = argv[i];
if(jsonPath.contains('~'))
{
jsonPath.replace('~', QDir::homePath());
}
}
else
{
showUsage();
}
continue;
}
#endif
if(strcmp(argv[i], "-console") == 0 ||
strcmp(argv[i], "--console") == 0)
{
guiMode = false;
continue;
}
if(strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0)
if(strcmp(argv[i], "-help") == 0 ||
strcmp(argv[i], "--help") == 0)
{
showUsage();
}
@@ -152,7 +289,11 @@ int main(int argc, char* argv[])
UINFO("Options:");
UINFO(" GUI mode = %s", guiMode?"true":"false");
UINFO(" Objects path: \"%s\"", objectsPath.toStdString().c_str());
UINFO(" Scene path: \"%s\"", scenePath.toStdString().c_str());
UINFO(" Settings path: \"%s\"", configPath.toStdString().c_str());
#ifdef WITH_JSONCPP
UINFO(" JSON path: \"%s\"", configPath.toStdString().c_str());
#endif
//////////////////////////
// parse options END
@@ -174,6 +315,15 @@ int main(int argc, char* argv[])
UWARN("No objects loaded from \"%s\"", objectsPath.toStdString().c_str());
}
}
cv::Mat scene;
if(!scenePath.isEmpty())
{
scene = cv::imread(scenePath.toStdString());
if(scene.empty())
{
UERROR("Failed to load scene \"%s\"", scenePath.toStdString().c_str());
}
}
if(guiMode)
{
@@ -183,7 +333,15 @@ int main(int argc, char* argv[])
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
mainWindow.show();
if(!scene.empty())
{
mainWindow.update(scene);
}
app.exec();
// Save settings
Settings::saveSettings();
}
else
{
@@ -195,43 +353,59 @@ int main(int argc, char* argv[])
}
QCoreApplication app(argc, argv);
Camera camera;
TcpServer tcpServer(Settings::getGeneral_port());
printf("IP: %s\nport: %d\n", tcpServer.getHostAddress().toString().toStdString().c_str(), tcpServer.getPort());
// connect stuff:
// [Camera] ---Image---> [FindObject] ---ObjectsDetected---> [TcpServer]
QObject::connect(&camera, SIGNAL(imageReceived(const cv::Mat &)), findObject, SLOT(detect(const cv::Mat &)));
// [FindObject] ---ObjectsDetected---> [TcpServer]
QObject::connect(findObject, SIGNAL(objectsFound(QMultiMap<int,QPair<QRect,QTransform> >)), &tcpServer, SLOT(publishObjects(QMultiMap<int,QPair<QRect,QTransform> >)));
setupQuitSignal();
// start processing!
while(running && !camera.start())
if(!scene.empty())
{
if(Settings::getCamera_6useTcpCamera())
// process the scene and exit
findObject->detect(scene); // this will automatically emit objectsFound()
if(!jsonPath.isEmpty())
{
UWARN("Camera initialization failed! (with server %s:%d) Trying again in 1 second...",
Settings::getCamera_7IP().toStdString().c_str(), Settings::getCamera_8port());
Sleep(1000);
}
else
{
UERROR("Camera initialization failed!");
running = false;
writeJSON(*findObject, jsonPath);
}
}
if(running)
else
{
app.exec();
Camera camera;
// [Camera] ---Image---> [FindObject]
QObject::connect(&camera, SIGNAL(imageReceived(const cv::Mat &)), findObject, SLOT(detect(const cv::Mat &)));
//use camera in settings
setupQuitSignal();
// start processing!
while(running && !camera.start())
{
if(Settings::getCamera_6useTcpCamera())
{
UWARN("Camera initialization failed! (with server %s:%d) Trying again in 1 second...",
Settings::getCamera_7IP().toStdString().c_str(), Settings::getCamera_8port());
Sleep(1000);
}
else
{
UERROR("Camera initialization failed!");
running = false;
}
}
if(running)
{
app.exec();
}
// cleanup
camera.stop();
}
// cleanup
camera.stop();
delete findObject;
tcpServer.close();
}
// Save settings
Settings::saveSettings();
}