JsonWriter.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "find_object/JsonWriter.h"
30 
31 #include <QtCore/QFile>
32 #include <QtCore/QTextStream>
33 #include <QtCore/QFileInfo>
34 
35 #include "json/json.h"
36 
37 namespace find_object {
38 
39 void JsonWriter::write(const DetectionInfo & info, const QString & path)
40 {
41  if(!path.isEmpty())
42  {
43  Json::Value root;
44 
45  if(info.objDetected_.size())
46  {
47  Json::Value detections;
48 
49  QMultiMap<int, int>::const_iterator iterInliers = info.objDetectedInliersCount_.constBegin();
50  QMultiMap<int, int>::const_iterator iterOutliers = info.objDetectedOutliersCount_.constBegin();
51  QMultiMap<int, QSize>::const_iterator iterSizes = info.objDetectedSizes_.constBegin();
52  QMultiMap<int, QString>::const_iterator iterFilePaths = info.objDetectedFilePaths_.constBegin();
53  for(QMultiMap<int, QTransform>::const_iterator iter = info.objDetected_.constBegin(); iter!= info.objDetected_.end();)
54  {
55  char index = 'a';
56  int id = iter.key();
57  while(iter != info.objDetected_.constEnd() && id == iter.key())
58  {
59  QString name = QString("object_%1%2").arg(id).arg(info.objDetected_.count(id)>1?QString(index++):"");
60  detections.append(name.toStdString());
61 
62  Json::Value homography;
63  homography.append(iter.value().m11());
64  homography.append(iter.value().m12());
65  homography.append(iter.value().m13());
66  homography.append(iter.value().m21());
67  homography.append(iter.value().m22());
68  homography.append(iter.value().m23());
69  homography.append(iter.value().m31()); // dx
70  homography.append(iter.value().m32()); // dy
71  homography.append(iter.value().m33());
72  root[name.toStdString()]["width"] = iterSizes.value().width();
73  root[name.toStdString()]["height"] = iterSizes.value().height();
74  root[name.toStdString()]["homography"] = homography;
75  root[name.toStdString()]["inliers"] = iterInliers.value();
76  root[name.toStdString()]["outliers"] = iterOutliers.value();
77  root[name.toStdString()]["filepath"] = iterFilePaths.value().toStdString();
78  QString filename;
79  if(!iterFilePaths.value().isEmpty())
80  {
81  QFileInfo file(iterFilePaths.value());
82  filename=file.fileName();
83  }
84  root[name.toStdString()]["filename"] = filename.toStdString();
85 
86  ++iter;
87  ++iterInliers;
88  ++iterOutliers;
89  ++iterSizes;
90  ++iterFilePaths;
91  }
92  }
93  root["objects"] = detections;
94  }
95 
96  if(info.matches_.size())
97  {
98  Json::Value matchesValues;
99  const QMap<int, QMultiMap<int, int> > & matches = info.matches_;
100  for(QMap<int, QMultiMap<int, int> >::const_iterator iter = matches.constBegin();
101  iter != matches.end();
102  ++iter)
103  {
104  QString name = QString("matches_%1").arg(iter.key());
105  root[name.toStdString()] = iter.value().size();
106  matchesValues.append(name.toStdString());
107  }
108  root["matches"] = matchesValues;
109  }
110 
111  // write in a nice readible way
112  Json::StyledWriter styledWriter;
113  //std::cout << styledWriter.write(root);
114  QFile file(path);
115  file.open(QIODevice::WriteOnly | QIODevice::Text);
116  QTextStream out(&file);
117  out << styledWriter.write(root).c_str();
118  file.close();
119  }
120 }
121 
122 } // namespace find_object
filename
QMultiMap< int, QString > objDetectedFilePaths_
Definition: DetectionInfo.h:73
virtual std::string write(const Value &root)
Serialize a Value in JSON format.
Definition: jsoncpp.cpp:3669
QMultiMap< int, QSize > objDetectedSizes_
Definition: DetectionInfo.h:72
Represents a JSON value.
Definition: json.h:433
QMap< int, QMultiMap< int, int > > matches_
Definition: DetectionInfo.h:83
QMultiMap< int, int > objDetectedInliersCount_
Definition: DetectionInfo.h:74
QMultiMap< int, QTransform > objDetected_
Definition: DetectionInfo.h:71
ArrayIndex size() const
Number of values in array or object.
Definition: jsoncpp.cpp:2506
QMultiMap< int, int > objDetectedOutliersCount_
Definition: DetectionInfo.h:75
ULogger class and convenient macros.
static void write(const DetectionInfo &info, const QString &path)
Definition: JsonWriter.cpp:39
Writes a Value in JSON format in a human friendly way.
Definition: json.h:1732
Value & append(const Value &value)
Append value to array at the end.
Definition: jsoncpp.cpp:2769


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:20:09