-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
352 lines (297 loc) · 11.2 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "Camera.h"
#include "Config.h"
#include "DotDetectorLayer.h"
#include "ProcessingThread.h"
#include "Util.h"
#include "VideoFrameBuffer.h"
#include "WaggleDanceDetector.h"
#include "WaggleDanceExport.h"
#include "opencv2/opencv.hpp"
#include <boost/dll/runtime_symbol_info.hpp>
#include <boost/filesystem.hpp>
#include <tclap/CmdLine.h>
using namespace wdd;
// saves loaded, modified camera configs
static std::vector<CamConf> camConfs;
// saves the next unique camId
static std::size_t nextUniqueCamID = 0;
// format of config file:
// <camId> <GUID> <Arena.p1> <Arena.p2> <Arena.p3> <Arena.p4>
void loadCamConfigFileReadLine(std::string line)
{
const char* delimiter = " ";
std::size_t tokenNumber = 0;
std::size_t camId = 0;
char guid_str[64];
std::array<cv::Point2i, 4> arena;
//copy & convert to char *
char* string1 = strdup(line.c_str());
// parse the line
char* token = nullptr;
token = strtok(string1, delimiter);
int arenaPointNumber = 0;
while (token != nullptr) {
int px, py;
// camId
if (tokenNumber == 0) {
camId = atoi(token);
}
// guid
else if (tokenNumber == 1) {
strcpy(guid_str, token);
} else {
switch (tokenNumber % 2) {
// arena.pi.x
case 0:
px = atoi(token);
break;
// arena.pi.y
case 1:
py = atoi(token);
arena[arenaPointNumber++] = cv::Point2i(px, py);
break;
}
}
tokenNumber++;
token = strtok(nullptr, delimiter);
}
free(string1);
free(token);
if (tokenNumber != 10)
std::cerr << "Warning! cams.config file contains corrupted line with total tokenNumber: " << tokenNumber << std::endl;
struct CamConf c;
c.camId = camId;
strcpy(c.guid_str, guid_str);
c.arena = arena;
c.configured = true;
// save loaded CamConf to global vector
camConfs.push_back(c);
// keep track of loaded camIds and alter nextUniqueCamID accordingly
if (camId >= nextUniqueCamID)
nextUniqueCamID = camId + 1;
}
void loadCamConfigFile()
{
boost::filesystem::path path(getExeFullPath());
path /= CAM_CONF_PATH;
if (!fileExists(path.c_str())) {
// create empty file
std::fstream f;
f.open(path.c_str(), std::ios::out);
f << std::flush;
f.close();
}
std::string line;
std::ifstream camconfigfile;
camconfigfile.open(path.c_str());
if (camconfigfile.is_open()) {
while (getline(camconfigfile, line)) {
loadCamConfigFileReadLine(line);
}
camconfigfile.close();
} else {
std::cerr << "Error! Can not open cams.config file!" << std::endl;
exit(111);
}
}
void saveCamConfigFile()
{
boost::filesystem::path path(getExeFullPath());
path /= CAM_CONF_PATH;
FILE* camConfFile_ptr = fopen(path.c_str(), "w+");
for (auto it = camConfs.begin(); it != camConfs.end(); ++it) {
if (it->configured) {
fprintf(camConfFile_ptr, "%d %s ", it->camId, it->guid_str);
for (unsigned i = 0; i < 4; i++)
fprintf(camConfFile_ptr, "%d %d ", it->arena[i].x, it->arena[i].y);
fprintf(camConfFile_ptr, "\n");
}
}
fclose(camConfFile_ptr);
}
int main(int nargs, char** argv)
{
const std::string version = "1.3.0";
const std::string compiletime = __TIMESTAMP__;
printf("WaggleDanceDetection Version %s - compiled at %s\n\n",
version.c_str(), compiletime.c_str());
// define values potentially set by command line
double dd_min_potential;
int wdd_signal_min_cluster_size;
bool autoStartUp;
std::string videofile;
std::string dancePath;
try {
// Define the command line object.
TCLAP::CmdLine cmd("Command description message", ' ', version);
// Define a value argument and add it to the command line.
TCLAP::ValueArg<double> potArg("p", "potential", "Potential minimum value", false, 32888, "double");
cmd.add(potArg);
// Define a value argument and add it to the command line.
TCLAP::ValueArg<int> cluArg("c", "cluster", "Cluster minimum size", false, 6, "int");
cmd.add(cluArg);
// Define a switch and add it to the command line.
TCLAP::SwitchArg autoSwitch("a", "auto", "Selects automatically configured cam", false);
cmd.add(autoSwitch);
#if defined(TEST_MODE_ON)
// path to test video input file
TCLAP::ValueArg<std::string> testVidArg("t", "video", "path to video file", true, "", "string");
cmd.add(testVidArg);
// path to output of dance detection file
TCLAP::ValueArg<std::string> outputArg("o", "output", "path to result file", false, "", "string");
cmd.add(outputArg);
#endif
// Parse the args.
cmd.parse(nargs, argv);
// Get the value parsed by each arg.
dd_min_potential = potArg.getValue();
wdd_signal_min_cluster_size = cluArg.getValue();
autoStartUp = autoSwitch.getValue();
videofile = testVidArg.getValue();
dancePath = outputArg.getValue();
if (dancePath.empty()) {
boost::filesystem::path path(getExeFullPath());
dancePath = path.string();
}
} catch (TCLAP::ArgException& e) {
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
exit(1);
}
// WaggleDanceExport initialization
WaggleDanceExport::execRootExistChk();
// Query for number of connected cameras
size_t numCams = Camera::getNumCameras();
if (numCams == 0) {
printf("Error No PS3Eye cameras detected\n");
exit(-1);
}
std::cout << numCams << " Cameras detected." << std::endl;
std::vector<std::string> cameraIdentifiers;
// Query & temporaly save unique camera uuid
for (int i = 0; i < numCams; i++) {
cameraIdentifiers.push_back(Camera::getIdentifier(i));
}
// Load & store cams.config file
loadCamConfigFile();
// prepare container for camIds
std::vector<std::size_t> camIdsLaunch;
// merge data from file and current connected cameras
// compare guids connected to guids loaded from file
// - if match, camera has camId and arena properties -> configured=true
// - else it gets new camId -> configured=false
for (int i = 0; i < numCams; i++) {
bool foundMatch = false;
const std::string identifier = Camera::getIdentifier(i);
for (auto it = camConfs.begin(); it != camConfs.end(); ++it) {
if (strcmp(identifier.c_str(), it->guid_str) == 0) {
foundMatch = true;
camIdsLaunch.push_back(it->camId);
break;
}
}
// no match found -> new config
// find next camId
if (!foundMatch) {
struct CamConf c;
c.camId = nextUniqueCamID++;
camIdsLaunch.push_back(c.camId);
strcpy(c.guid_str, identifier.c_str());
c.arena[0] = cv::Point2i(0, 0);
c.arena[1] = cv::Point2i(639, 0);
c.arena[2] = cv::Point2i(639, 479);
c.arena[3] = cv::Point2i(0, 479);
c.configured = false;
camConfs.push_back(c);
}
}
// if autoStartUp flag, iterate camIdsLaunch and select first configured camId
int camIdUserSelect = -1;
if (autoStartUp) {
for (std::size_t i = 0; i < camIdsLaunch.size(); i++) {
std::size_t _camId = camIdsLaunch[i];
for (auto it = camConfs.begin(); it != camConfs.end(); ++it)
if (it->camId == _camId)
if (it->configured) {
camIdUserSelect = _camId;
break;
}
}
if (camIdUserSelect < 0)
std::cout << "\nWARNING! Could not autostart because no configrued camera present!\n\n";
}
if (camIdUserSelect < 0) {
// for all camIds pushed to launch retrieve information and push to user prompt
printf("CamID\tGUID\tconfigured?\n");
printf("***********************************\n");
for (std::size_t i = 0; i < camIdsLaunch.size(); i++) {
std::size_t _camId = camIdsLaunch[i];
CamConf* cc_ptr = nullptr;
for (auto it = camConfs.begin(); it != camConfs.end(); ++it)
if (it->camId == _camId)
cc_ptr = &(*it);
if (cc_ptr != nullptr)
printf("%d\t %s\t%s\n", cc_ptr->camId, cc_ptr->guid_str, cc_ptr->configured ? "true" : "false");
}
printf("\n\n");
// retrieve users camId choice
while (camIdUserSelect < 0) {
std::string in;
std::cout << " -> Please select camera id to start:" << std::endl;
std::getline(std::cin, in);
try {
std::size_t i_dec = static_cast<std::size_t>(std::stoi(in, nullptr));
bool found = false;
for (auto it = camIdsLaunch.begin(); it != camIdsLaunch.end(); ++it) {
if (*it == i_dec) {
std::cout << " -> " << i_dec << " selected!" << std::endl;
camIdUserSelect = i_dec;
found = true;
}
}
if (!found) {
std::cout << i_dec << " unavailable!" << std::endl;
}
} catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
}
}
}
// retrieve guid from camConfs according to camId
std::unique_ptr<ProcessingThread> pCam;
const std::string windowName = cameraIdentifiers[camIdUserSelect];
for (auto it = camConfs.begin(); it != camConfs.end(); ++it) {
if (it->camId == camIdUserSelect) {
printf(windowName.c_str(), "WaggleDanceDetector - CamID: %d", camIdUserSelect);
bool foundMatch = false;
for (int i = 0; i < numCams; i++) {
if (strcmp(cameraIdentifiers[i].c_str(), it->guid_str) == 0) {
foundMatch = true;
pCam = std::make_unique<ProcessingThread>(
windowName, cameraIdentifiers[i], camIdUserSelect, 320, 240, WDD_FRAME_RATE, *it,
dd_min_potential, wdd_signal_min_cluster_size, dancePath);
break;
}
}
std::cout << foundMatch << std::endl;
}
}
printf("Starting WaggleDanceDetector - CamID: %d\n", camIdUserSelect);
const CamConf* cc_ptr = pCam->getCamConfPtr();
if (!cc_ptr->configured) {
// run Setup mode first
pCam->setSetupModeOn(true);
pCam->StartCapture();
pCam->setSetupModeOn(false);
// update camConfs
for (auto it = camConfs.begin(); it != camConfs.end(); ++it) {
if (it->camId == cc_ptr->camId) {
camConfs.erase(it);
break;
}
}
camConfs.push_back(*cc_ptr);
saveCamConfigFile();
}
pCam->StartCapture();
return 0;
}