-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
106 lines (88 loc) · 2.16 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "nlohmann/json.hpp"
#include <iostream>
#include <fstream>
using namespace std;
void PrettyJson(std::string filename)
{
using nlohmann::json;
std::ifstream iFile(filename);
json j;
try { iFile >> j; }
catch (...) {
std::cerr << "Load configuration file from json failed: " << filename << std::endl;
iFile.close();
return;
}
std::ofstream o(filename);
o << std::setw(4) << j << std::endl;
iFile.close();
}
void writeExample(string jsonFile)
{
using nlohmann::json;
std::fstream File;
File.open(jsonFile, fstream::out);
double val = 0.0;
json j;
j["param"]["val1"] = val;
j["param"]["val2"] = val;
j["param"]["val3"] = val;
j["param"]["val4"] = val;
j["result"]["rmse_mm"] = val ;
j["result"]["rmse_pixel"] = val;
json j_arr = json::array();
j_arr.push_back(j);
int cnt = 5;
for (int i = 0; i < cnt; ++i) {
//LOG(INFO) << frameFilenames_[i] << endl;
json jj;
jj["image"] = to_string(i)+".jpg";
jj["resolution"] = { 100, 100 };
jj["id"] = i;
j_arr.push_back(jj);
}
File << j_arr;
File.close();
PrettyJson(jsonFile);
}
void readExample(string jsonFile)
{
using nlohmann::json;
ifstream iFile(jsonFile);
json j;
try { iFile >> j; }
catch (...) {
cerr << "Parse result json file failed: " << jsonFile << std::endl;
iFile.close();
}
iFile.close();
// Print out
for (auto item : j) {
if (item.contains("param")) {
json jItem = item["param"];
double val = jItem["val1"];
cout << "param::val1 is: " << val << endl;
val = jItem["val2"];
cout << "param::val2 is: " << val << endl;
}
if (item.contains("result")) {
json jItem = item["result"];
double val = jItem["rmse_mm"];
cout << "param::rmse_mm is: " << val << endl;
}
if (item.contains("image")) {
std::string imgNameStr = item["image"];
int id = item["id"];
vector<int> res = item["resolution"].get<vector<int>>(); // Get array from json
cout << "Image id:" << id << ": " << imgNameStr << endl;
cout << "Res: " << res[0] << ", " << res[1] << endl;
}
}
}
int main()
{
string jsonFile = "example.json";
writeExample(jsonFile);
readExample(jsonFile);
return 0;
}