forked from firepick1/FireSight
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FireSight.cpp
261 lines (236 loc) · 7.99 KB
/
FireSight.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
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include "FireLog.h"
#include "FireSight.hpp"
#include "version.h"
#include "jo_util.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "jansson.h"
using namespace cv;
using namespace std;
using namespace firesight;
typedef enum{UI_STILL, UI_VIDEO} UIMode;
static void help() {
cout << "FireSight image processing pipeline v" << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH << endl;
cout << "Copyright 2014, Karl Lew, Simon Fojtu" << endl;
cout << "https://github.com/firepick1/FireSight/wiki" << endl;
cout << "OpenCV " CV_VERSION << " (" << FIRESIGHT_PLATFORM_BITS << "-bit)" << endl;
cout << endl;
cout << "Example:" << endl;
cout << " firesight -p json/pipeline0.json -i img/cam.jpg -o target/output.jpg" << endl;
cout << " firesight -p json/pipeline1.json " << endl;
cout << " firesight -p json/pipeline2.json " << endl;
cout << endl;
cout << "All FireSight parameters are optional." << endl;
cout << endl;
cout << "Input parameters:" << endl;
cout << " -i input-image-file" << endl;
cout << " File path of pipeline input image" << endl;
cout << " -video " << endl;
cout << " Use video for pipeline input" << endl;
cout << endl;
cout << "Transformation parameters:" << endl;
cout << " -Dvar=value" << endl;
cout << " Define pipeline parameter value" << endl;
cout << " -ji JSON-model-indent" << endl;
cout << " Specify 0 for compact JSON output of model" << endl;
cout << " -o output-image-file" << endl;
cout << " File for saving pipeline image " << endl;
cout << " -p JSON-pipeline-file" << endl;
cout << " JSON pipeline specification file. If omitted, input and output images must be specified." << endl;
cout << endl;
cout << "Diagnostic parameters:" << endl;
cout << " -opencv " << endl;
cout << " OpenCV version for FireSight test compatibility" << endl;
cout << " -debug " << endl;
cout << " Start logging at DEBUG log level" << endl;
cout << " -error " << endl;
cout << " Start logging at ERROR log level" << endl;
cout << " -time " << endl;
cout << " Time multiple executions of pipeline iterations and return average" << endl;
cout << " -trace " << endl;
cout << " Start logging at TRACE log level" << endl;
cout << " -warn " << endl;
cout << " Start logging at WARN log level" << endl;
}
bool parseArgs(int argc, char *argv[],
string &pipelinePath, char *&imagePath, char * &outputPath, UIMode &uimode, ArgMap &argMap, bool &isTime, int &jsonIndent)
{
uimode = UI_STILL;
isTime = false;
firelog_level(FIRELOG_INFO);
if (argc <= 1) {
return false;
}
for (int i = 1; i < argc; i++) {
if (argv[i][0] == 0) {
// empty argument
} else if (strcmp("-opencv",argv[i]) == 0) {
cout << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << endl;
exit(0);
} else if (strcmp("-p",argv[i]) == 0) {
if (i+1>=argc) {
LOGERROR("expected pipeline path after -p");
exit(-1);
}
pipelinePath = argv[++i];
LOGTRACE1("parseArgs(-p) \"%s\" is JSON pipeline path", pipelinePath.c_str());
} else if (strcmp("-ji",argv[i]) == 0) {
if (i+1>=argc) {
LOGERROR("expected JSON indent after -ji");
exit(-1);
}
jsonIndent = atoi(argv[++i]);
LOGTRACE1("parseArgs(-ji) JSON indent:%d", jsonIndent);
} else if (strcmp("-o",argv[i]) == 0) {
if (i+1>=argc) {
LOGERROR("expected output path after -o");
exit(-1);
}
outputPath = argv[++i];
LOGTRACE1("parseArgs(-o) \"%s\" is output image path", outputPath);
} else if (strcmp("-time",argv[i]) == 0) {
isTime = true;
} else if (strncmp("-D",argv[i],2) == 0) {
char * pEq = strchr(argv[i],'=');
if (!pEq || (pEq-argv[i])<=2) {
LOGERROR("expected argName=argValue pair after -D");
exit(-1);
}
*pEq = 0;
char *pName = argv[i] + 2;
char *pVal = pEq + 1;
argMap[pName] = pVal;
LOGTRACE2("parseArgs(-D) argMap[%s]=\"%s\"", pName, pVal );
*pEq = '=';
} else if (strcmp("-i",argv[i]) == 0) {
if (i+1>=argc) {
LOGERROR("expected image path after -i");
exit(-1);
}
imagePath = argv[++i];
LOGTRACE1("parseArgs(-i) \"%s\" is input image path", imagePath);
} else if (strcmp("-video", argv[i]) == 0) {
uimode = UI_VIDEO;
LOGTRACE("parseArgs(-video) UI_VIDEO user interface selected");
} else if (strcmp("-warn", argv[i]) == 0) {
firelog_level(FIRELOG_WARN);
} else if (strcmp("-error", argv[i]) == 0) {
firelog_level(FIRELOG_ERROR);
} else if (strcmp("-info", argv[i]) == 0) {
firelog_level(FIRELOG_INFO);
} else if (strcmp("-debug", argv[i]) == 0) {
firelog_level(FIRELOG_DEBUG);
} else if (strcmp("-trace", argv[i]) == 0) {
firelog_level(FIRELOG_TRACE);
} else {
LOGERROR1("unknown firesight argument: '%s'", argv[i]);
return false;
}
}
return true;
}
/**
* Single image example of FireSight lib_firesight library use
*/
static int uiStill(const char * pipelinePath, Mat &image, ArgMap &argMap, bool isTime, int jsonIndent) {
Pipeline pipeline(pipelinePath, Pipeline::PATH);
json_t *pModel = pipeline.process(image, argMap);
if (isTime) {
long long tickStart = cvGetTickCount();
//cout << "cvGetTickCount()" << cvGetTickCount() << endl;
//cout << "tickStart" << tickStart << endl;
int iterations = 100;
for (int i=0; i < iterations; i++) {
json_decref(pModel);
pModel = pipeline.process(image, argMap);
}
float ticksElapsed = cvGetTickCount() - tickStart;
//cout << "ticksElapsed:" << ticksElapsed << endl;
float msElapsed = ticksElapsed/cvGetTickFrequency()*1E-3;
//cout << "msElapsed:" << msElapsed << endl;
float msIter = msElapsed/iterations;
//cout << "msIter:" << msIter << endl;
LOGINFO2("timed %d iterations with an average of %.1fms per iteration", iterations, msIter);
}
// Print out returned model
char *pModelStr = json_dumps(pModel, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_INDENT(jsonIndent));
cout << pModelStr << endl;
free(pModelStr);
// Free model
json_decref(pModel);
return 0;
}
/**
* Video capture example of FireSight lib_firesight library use
*/
static int uiVideo(const char * pipelinePath, ArgMap &argMap) {
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) { // check if we succeeded
LOGERROR("Could not open camera");
exit(-1);
}
namedWindow("image",1);
Pipeline pipeline(pipelinePath, Pipeline::PATH);
for(;;) {
Mat frame;
cap >> frame; // get a new frame from camera
json_t *pModel = pipeline.process(frame, argMap);
// Display pipeline output
imshow("image", frame);
if(waitKey(30) >= 0) break;
// Free model
json_decref(pModel);
}
return 0;
}
int main(int argc, char *argv[])
{
UIMode uimode;
string pipelinePath;
char * imagePath = NULL;
char * outputPath = NULL;
ArgMap argMap;
bool isTime;
int jsonIndent = 2;
bool argsOk = parseArgs(argc, argv, pipelinePath, imagePath, outputPath, uimode, argMap, isTime, jsonIndent);
if (!argsOk) {
help();
exit(-1);
}
Mat image;
if (imagePath) {
LOGTRACE1("Reading image: %s", imagePath);
image = imread(imagePath);
if (!image.data) {
LOGERROR1("main() imread(%s) failed", imagePath);
exit(-1);
}
} else {
LOGDEBUG("No image specified.");
}
switch (uimode) {
case UI_STILL:
uiStill(pipelinePath.c_str(), image, argMap, isTime, jsonIndent);
break;
case UI_VIDEO:
uiVideo(pipelinePath.c_str(), argMap);
break;
default:
LOGERROR("Unknown UI mode");
exit(-1);
}
if (outputPath) {
if (!imwrite(outputPath, image)) {
LOGERROR1("Could not write image to: %s", outputPath);
exit(-1);
}
LOGTRACE1("Image written to: %s", outputPath);
}
return 0;
}