Added DetectionInfo object for easier serialization/deserialization of the detection results.

Added filename property in json files
Added JsonWritter class for convenience

git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@369 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe
2014-08-04 01:33:52 +00:00
parent cd8368f0de
commit e6fcc961b8
24 changed files with 486 additions and 426 deletions
-12
View File
@@ -12,18 +12,6 @@ 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})
+56 -118
View File
@@ -6,12 +6,9 @@
#include "find_object/FindObject.h"
#include "find_object/Camera.h"
#include "find_object/TcpServer.h"
#include "find_object/JsonWriter.h"
#include "find_object/utilite/ULogger.h"
#ifdef WITH_JSONCPP
#include <jsoncpp/json/writer.h>
#endif
bool running = true;
#ifdef WIN32
@@ -76,89 +73,12 @@ void showUsage()
" --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())
if(JsonWriter::available())
{
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());
printf(" --json \"path\" Path to an output JSON file (only in --console mode with --scene).\n");
}
#else
UERROR("Not built with JSON support!");
#endif
exit(-1);
}
int main(int argc, char* argv[])
@@ -249,26 +169,6 @@ int main(int argc, char* argv[])
}
continue;
}
#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)
{
@@ -280,6 +180,27 @@ int main(int argc, char* argv[])
{
showUsage();
}
if(JsonWriter::available())
{
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;
}
}
UERROR("Unrecognized option : %s", argv[i]);
showUsage();
@@ -290,9 +211,10 @@ int main(int argc, char* argv[])
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\"", jsonPath.toStdString().c_str());
#endif
if(JsonWriter::available())
{
UINFO(" JSON path: \"%s\"", jsonPath.toStdString().c_str());
}
//////////////////////////
// parse options END
@@ -353,26 +275,42 @@ int main(int argc, char* argv[])
QCoreApplication app(argc, argv);
TcpServer tcpServer(Settings::getGeneral_port());
UINFO("Detection sent on port: %d (IP=%s)", tcpServer.getPort(), tcpServer.getHostAddress().toString().toStdString().c_str());
// connect stuff:
// [FindObject] ---ObjectsDetected---> [TcpServer]
QObject::connect(findObject, SIGNAL(objectsFound(QMultiMap<int,QPair<QRect,QTransform> >)), &tcpServer, SLOT(publishObjects(QMultiMap<int,QPair<QRect,QTransform> >)));
if(!scene.empty())
{
// process the scene and exit
findObject->detect(scene); // this will automatically emit objectsFound()
QTime time;
time.start();
DetectionInfo info;
findObject->detect(scene, info);
if(!jsonPath.isEmpty())
if(info.objDetected_.size() > 1)
{
writeJSON(*findObject, jsonPath);
UINFO("%d objects detected! (%d ms)", (int)info.objDetected_.size(), time.elapsed());
}
else if(info.objDetected_.size() == 1)
{
UINFO("Object %d detected! (%d ms)", (int)info.objDetected_.begin().key(), time.elapsed());
}
else if(Settings::getGeneral_sendNoObjDetectedEvents())
{
UINFO("No objects detected. (%d ms)", time.elapsed());
}
if(!jsonPath.isEmpty() && JsonWriter::available())
{
JsonWriter::write(info, jsonPath);
UINFO("JSON written to \"%s\"", jsonPath.toStdString().c_str());
}
}
else
{
Camera camera;
TcpServer tcpServer(Settings::getGeneral_port());
UINFO("Detection sent on port: %d (IP=%s)", tcpServer.getPort(), tcpServer.getHostAddress().toString().toStdString().c_str());
// connect stuff:
// [FindObject] ---ObjectsDetected---> [TcpServer]
QObject::connect(findObject, SIGNAL(objectsFound(DetectionInfo)), &tcpServer, SLOT(publishDetectionInfo(DetectionInfo)));
// [Camera] ---Image---> [FindObject]
QObject::connect(&camera, SIGNAL(imageReceived(const cv::Mat &)), findObject, SLOT(detect(const cv::Mat &)));
@@ -394,9 +332,9 @@ int main(int argc, char* argv[])
// cleanup
camera.stop();
tcpServer.close();
}
delete findObject;
tcpServer.close();
}
}