22 #include <fvutils/writers/png.h> 23 #include <mongo/client/dbclient.h> 24 #include <mongo/client/gridfs.h> 25 #include <utils/misc/string_conversions.h> 26 #include <utils/system/argparser.h> 28 using namespace firevision;
29 using namespace mongo;
32 #ifdef HAVE_MONGODB_VERSION_H 34 # define QUERY MONGO_QUERY 38 print_usage(
const char *progname)
40 printf(
"Usage: %s [-h] [-o dir] [-f] [-d database] -c collection items...\n" 41 " -h Show this help message\n" 42 " -o dir Output directory where to create PNG files\n" 43 " -f Use original filenames form database\n" 44 " -d database Database to query for images\n" 45 " -c collection Collection to query for images\n" 47 "Items are either timestamps (ms precision) or timestamp ranges in\n" 50 "Example: %s -d fflog -c openni_image_rgb 0..1355421345807\n" 57 main(
int argc,
char **argv)
60 if (argp.has_arg(
"h")) {
65 const std::vector<const char *> &items = argp.items();
67 std::string output_dir =
"tmp/";
68 std::string database =
"fflog";
69 std::string collection;
70 std::string query_coll;
71 bool filename_indexed = !argp.has_arg(
"f");
73 std::vector<std::pair<long long, long long>> times;
75 if (argp.has_arg(
"o")) {
76 output_dir = argp.arg(
"o");
77 if (output_dir[output_dir.length() - 1] !=
'/') {
81 if (argp.has_arg(
"d")) {
82 database = argp.arg(
"d");
84 if (argp.has_arg(
"c")) {
85 collection = argp.arg(
"c");
88 printf(
"No collection given\n");
92 query_coll = database +
"." + collection;
95 times.push_back(std::make_pair(0L, std::numeric_limits<long long>::max()));
97 for (
unsigned int i = 0; i < items.size(); ++i) {
98 std::string item = items[i];
99 std::string::size_type dotpos = item.find(
"..");
100 if (dotpos == std::string::npos) {
102 long int ts = argp.parse_item_int(i);
103 times.push_back(std::make_pair(ts, ts));
106 std::string first_ts, second_ts;
107 first_ts = item.substr(0, dotpos);
108 second_ts = item.substr(dotpos + 2);
109 times.push_back(std::make_pair(StringConversions::to_long(first_ts),
110 StringConversions::to_long(second_ts)));
115 unsigned int image_n = 0;
117 DBClientConnection *mongodb_client =
new DBClientConnection(
true);
119 mongodb_client->connect(
"localhost", errmsg);
121 GridFS *gridfs =
new GridFS(*mongodb_client,
"fflog");
123 for (
unsigned int i = 0; i < times.size(); ++i) {
126 if (times[i].first == times[i].second) {
127 printf(
"Querying for timestamp %lli\n", times[i].first);
128 q = QUERY(
"timestamp" << times[i].first).sort(
"timestamp", 1);
130 printf(
"Querying for range %lli..%lli\n", times[i].first, times[i].second);
131 q = QUERY(
"timestamp" << mongo::GTE << times[i].first << mongo::LTE << times[i].second)
132 .sort(
"timestamp", 1);
135 #if __cplusplus >= 201103L 136 std::unique_ptr<mongo::DBClientCursor> cursor = mongodb_client->query(query_coll, q);
138 std::auto_ptr<mongo::DBClientCursor> cursor = mongodb_client->query(query_coll, q);
141 while (cursor->more()) {
142 BSONObj doc = cursor->next();
144 BSONObj imgdoc = doc.getObjectField(
"image");
145 if (imgdoc[
"colorspace"].String() ==
"RGB") {
146 std::string filename = imgdoc.getFieldDotted(
"data.filename").String();
147 long filesize = imgdoc.getFieldDotted(
"data.length").numberLong();
148 std::string image_id = imgdoc[
"image_id"].String();
150 std::string out_filename;
152 if (filename_indexed) {
153 if (asprintf(&fntmp,
"%s%s-%08d.png", output_dir.c_str(), image_id.c_str(), image_n++)
155 out_filename = fntmp;
159 if (asprintf(&fntmp,
"%s%s.png", output_dir.c_str(), filename.c_str()) != -1) {
160 out_filename = fntmp;
166 printf(
"Restoring RGB image %s (%s)\n", filename.c_str(), out_filename.c_str());
168 GridFile file = gridfs->findFile(filename);
169 if (!file.exists()) {
170 printf(
"File %s does not exist\n", filename.c_str());
174 unsigned int width = imgdoc[
"width"].Int();
175 unsigned int height = imgdoc[
"height"].Int();
177 if (colorspace_buffer_size(RGB, width, height) != (
size_t)filesize) {
178 printf(
"Buffer size mismatch (DB %li vs. exp. %zu)\n",
180 colorspace_buffer_size(RGB, width, height));
184 unsigned char *buffer = malloc_buffer(RGB, width, height);
186 unsigned char *tmp = buffer;
187 for (
int c = 0; c < file.getNumChunks(); ++c) {
188 mongo::GridFSChunk chunk = file.getChunk(c);
190 const char * chunk_data = chunk.data(len);
191 memcpy(tmp, chunk_data, len);
195 PNGWriter writer(out_filename.c_str(), width, height);
205 delete mongodb_client;
Fawkes library namespace.
Parse command line arguments.
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.