-
Notifications
You must be signed in to change notification settings - Fork 0
/
apriltag_detect.cpp
80 lines (73 loc) · 2.24 KB
/
apriltag_detect.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
#include <chrono>
#include "core/libcamera_app.hpp"
#include "core/options.hpp"
#include <libcamera/stream.h>
using namespace std::placeholders;
using Stream = libcamera::Stream;
LibcameraApp app;
Stream *stream_;
uint64_t timeOut;
static LibcameraApp *
startCamera() {
Options *options = app.GetOptions();
char arg0[6], *argv[1];
sprintf(arg0,"hello"); argv[0] = arg0;
if( !options->Parse(1, argv) ) throw std::runtime_error("impossible error.");
// 1640x1232 is the camera resolution and it can see these
// tags (22x22mm) at 1.3m
options->lores_width = 1640; //640; //64;
options->lores_height = 1232; //480; //64;
options->post_process_file = "apriltag.json";
timeOut = 10000;
app.OpenCamera();
app.ConfigureViewfinder();
app.StartCamera();
return &app;
}
// The main event loop for the application.
static void
event_loop(LibcameraApp &app) {
auto start_time = std::chrono::high_resolution_clock::now();
StreamInfo info;
stream_ = app.LoresStream(&info);
for (unsigned int count = 0; ; count++) {
LibcameraApp::Msg msg = app.Wait();
if (msg.type == LibcameraApp::MsgType::Timeout) {
LOG_ERROR("ERROR: Device timeout detected, attempting a restart!!!");
app.StopCamera();
app.StartCamera();
continue;
}
if (msg.type == LibcameraApp::MsgType::Quit)
return;
if (msg.type != LibcameraApp::MsgType::RequestComplete)
throw std::runtime_error("unrecognised message!");
LOG(2, "Viewfinder frame " << count);
auto now = std::chrono::high_resolution_clock::now();
if (timeOut && now - start_time > std::chrono::milliseconds(timeOut))
return;
CompletedRequestPtr &completed_request = std::get<CompletedRequestPtr>(msg.payload);
for(int i=0; i<4; i++) {
int x = -1, y = -1;
char xvar[4], yvar[4];
sprintf(xvar,"x%d",i);
completed_request->post_process_metadata.Get(xvar,x);
if( x == -1 ) continue;
sprintf(yvar,"y%d",i);
completed_request->post_process_metadata.Get(yvar,y);
printf("tag-%d: %d/%d\n",i,x,y);
}
app.ShowPreview(completed_request, stream_);
}
}
int
main(int argc, char *argv[]) {
try {
LibcameraApp *appPtr = startCamera();
event_loop(*appPtr);
} catch (std::exception const &e) {
LOG_ERROR("ERROR: *** " << e.what() << " ***");
return -1;
}
return 0;
}