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
+3
View File
@@ -0,0 +1,3 @@
ADD_SUBDIRECTORY( tcpObjectsClient )
ADD_SUBDIRECTORY( tcpImagesServer )
ADD_SUBDIRECTORY( similarity )
+28
View File
@@ -0,0 +1,28 @@
SET(SRC_FILES
main.cpp
)
SET(INCLUDE_DIRS
${OpenCV_INCLUDE_DIRS}
)
SET(LIBRARIES
${OpenCV_LIBS}
)
# Make sure the compiler can find include files from our library.
INCLUDE_DIRECTORIES(${INCLUDE_DIRS})
# Add binary called "similarity" that is built from the source file "main.cpp".
# The extension is automatically found.
ADD_EXECUTABLE(similarity ${SRC_FILES})
TARGET_LINK_LIBRARIES(similarity ${LIBRARIES})
SET_TARGET_PROPERTIES( similarity
PROPERTIES OUTPUT_NAME ${PROJECT_PREFIX}-similarity)
INSTALL(TARGETS similarity
RUNTIME DESTINATION bin COMPONENT runtime
BUNDLE DESTINATION "${CMAKE_BUNDLE_LOCATION}" COMPONENT runtime)
+164
View File
@@ -0,0 +1,164 @@
#include <stdio.h>
#include <stdlib.h>
// OpenCV stuff
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp> // for homography
void showUsage()
{
printf(
"\n"
"Return similarity between two images (the number of similar features between the images).\n"
"Usage :\n"
" ./find_object-similarity [option] object.png scene.png\n"
"Options: \n"
" -inliers return inliers percentage : inliers / (inliers + outliers)\n"
" -quiet don't show messages\n");
exit(-1);
}
enum {mTotal, mInliers};
int main(int argc, char * argv[])
{
bool quiet = false;
int method = mTotal; //total matches
if(argc<3)
{
printf("Two images required!\n");
showUsage();
}
else if(argc>3)
{
for(int i=1; i<argc-2; ++i)
{
if(std::string(argv[i]).compare("-inliers") == 0)
{
method = mInliers;
}
else if(std::string(argv[i]).compare("-quiet") == 0)
{
quiet = true;
}
else
{
printf("Option %s not recognized!", argv[1]);
showUsage();
}
}
}
//Load as grayscale
cv::Mat objectImg = cv::imread(argv[argc-2], cv::IMREAD_GRAYSCALE);
cv::Mat sceneImg = cv::imread(argv[argc-1], cv::IMREAD_GRAYSCALE);
int value = 0;
if(!objectImg.empty() && !sceneImg.empty())
{
std::vector<cv::KeyPoint> objectKeypoints;
std::vector<cv::KeyPoint> sceneKeypoints;
cv::Mat objectDescriptors;
cv::Mat sceneDescriptors;
////////////////////////////
// EXTRACT KEYPOINTS
////////////////////////////
cv::SIFT sift;
sift.detect(objectImg, objectKeypoints);
sift.detect(sceneImg, sceneKeypoints);
////////////////////////////
// EXTRACT DESCRIPTORS
////////////////////////////
sift.compute(objectImg, objectKeypoints, objectDescriptors);
sift.compute(sceneImg, sceneKeypoints, sceneDescriptors);
////////////////////////////
// NEAREST NEIGHBOR MATCHING USING FLANN LIBRARY (included in OpenCV)
////////////////////////////
cv::Mat results;
cv::Mat dists;
std::vector<std::vector<cv::DMatch> > matches;
int k=2; // find the 2 nearest neighbors
// Create Flann KDTree index
cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN);
results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index
dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1
// search (nearest neighbor)
flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() );
////////////////////////////
// PROCESS NEAREST NEIGHBOR RESULTS
////////////////////////////
// Find correspondences by NNDR (Nearest Neighbor Distance Ratio)
float nndrRatio = 0.6;
std::vector<cv::Point2f> mpts_1, mpts_2; // Used for homography
std::vector<int> indexes_1, indexes_2; // Used for homography
std::vector<uchar> outlier_mask; // Used for homography
// Check if this descriptor matches with those of the objects
for(int i=0; i<objectDescriptors.rows; ++i)
{
// Apply NNDR
if(dists.at<float>(i,0) <= nndrRatio * dists.at<float>(i,1))
{
mpts_1.push_back(objectKeypoints.at(i).pt);
indexes_1.push_back(i);
mpts_2.push_back(sceneKeypoints.at(results.at<int>(i,0)).pt);
indexes_2.push_back(results.at<int>(i,0));
}
}
if(method == mInliers)
{
// FIND HOMOGRAPHY
unsigned int minInliers = 8;
if(mpts_1.size() >= minInliers)
{
cv::Mat H = findHomography(mpts_1,
mpts_2,
cv::RANSAC,
1.0,
outlier_mask);
int inliers=0, outliers=0;
for(unsigned int k=0; k<mpts_1.size();++k)
{
if(outlier_mask.at(k))
{
++inliers;
}
else
{
++outliers;
}
}
if(!quiet)
printf("Total=%d Inliers=%d Outliers=%d\n", (int)mpts_1.size(), inliers, outliers);
value = (inliers*100) / (inliers+outliers);
}
}
else
{
value = mpts_1.size();
}
}
else
{
printf("Images are not valid!\n");
showUsage();
}
if(!quiet)
printf("Similarity = %d\n", value);
return value;
}
+42
View File
@@ -0,0 +1,42 @@
### Qt Gui stuff ###
SET(headers_ui
ImagesTcpServer.h
)
#This will generate moc_* for Qt
QT4_WRAP_CPP(moc_srcs ${headers_ui})
### Qt Gui stuff end###
SET(SRC_FILES
ImagesTcpServer.cpp
main.cpp
${moc_srcs}
)
SET(INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/../../include
${CMAKE_CURRENT_SOURCE_DIR}
${OpenCV_INCLUDE_DIRS}
)
INCLUDE(${QT_USE_FILE})
SET(LIBRARIES
${OpenCV_LIBS}
${QT_LIBRARIES}
)
# Make sure the compiler can find include files from our library.
INCLUDE_DIRECTORIES(${INCLUDE_DIRS})
# Add binary called "example" that is built from the source file "main.cpp".
# The extension is automatically found.
ADD_EXECUTABLE(tcpImagesServer ${SRC_FILES})
TARGET_LINK_LIBRARIES(tcpImagesServer find_object ${LIBRARIES})
SET_TARGET_PROPERTIES( tcpImagesServer
PROPERTIES OUTPUT_NAME ${PROJECT_PREFIX}-tcpImagesServer)
INSTALL(TARGETS tcpImagesServer
RUNTIME DESTINATION bin COMPONENT runtime
BUNDLE DESTINATION "${CMAKE_BUNDLE_LOCATION}" COMPONENT runtime)
+93
View File
@@ -0,0 +1,93 @@
/*
* ImagesTcpServer.cpp
*
* Created on: 2014-05-21
* Author: mathieu
*/
#include "find_object/Settings.h"
#include "find_object/QtOpenCV.h"
#include "ImagesTcpServer.h"
#include <QtNetwork/QNetworkInterface>
#include <QtNetwork/QTcpSocket>
#include <QtGui/QTransform>
ImagesTcpServer::ImagesTcpServer(float hz, const QString & path, QObject * parent) :
QTcpServer(parent)
{
// Set camera parameters
Settings::setCamera_4imageRate(hz);
Settings::setCamera_5mediaPath(path);
connect(this, SIGNAL(newConnection()), this, SLOT(addClient()));
connect(&camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(publishImage(const cv::Mat &)));
}
QHostAddress ImagesTcpServer::getHostAddress() const
{
QHostAddress hostAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
{
hostAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (hostAddress.isNull())
{
hostAddress = QHostAddress(QHostAddress::LocalHost);
}
return hostAddress;
}
quint16 ImagesTcpServer::getPort() const
{
return this->serverPort();
}
void ImagesTcpServer::publishImage(const cv::Mat & image)
{
QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
if(clients.size())
{
std::vector<unsigned char> buf;
cv::imencode(".png", image, buf);
for(QList<QTcpSocket*>::iterator iter = clients.begin(); iter!=clients.end(); ++iter)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint64)0;
out.writeRawData((char*)buf.data(), (int)buf.size());
out.device()->seek(0);
out << (quint64)(block.size() - sizeof(quint64));
(*iter)->write(block);
}
}
else
{
printf("Paused...\n");
camera_.pause();
}
}
void ImagesTcpServer::addClient()
{
QTcpSocket * client = this->nextPendingConnection();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
if(!camera_.isRunning())
{
printf("Start...\n");
camera_.start();
}
}
+33
View File
@@ -0,0 +1,33 @@
/*
* ImagesTcpServer.h
*
* Created on: 2014-05-21
* Author: mathieu
*/
#ifndef TCPCLIENT_H_
#define TCPCLIENT_H_
#include "find_object/Camera.h"
#include <QtNetwork/QTcpServer>
class ImagesTcpServer : public QTcpServer
{
Q_OBJECT
public:
ImagesTcpServer(float hz = 10.0f, const QString & path = "", QObject * parent = 0);
QHostAddress getHostAddress() const;
quint16 getPort() const;
private Q_SLOTS:
void addClient();
void publishImage(const cv::Mat & image);
private:
Camera camera_;
};
#endif /* TCPCLIENT_H_ */
+106
View File
@@ -0,0 +1,106 @@
/*
* main.cpp
*
* Created on: 2014-05-05
* Author: mathieu
*/
#include <QtNetwork/QNetworkInterface>
#include <QtCore/QCoreApplication>
#include "ImagesTcpServer.h"
void showUsage()
{
printf("imagesTcpServer [options]\n"
" Options:\n"
" -hz #.# Image rate (default 10 Hz).\n"
" -p # Set manually a port to which the clients will connect.\n"
" -path \"\" Set a path of a directory of images or a video file.\n");
exit(-1);
}
int main(int argc, char * argv[])
{
QString ipAddress;
float hz = 10.0f;
quint16 port = 0;
QString path;
for(int i=1; i<argc; ++i)
{
if(strcmp(argv[i], "-hz") == 0)
{
++i;
if(i < argc)
{
hz = std::atof(argv[i]);
if(hz < 0.0f)
{
printf("[ERROR] Image rate not valid : %s\n", argv[i]);
showUsage();
}
}
else
{
showUsage();
}
continue;
}
if(strcmp(argv[i], "-p") == 0)
{
++i;
if(i < argc)
{
int v = std::atoi(argv[i]);
if(v < 0)
{
printf("[ERROR] Port not valid : %s\n", argv[i]);
showUsage();
}
port = v;
}
else
{
showUsage();
}
continue;
}
if(strcmp(argv[i], "-path") == 0)
{
++i;
if(i < argc)
{
path = argv[i];
}
else
{
showUsage();
}
continue;
}
printf("Unrecognized option: %s\n", argv[i]);
showUsage();
}
if(!path.isEmpty())
{
printf("Using images from path \"%s\"\n", path.toStdString().c_str());
}
QCoreApplication app(argc, argv);
ImagesTcpServer server(hz, path);
if (!server.listen(QHostAddress::Any, port))
{
printf("ERROR: Unable to start the TCP server: %s\n", server.errorString().toStdString().c_str());
return -1;
}
printf("Images server waiting on \"%s:%d\"...\n",
server.getHostAddress().toString().toStdString().c_str(), server.getPort());
return app.exec();
}
+40
View File
@@ -0,0 +1,40 @@
### Qt Gui stuff ###
SET(headers_ui
TcpClient.h
)
#This will generate moc_* for Qt
QT4_WRAP_CPP(moc_srcs ${headers_ui})
### Qt Gui stuff end###
SET(SRC_FILES
TcpClient.cpp
main.cpp
${moc_srcs}
)
SET(INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}
${OpenCV_INCLUDE_DIRS}
)
INCLUDE(${QT_USE_FILE})
SET(LIBRARIES
${OpenCV_LIBS}
${QT_LIBRARIES}
)
# Make sure the compiler can find include files from our library.
INCLUDE_DIRECTORIES(${INCLUDE_DIRS})
# Add binary called "example" that is built from the source file "main.cpp".
# The extension is automatically found.
ADD_EXECUTABLE(tcpObjectsClient ${SRC_FILES})
TARGET_LINK_LIBRARIES(tcpObjectsClient ${LIBRARIES})
SET_TARGET_PROPERTIES( tcpObjectsClient
PROPERTIES OUTPUT_NAME ${PROJECT_PREFIX}-tcpObjectsClient)
INSTALL(TARGETS tcpObjectsClient
RUNTIME DESTINATION bin COMPONENT runtime
BUNDLE DESTINATION "${CMAKE_BUNDLE_LOCATION}" COMPONENT runtime)
+143
View File
@@ -0,0 +1,143 @@
/*
* TCPClient.cpp
*
* Created on: 2014-05-05
* Author: mathieu
*/
#include "TcpClient.h"
#include <opencv2/opencv.hpp>
#include <QtGui/QTransform>
#include <QtCore/QPointF>
#include <QtCore/QTime>
TcpClient::TcpClient(const QString & hostname, quint16 port, QObject *parent) :
QTcpSocket(parent),
blockSize_(0)
{
connect(this, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(this, SIGNAL(disconnected()), this, SLOT(connectionLost()));
this->connectToHost(hostname, port);
}
void TcpClient::readReceivedData()
{
QDataStream in(this);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize_ == 0)
{
if (this->bytesAvailable() < (int)sizeof(quint16))
{
return;
}
in >> blockSize_;
}
if (this->bytesAvailable() < blockSize_)
{
return;
}
blockSize_ = 0;
QVector<float> data;
in >> data;
printf("---\n");
if(data.size() == 0)
{
printf("(%s) No objects detected.\n",
QTime::currentTime().toString("HH:mm:ss.zzz").toStdString().c_str());
}
else
{
for(int i=0; i<data.size(); i+=12)
{
// get data
int id = (int)data[i];
float objectWidth = data[i+1];
float objectHeight = data[i+2];
// Find corners Qt
QTransform qtHomography(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]);
QPointF qtTopLeft = qtHomography.map(QPointF(0,0));
QPointF qtTopRight = qtHomography.map(QPointF(objectWidth,0));
QPointF qtBottomLeft = qtHomography.map(QPointF(0,objectHeight));
QPointF qtBottomRight = qtHomography.map(QPointF(objectWidth,objectHeight));
printf("(%s) Object %d detected, Qt corners at (%f,%f) (%f,%f) (%f,%f) (%f,%f)\n",
QTime::currentTime().toString("HH:mm:ss.zzz").toStdString().c_str(),
id,
qtTopLeft.x(), qtTopLeft.y(),
qtTopRight.x(), qtTopRight.y(),
qtBottomLeft.x(), qtBottomLeft.y(),
qtBottomRight.x(), qtBottomRight.y());
// Example with OpenCV
if(0)
{
// Find corners OpenCV
cv::Mat cvHomography(3, 3, CV_32F);
cvHomography.at<float>(0,0) = data[i+3];
cvHomography.at<float>(1,0) = data[i+4];
cvHomography.at<float>(2,0) = data[i+5];
cvHomography.at<float>(0,1) = data[i+6];
cvHomography.at<float>(1,1) = data[i+7];
cvHomography.at<float>(2,1) = data[i+8];
cvHomography.at<float>(0,2) = data[i+9];
cvHomography.at<float>(1,2) = data[i+10];
cvHomography.at<float>(2,2) = data[i+11];
std::vector<cv::Point2f> inPts, outPts;
inPts.push_back(cv::Point2f(0,0));
inPts.push_back(cv::Point2f(objectWidth,0));
inPts.push_back(cv::Point2f(0,objectHeight));
inPts.push_back(cv::Point2f(objectWidth,objectHeight));
cv::perspectiveTransform(inPts, outPts, cvHomography);
printf("(%s) Object %d detected, CV corners at (%f,%f) (%f,%f) (%f,%f) (%f,%f)\n",
QTime::currentTime().toString("HH:mm:ss.zzz").toStdString().c_str(),
id,
outPts.at(0).x, outPts.at(0).y,
outPts.at(1).x, outPts.at(1).y,
outPts.at(2).x, outPts.at(2).y,
outPts.at(3).x, outPts.at(3).y);
}
}
}
}
void TcpClient::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError)
{
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
printf("Tcp error: The host was not found. Please "
"check the host name and port settings.\n");
break;
case QAbstractSocket::ConnectionRefusedError:
printf("The connection was refused by the peer. "
"Make sure Find-Object is running, "
"and check that the host name and port "
"settings are correct.\n");
break;
default:
printf("The following error occurred: %s.\n", this->errorString().toStdString().c_str());
break;
}
}
void TcpClient::connectionLost()
{
printf("Connection lost!\n");
}
+28
View File
@@ -0,0 +1,28 @@
/*
* TCPClient.h
*
* Created on: 2014-05-05
* Author: mathieu
*/
#ifndef TCPCLIENT_H_
#define TCPCLIENT_H_
#include <QtNetwork/QTcpSocket>
class TcpClient : public QTcpSocket
{
Q_OBJECT;
public:
TcpClient(const QString & hostname, quint16 port, QObject * parent = 0);
private Q_SLOTS:
void readReceivedData();
void displayError(QAbstractSocket::SocketError socketError);
void connectionLost();
private:
quint16 blockSize_;
};
#endif /* TCPCLIENT_H_ */
+77
View File
@@ -0,0 +1,77 @@
/*
* main.cpp
*
* Created on: 2014-05-05
* Author: mathieu
*/
#include <QtNetwork/QNetworkInterface>
#include <QtCore/QCoreApplication>
#include "TcpClient.h"
void showUsage()
{
printf("tcpClient [hostname] port\n");
exit(-1);
}
int main(int argc, char * argv[])
{
if(argc < 2 || argc > 3)
{
showUsage();
}
QString ipAddress;
quint16 port = 0;
if(argc == 2)
{
port = atoi(argv[1]);
}
else if(argc == 3)
{
ipAddress = argv[1];
port = atoi(argv[2]);
}
if(ipAddress.isEmpty())
{
// find out which IP to connect to
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address())
{
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
{
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
}
}
QCoreApplication app(argc, argv);
printf("Connecting to \"%s:%d\"...\n", ipAddress.toStdString().c_str(), port);
TcpClient client(ipAddress, port);
if(client.waitForConnected())
{
printf("Connecting to \"%s:%d\"... connected!\n", ipAddress.toStdString().c_str(), port);
app.exec();
}
else
{
printf("Connecting to \"%s:%d\"... connection failed!\n", ipAddress.toStdString().c_str(), port);
}
return 0;
}