JsonWriter.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the Universite de Sherbrooke nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00028 #include "find_object/JsonWriter.h"
00029 #include "find_object/utilite/ULogger.h"
00030 
00031 #include <QtCore/QFile>
00032 #include <QtCore/QTextStream>
00033 
00034 #include "json/json.h"
00035 
00036 namespace find_object {
00037 
00038 void JsonWriter::write(const DetectionInfo & info, const QString & path)
00039 {
00040         if(!path.isEmpty())
00041         {
00042                 Json::Value root;
00043 
00044                 if(info.objDetected_.size())
00045                 {
00046                         Json::Value detections;
00047 
00048                         QMultiMap<int, int>::const_iterator iterInliers = info.objDetectedInliersCount_.constBegin();
00049                         QMultiMap<int, int>::const_iterator iterOutliers = info.objDetectedOutliersCount_.constBegin();
00050                         QMultiMap<int, QSize>::const_iterator iterSizes = info.objDetectedSizes_.constBegin();
00051                         QMultiMap<int, QString>::const_iterator iterFilenames = info.objDetectedFilenames_.constBegin();
00052                         for(QMultiMap<int, QTransform>::const_iterator iter = info.objDetected_.constBegin(); iter!= info.objDetected_.end();)
00053                         {
00054                                 char index = 'a';
00055                                 int id = iter.key();
00056                                 while(iter != info.objDetected_.constEnd() && id == iter.key())
00057                                 {
00058                                         QString name = QString("object_%1%2").arg(id).arg(info.objDetected_.count(id)>1?QString(index++):"");
00059                                         detections.append(name.toStdString());
00060 
00061                                         Json::Value homography;
00062                                         homography.append(iter.value().m11());
00063                                         homography.append(iter.value().m12());
00064                                         homography.append(iter.value().m13());
00065                                         homography.append(iter.value().m21());
00066                                         homography.append(iter.value().m22());
00067                                         homography.append(iter.value().m23());
00068                                         homography.append(iter.value().m31());  // dx
00069                                         homography.append(iter.value().m32());  // dy
00070                                         homography.append(iter.value().m33());
00071                                         root[name.toStdString()]["width"] = iterSizes.value().width();
00072                                         root[name.toStdString()]["height"] = iterSizes.value().height();
00073                                         root[name.toStdString()]["homography"] = homography;
00074                                         root[name.toStdString()]["inliers"] = iterInliers.value();
00075                                         root[name.toStdString()]["outliers"] = iterOutliers.value();
00076                                         root[name.toStdString()]["filename"] = iterFilenames.value().toStdString();
00077 
00078                                         ++iter;
00079                                         ++iterInliers;
00080                                         ++iterOutliers;
00081                                         ++iterSizes;
00082                                         ++iterFilenames;
00083                                 }
00084                         }
00085                         root["objects"] = detections;
00086                 }
00087 
00088                 if(info.matches_.size())
00089                 {
00090                         Json::Value matchesValues;
00091                         const QMap<int, QMultiMap<int, int> > & matches = info.matches_;
00092                         for(QMap<int, QMultiMap<int, int> >::const_iterator iter = matches.constBegin();
00093                                 iter != matches.end();
00094                                 ++iter)
00095                         {
00096                                 QString name = QString("matches_%1").arg(iter.key());
00097                                 root[name.toStdString()] = iter.value().size();
00098                                 matchesValues.append(name.toStdString());
00099                         }
00100                         root["matches"] = matchesValues;
00101                 }
00102 
00103                 // write in a nice readible way
00104                 Json::StyledWriter styledWriter;
00105                 //std::cout << styledWriter.write(root);
00106                 QFile file(path);
00107                 file.open(QIODevice::WriteOnly | QIODevice::Text);
00108                 QTextStream out(&file);
00109                 out << styledWriter.write(root).c_str();
00110                 file.close();
00111         }
00112 }
00113 
00114 } // namespace find_object


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Feb 11 2016 22:57:56