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:
@@ -1,4 +1,4 @@
|
||||
ADD_SUBDIRECTORY( tcpObjectsClient )
|
||||
ADD_SUBDIRECTORY( tcpClient )
|
||||
ADD_SUBDIRECTORY( tcpImagesServer )
|
||||
ADD_SUBDIRECTORY( tcpRequest )
|
||||
ADD_SUBDIRECTORY( similarity )
|
||||
@@ -25,18 +25,6 @@ SET(LIBRARIES
|
||||
${QT_LIBRARIES}
|
||||
)
|
||||
|
||||
IF(JSONCPP_FOUND)
|
||||
SET(INCLUDE_DIRS
|
||||
${INCLUDE_DIRS}
|
||||
${JSONCPP_INCLUDE_DIRS}
|
||||
)
|
||||
SET(LIBRARIES
|
||||
${LIBRARIES}
|
||||
${JSONCPP_LIBRARIES}
|
||||
)
|
||||
ADD_DEFINITIONS("-DWITH_JSONCPP")
|
||||
ENDIF(JSONCPP_FOUND)
|
||||
|
||||
# Make sure the compiler can find include files from our library.
|
||||
INCLUDE_DIRECTORIES(${INCLUDE_DIRS})
|
||||
|
||||
|
||||
@@ -44,24 +44,7 @@ void TcpResponse::readReceivedData()
|
||||
|
||||
blockSize_ = 0;
|
||||
|
||||
QVector<float> data;
|
||||
in >> data;
|
||||
|
||||
objectsDetected_.clear();
|
||||
for(int i=0; i<data.size(); i+=12)
|
||||
{
|
||||
// get data
|
||||
int id = (int)data[i];
|
||||
float width = data[i+1];
|
||||
float height = data[i+2];
|
||||
|
||||
// Find corners Qt
|
||||
QTransform homography(data[i+3], data[i+4], data[i+5],
|
||||
data[i+6], data[i+7], data[i+8],
|
||||
data[i+9], data[i+10], data[i+11]);
|
||||
|
||||
objectsDetected_.insert(id, QPair<QRect, QTransform>(QRect(0,0,width, height), homography));
|
||||
}
|
||||
in >> info_;
|
||||
|
||||
dataReceived_ = true;
|
||||
Q_EMIT detectionReceived();
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#ifndef TCPRESPONSE_H_
|
||||
#define TCPRESPONSE_H_
|
||||
|
||||
#include "find_object/DetectionInfo.h"
|
||||
|
||||
#include <QtNetwork/QTcpSocket>
|
||||
#include <QtCore/QMultiMap>
|
||||
#include <QtGui/QTransform>
|
||||
@@ -18,7 +20,7 @@ class TcpResponse : public QTcpSocket
|
||||
Q_OBJECT;
|
||||
public:
|
||||
TcpResponse(QObject * parent = 0);
|
||||
const QMultiMap<int, QPair<QRect, QTransform> > & objectsDetected() const {return objectsDetected_;}
|
||||
const DetectionInfo & info() const {return info_;}
|
||||
bool dataReceived() const {return dataReceived_;}
|
||||
|
||||
private Q_SLOTS:
|
||||
@@ -31,7 +33,7 @@ Q_SIGNALS:
|
||||
|
||||
private:
|
||||
quint16 blockSize_;
|
||||
QMultiMap<int, QPair<QRect, QTransform> > objectsDetected_;
|
||||
DetectionInfo info_;
|
||||
bool dataReceived_;
|
||||
};
|
||||
|
||||
|
||||
+29
-82
@@ -11,10 +11,7 @@
|
||||
#include <QtCore/QTime>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "TcpResponse.h"
|
||||
|
||||
#ifdef WITH_JSONCPP
|
||||
#include <jsoncpp/json/writer.h>
|
||||
#endif
|
||||
#include "find_object/JsonWriter.h"
|
||||
|
||||
void showUsage()
|
||||
{
|
||||
@@ -22,67 +19,12 @@ void showUsage()
|
||||
" \"out\" is the port to which the image is sent.\n"
|
||||
" \"in\" is the port from which the detection is received.\n"
|
||||
" Options:\n"
|
||||
" --host #.#.#.# Set host address.\n"
|
||||
#ifdef WITH_JSONCPP
|
||||
" --json \"path\" Path to an output JSON file.\n"
|
||||
#endif
|
||||
);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
void writeJSON(const QMultiMap<int, QPair<QRect, QTransform> > & objectsDetected, const QString & path)
|
||||
{
|
||||
#ifdef WITH_JSONCPP
|
||||
if(!path.isEmpty())
|
||||
" --host #.#.#.# Set host address.\n");
|
||||
if(JsonWriter::available())
|
||||
{
|
||||
Json::Value root;
|
||||
Json::Value detections;
|
||||
|
||||
if(objectsDetected.size())
|
||||
{
|
||||
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;
|
||||
}
|
||||
iter = jter;
|
||||
}
|
||||
}
|
||||
|
||||
root["objects"] = detections;
|
||||
|
||||
// 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();
|
||||
printf("JSON written to \"%s\"\n", path.toStdString().c_str());
|
||||
printf(" --json \"path\" Path to an output JSON file.\n");
|
||||
}
|
||||
#else
|
||||
printf("Not built with JSON support!\n");
|
||||
#endif
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
@@ -123,20 +65,6 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(strcmp(argv[i], "--json") == 0 || strcmp(argv[i], "-json") == 0)
|
||||
{
|
||||
++i;
|
||||
if(i < argc)
|
||||
{
|
||||
jsonPath = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("error parsing --json\n");
|
||||
showUsage();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(strcmp(argv[i], "--out") == 0 || strcmp(argv[i], "-out") == 0)
|
||||
{
|
||||
++i;
|
||||
@@ -166,6 +94,24 @@ int main(int argc, char * argv[])
|
||||
continue;
|
||||
}
|
||||
|
||||
if(JsonWriter::available())
|
||||
{
|
||||
if(strcmp(argv[i], "--json") == 0 || strcmp(argv[i], "-json") == 0)
|
||||
{
|
||||
++i;
|
||||
if(i < argc)
|
||||
{
|
||||
jsonPath = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("error parsing --json\n");
|
||||
showUsage();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Unrecognized option: %s\n", argv[i]);
|
||||
showUsage();
|
||||
}
|
||||
@@ -242,12 +188,12 @@ int main(int argc, char * argv[])
|
||||
{
|
||||
printf("Response received! (%d ms)\n", time.elapsed());
|
||||
// print detected objects
|
||||
if(response.objectsDetected().size())
|
||||
if(response.info().objDetected_.size())
|
||||
{
|
||||
QList<int> ids = response.objectsDetected().uniqueKeys();
|
||||
QList<int> ids = response.info().objDetected_.uniqueKeys();
|
||||
for(int i=0; i<ids.size(); ++i)
|
||||
{
|
||||
int count = response.objectsDetected().count(ids[i]);
|
||||
int count = response.info().objDetected_.count(ids[i]);
|
||||
if(count == 1)
|
||||
{
|
||||
printf("Object %d detected.\n", ids[i]);
|
||||
@@ -263,9 +209,10 @@ int main(int argc, char * argv[])
|
||||
printf("No objects detected.\n");
|
||||
}
|
||||
// write json
|
||||
if(!jsonPath.isEmpty())
|
||||
if(!jsonPath.isEmpty() && JsonWriter::available())
|
||||
{
|
||||
writeJSON(response.objectsDetected(), jsonPath);
|
||||
JsonWriter::write(response.info(), jsonPath);
|
||||
printf("JSON written to \"%s\"\n", jsonPath.toStdString().c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user