/
picpac-stream.cpp
96 lines (88 loc) · 3.19 KB
/
picpac-stream.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
#include <set>
#include <boost/filesystem/fstream.hpp>
#include <boost/program_options.hpp>
#include "picpac-cv.h"
using namespace std;
using namespace picpac;
int main(int argc, char const* argv[]) {
BatchImageStream::Config config;
unsigned max;
float scale;
fs::path db_path;
fs::path dir_path;
vector<int> picks;
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message.")
("max", po::value(&max)->default_value(100), "")
("scale", po::value(&scale)->default_value(1), "")
("db", po::value(&db_path), "")
("dir", po::value(&dir_path), "")
("pick,p", po::value(&picks), "pick label")
("norm", "")
;
#define PICPAC_CONFIG_UPDATE(C,p) desc.add_options()(#p, po::value(&C.p)->default_value(C.p), "")
PICPAC_CONFIG_UPDATE_ALL(config);
#undef PICPAC_CONFIG_UPDATE
po::positional_options_description p;
p.add("db", 1);
p.add("dir", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help") || db_path.empty() || dir_path.empty()) {
cout << "Usage:" << endl;
cout << "\tpicpac-stat ... <db>" << endl;
cout << desc;
cout << endl;
return 0;
}
bool do_norm = vm.count("norm");
ImageStream db(db_path, config);
fs::create_directories(dir_path);
fs::ofstream index(dir_path/"index.html");
int c = 0;
index << "<html><body><table><tr><th>label</th><th>image</th></tr>" << endl;
set<int> picked(picks.begin(), picks.end());
for (unsigned i = 0; c < max; ++i) {
try {
ImageStream::Value v(db.next());
if (picks.size()) {
if (picked.count(int(v.label)) == 0) continue;
}
index << "<tr><td>" << v.label << "</td><td><img src='" << c << ".jpg'></img></td>";
fs::path ip = dir_path / (lexical_cast<string>(c) + ".jpg");
if (scale != 1) {
cv::resize(v.image, v.image, cv::Size(), scale, scale);
if (v.annotation.data) {
cv::resize(v.annotation, v.annotation, cv::Size(), scale, scale);
}
}
if (do_norm) {
cv::Mat tmp;
cv::normalize(v.image, tmp, 0, 255, cv::NORM_MINMAX, CV_8U);
v.image = tmp;
}
cv::imwrite(ip.native(), v.image);
if (v.annotation.data) {
if (config.anno_copy && do_norm) {
cv::Mat tmp;
cv::normalize(v.annotation, tmp, 0, 255, cv::NORM_MINMAX, CV_8U);
v.annotation = tmp;
}
index << "<td><img src='" << c << "a.jpg'></img></td>";
fs::path ap = dir_path / (lexical_cast<string>(c) + "a.jpg");
cv::imwrite(ap.native(), v.annotation);
}
index << "</tr>" << endl;
++c;
}
catch (EoS const &) {
break;
}
}
index << "</table></body></html>" << endl;
return 0;
}