forked from starkdg/Redis-ImageScout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgscoutclient.cpp
280 lines (220 loc) · 7.37 KB
/
imgscoutclient.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include "hiredis.h"
#include "pHash.h"
using namespace std;
namespace fs = boost::filesystem;
namespace po = boost::program_options;
struct Args {
string cmd, host, key;
fs::path dirname;
int port;
long long id;
double radius;
};
Args ParseOptions(int argc, char **argv){
Args args;
po::options_description descr("Imgscout Options");
try {
descr.add_options()
("help,h", "produce help message")
("key,k", po::value<string>(&args.key)->required(), "redis key")
("dir,d", po::value<fs::path>(&args.dirname)->default_value(""), "directory of images to process")
("cmd,c", po::value<string>(&args.cmd)->required(), "command: add, sync, del, query, lookup, help")
("server,s", po::value<string>(&args.host)->default_value("localhost"), "redis server address")
("port,p", po::value<int>(&args.port)->default_value(6379), "redis server port")
("radius,r", po::value<double>(&args.radius)->default_value(5), "query radius")
("id,i", po::value<long long>(&args.id)->default_value(0), "id value for lookup");
po::positional_options_description pd;
pd.add("cmd", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(descr).positional(pd).run(), vm);
if (vm.count("help") || args.cmd == "help"){
cout << descr << endl;
exit(0);
}
if (args.cmd == "add" || args.cmd == "query"){
if (args.cmd.empty()) {
cout << "missing directory argument" << endl;
cout << descr << endl;
exit(0);
}
}
if (args.cmd == "del" || args.cmd == "lookup"){
if (args.id == 0){
cout << "missing id arg" << endl;
cout << descr << endl;
exit(0);
}
}
po::notify(vm);
} catch(const po::error &ex){
cout << ex.what() << endl;
cout << descr << endl;
exit(0);
}
return args;
}
/*-------- execute commmands -----------------*/
int SubmitFiles(redisContext *c, const string &key, const fs::path &dirname){
fs::directory_iterator dir(dirname), end;
int count = 0;
for (;dir!= end;++dir){
if (fs::is_regular_file(dir->status())){
string filename = dir->path().string();
cout << "(" << count++ << ") " << filename << endl;
ulong64 hash_value;
if (ph_dct_imagehash(filename.c_str(), hash_value) < 0){
cout << "unable to calc hash fingerprint" << endl;
continue;
}
redisReply *reply = (redisReply*)redisCommand(c, "imgscout.add %s %llu %s",
key.c_str(), hash_value, filename.c_str());
if (reply && reply->type == REDIS_REPLY_INTEGER){
cout << "=> id = " << reply->integer << endl;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cerr << "=> error: " << reply->str << endl;
} else {
cerr << "Disconnected" << endl;
break;
}
freeReplyObject(reply);
} else {
cerr << "not a file: " << dir->path() << endl;
}
}
return count;
}
int Sync(redisContext *c, const string &key){
redisReply *reply = (redisReply*)redisCommand(c, "imgscout.sync %s", key.c_str());
if (reply && reply->type == REDIS_REPLY_STATUS){
cout << "=> sync'd" << endl;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cout << "Error: " << reply->str << endl;
} else {
cerr << "Disconnected" << endl;
return 0;
}
freeReplyObject(reply);
return 1;
}
int Size(redisContext *c, const string &key){
redisReply *reply = (redisReply*)redisCommand(c, "imgscout.size %s", key.c_str());
if (reply && reply->type == REDIS_REPLY_INTEGER){
cout << "size " << reply->integer << " bytes" << endl;
} if (reply && reply->type == REDIS_REPLY_ERROR){
cout << "Error: " << reply->str << endl;
} else {
cerr << "Disconnected" << endl;
return 0;
}
return 1;
}
int DeleteId(redisContext *c, const string &key, const long long id){
redisReply *reply = (redisReply*)redisCommand(c, "imgscout.del %s %lld", key.c_str(), id);
if (reply && reply->type == REDIS_REPLY_STRING){
cout << "=> deleted " << id << endl;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cout << "Error: " << reply->str << endl;
} else {
cerr << "Disconnected" << endl;
return -1;
}
freeReplyObject(reply);
return 0;
}
void ProcessSubReply(redisReply *subreply){
if (subreply->type == REDIS_REPLY_ARRAY && subreply->elements == 3){
cout << " descr = " << subreply->element[0]->str << endl;
cout << " id = " << subreply->element[1]->integer << endl;
cout << " distance = " << subreply->element[2]->str << endl;
}
}
int QueryFiles(redisContext *c, const string &key, const fs::path &dirname, const double radius){
fs::directory_iterator dir(dirname), end;
int count = 0;
for (;dir!=end;++dir){
if (fs::is_regular_file(dir->status())){
string filename = dir->path().string();
ulong64 hash_value;
if (ph_dct_imagehash(filename.c_str(), hash_value) < 0){
cout << "unable to calc hash fingerprint" << endl;
continue;
}
cout << "(" << ++count << ") query: " << filename << endl;
redisReply *reply = (redisReply*)redisCommand(c, "imgscout.query %s %llu %f",
key.c_str(), hash_value, radius);
if (reply && reply->type == REDIS_REPLY_ARRAY){
for (int i=0;i<reply->elements;i++){
cout << " (" << i << ")" << endl;
ProcessSubReply(reply->element[i]);
}
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cerr << "=> error: " << reply->str << endl;
} else if (reply){
cerr << "error: " << reply->str << endl;
} else {
cerr << "Disconnected" << endl;
break;
}
freeReplyObject(reply);
} else {
cerr << "not a file: " << dir->path() << endl;
}
}
return count;
}
int LookupId(redisContext *c, const string &key, const long long id){
redisReply *reply = (redisReply*)redisCommand(c, "imgscout.lookup %s %lld", key.c_str(), id);
if (reply && reply->type == REDIS_REPLY_STRING){
cout << " => " << reply->str << endl;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cout << " Error: " << reply->str << endl;
} else {
cout << "Disconnected" << endl;
return 0;
}
freeReplyObject(reply);
return 1;
}
void print_header(){
cout << endl << "------- Image Scout Client -----------" << endl << endl;
}
int main(int argc, char **argv){
print_header();
Args args = ParseOptions(argc, argv);
cout << "Connect to " << args.host << ":" << args.port << endl;
redisContext *c = redisConnect(args.host.c_str(), args.port);
if (c == NULL || c->err){
if (c) cerr << "Unable to connect: " << c->errstr << endl;
else cerr << "Unable to connect to server" << endl;
exit(0);
}
int n_files = 0;
if (args.cmd == "add"){
cout << "Submit files in " << args.dirname << endl;
n_files = SubmitFiles(c, args.key, args.dirname);
cout << "Successfully submitted " << n_files << " files " << endl;
} else if (args.cmd == "del"){
cout << "Delete " << args.id << endl;
n_files = DeleteId(c, args.key, args.id);
} else if (args.cmd == "query"){
cout << "Query files in " << args.dirname << endl;
n_files = QueryFiles(c, args.key, args.dirname, args.radius);
cout << "Successfully queried " << n_files << " files " << endl;
} else if (args.cmd == "lookup"){
cout << "Lookup id " << args.id << endl;
n_files = LookupId(c, args.key, args.id);
} else if (args.cmd == "sync"){
cout << "Sync database" << endl;
Sync(c, args.key);
} else {
cerr << "Unrecognized command: " << args.cmd << endl;
}
cout << "Done." << endl;
return 0;
}