-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
255 lines (229 loc) · 7.29 KB
/
main.cpp
File metadata and controls
255 lines (229 loc) · 7.29 KB
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
#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <opencv2/opencv.hpp>
#include <csignal>
#include "spinnaker_stream.h"
#include "fisheye.h"
#include "homography.h"
#include <fstream>
#define DEFAULT_VIDEO_DEVICE "/dev/video16"
#define DEFAULT_IP "0.0.0.0"
#define DEFAULT_PORT 10086
#define DEFAULT_FPS 60
#define DEFAULT_DELAY_MS 0
#define DEFAULT_SCALE 1
bool capture_signal = false;
void run_spinnaker_stream(const char *videoDevice, const char *ip, int port, int fps, int delay_ms, const char *logger, const bool is_hmi, const bool is_p_hmi, const int scale)
{
std::cout << "[main] Starting to capture frames from the FLIR camera..." << std::endl;
capture_frames(videoDevice, ip, port, capture_signal, fps, delay_ms, logger, is_hmi, is_p_hmi, scale);
std::cout << "[main] Capture process finished" << std::endl;
}
void run_fc()
{
const Size boardSize(10, 7);
constexpr float squareSize = 0.025f;
const String filename = "data/*.jpg";
const Fisheye fisheye(boardSize, squareSize, &filename);
fisheye.save("fisheye_calibration.yaml");
}
void run_fu(const std::string &filename)
{
const Fisheye camera("fisheye_calibration.yaml");
Mat image = imread(filename);
camera.undistort(image, image);
imwrite("undistorted.jpg", image);
}
void run_hc()
{
const vector<Point2f> src{{1510, 2047}, {1560, 2047}, {1510, 797}, {1560, 797}};
vector<Point2f> dst;
FileStorage fs("homography_points.yaml", FileStorage::READ);
fs["points"] >> dst;
const Homography homography(&src, &dst);
homography.save("homography_calibration.yaml");
}
void signalHandler(int signum)
{
std::cout << "[main] Received signal " << signum << ", cleaning up resources..." << std::endl;
capture_signal = true;
}
std::unordered_map<std::string, std::string> parseArguments(int argc, char *argv[])
{
std::unordered_map<std::string, std::string> args;
for (int i = 1; i < argc; ++i)
{
std::string key = argv[i];
if (key.rfind('-', 0) == 0)
{
if (i + 1 < argc && argv[i + 1][0] != '-')
{
args[key] = argv[i + 1];
++i;
}
else
{
args[key] = "";
}
}
}
return args;
}
int main(int argc, char *argv[])
{
auto args = parseArguments(argc, argv);
if (args.find("-h") != args.end())
{
std::cout << "[main] Usage: " << argv[0] << " [-d <video_device>] [-s [-ip <ip>] [-p <port>] [-hmi]]" << std::endl;
std::cout << "[main] Options:" << std::endl;
std::cout << "[main] -d <video_device> Specify the video device to capture frames from (default: /dev/video16)" << std::endl;
std::cout << "[main] -fps <fps> Specify the frames per second (default: 60)" << std::endl;
std::cout << "[main] -scale <scale> Specify the scale of the frame size (default: 1)" << std::endl;
std::cout << "[main] -delay <ms> Specify the delay in milliseconds for video output (default: 0)" << std::endl;
std::cout << "[main] -s Add the sensor data to the video stream" << std::endl;
std::cout << "[main] -hmi Add HMI to the stream" << std::endl;
std::cout << "[main] -p_hmi Add Prediction HMI to the stream" << std::endl;
std::cout << "[main] -ip <ip> Specify the IP address to stream frames to (default: 0.0.0.0)" << std::endl;
std::cout << "[main] -p <port> Specify the port to stream frames to (default: 10086)" << std::endl;
std::cout << "[main] -log <logger_file> Specify the logger file to log sensor data" << std::endl;
std::cout << "[main] -fc Run fisheye calibration" << std::endl;
std::cout << "[main] -fu <image> Run fisheye undistortion on the specified image" << std::endl;
std::cout << "[main] -hc Run homography calibration" << std::endl;
return 0;
}
if (args.find("-fc") != args.end())
{
run_fc();
return 0;
}
if (args.find("-fu") != args.end())
{
const std::string filename = args["-fu"];
if (filename.empty())
{
std::cerr << "[main] Usage: " << argv[0] << " -fu <image>" << std::endl;
return -1;
}
run_fu(args["-fu"]);
return 0;
}
if (args.find("-hc") != args.end())
{
run_hc();
return 0;
}
const char *videoDevice;
if (args.find("-d") != args.end())
{
videoDevice = args["-d"].c_str();
}
else
{
videoDevice = DEFAULT_VIDEO_DEVICE;
}
std::cout << "[main] Using video device: " << videoDevice << std::endl;
const char *ip;
int port;
if (args.find("-s") != args.end())
{
if (args.find("-ip") != args.end())
{
ip = args["-ip"].c_str();
}
else
{
ip = DEFAULT_IP;
}
if (args.find("-p") != args.end())
{
port = std::stoi(args["-p"]);
}
else
{
port = DEFAULT_PORT;
}
std::cout << "[main] Using IP: " << ip << " and port: " << port << "/" << port + 1 << std::endl;
}
else
{
ip = "0.0.0.0";
port = -1;
}
int fps;
if (args.find("-fps") != args.end())
{
fps = std::stoi(args["-fps"]);
}
else
{
fps = DEFAULT_FPS;
}
std::cout << "[main] Using FPS: " << fps << std::endl;
int delay_ms;
if (args.find("-delay") != args.end())
{
delay_ms = std::stoi(args["-delay"]);
}
else
{
delay_ms = DEFAULT_DELAY_MS;
}
std::cout << "[main] Using delay: " << delay_ms << "ms" << std::endl;
std::string logger_filename;
const char *logger;
if (args.find("-log") != args.end())
{
const auto file_name = args["-log"];
auto now = std::time(nullptr);
auto tm = *std::localtime(&now);
std::ostringstream timestamp;
timestamp << std::put_time(&tm, "%Y%m%d_%H%M%S");
logger_filename = file_name + "_" + timestamp.str() + ".csv";
logger = logger_filename.c_str();
std::cout << "[main] Using logger file: " << logger << std::endl;
}
else
{
logger = nullptr;
}
bool is_hmi;
bool is_p_hmi;
if (args.find("-hmi") != args.end())
{
is_hmi = true;
std::cout << "[main] HMI mode enabled" << std::endl;
}
else
{
is_hmi = false;
std::cout << "[main] HMI mode disabled" << std::endl;
}
if (args.find("-p_hmi") != args.end())
{
is_p_hmi = true;
std::cout << "[main] P-HMI mode enabled" << std::endl;
}
else
{
is_p_hmi = false;
std::cout << "[main] P-HMI mode disabled" << std::endl;
}
int scale;
if (args.find("-scale") != args.end())
{
scale = std::stoi(args["-scale"]);
}
else
{
scale = DEFAULT_SCALE;
}
std::cout << "[main] Using scale: " << scale << std::endl;
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
run_spinnaker_stream(videoDevice, ip, port, fps, delay_ms, logger, is_hmi, is_p_hmi, scale);
return 0;
}