blob: cd087c7c3f15609297088908dd8c35dace59d49e [file] [log] [blame]
Jim Ostrowskic560cbe2020-03-07 00:29:30 -08001#include <map>
Jim Ostrowski23eb6582020-03-04 23:15:32 -08002#include <opencv2/calib3d.hpp>
3#include <opencv2/features2d.hpp>
4#include <opencv2/highgui/highgui.hpp>
5#include <opencv2/imgproc.hpp>
6
7#include "aos/events/shm_event_loop.h"
8#include "aos/init.h"
Jim Ostrowskic560cbe2020-03-07 00:29:30 -08009#include "y2020/vision/sift/sift_generated.h"
Jim Ostrowski23eb6582020-03-04 23:15:32 -080010#include "y2020/vision/vision_generated.h"
11
12DEFINE_string(config, "config.json", "Path to the config file to use.");
13
14namespace frc971 {
15namespace vision {
16namespace {
17
18void ViewerMain() {
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080019 struct TargetData {
20 float x;
21 float y;
22 float radius;
23 };
24
25 std::map<int64_t, TargetData> target_data_map;
26
Jim Ostrowski23eb6582020-03-04 23:15:32 -080027 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
28 aos::configuration::ReadConfig(FLAGS_config);
29
30 aos::ShmEventLoop event_loop(&config.message());
31
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080032 event_loop.MakeWatcher(
33 "/camera", [&target_data_map](const CameraImage &image) {
34 cv::Mat image_mat(image.rows(), image.cols(), CV_8U);
35 CHECK(image_mat.isContinuous());
36 const int number_pixels = image.rows() * image.cols();
37 for (int i = 0; i < number_pixels; ++i) {
38 reinterpret_cast<uint8_t *>(image_mat.data)[i] =
39 image.data()->data()[i * 2];
40 }
Jim Ostrowski23eb6582020-03-04 23:15:32 -080041
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080042 int64_t timestamp = image.monotonic_timestamp_ns();
43 auto target_it = target_data_map.find(timestamp);
44 if (target_it != target_data_map.end()) {
45 float x = target_it->second.x;
46 float y = target_it->second.y;
47 float radius = target_it->second.radius;
48 cv::circle(image_mat, cv::Point2f(x, y), radius, 255, 5);
49 } else {
50 LOG(INFO) << "Couldn't find timestamp match for timestamp: "
51 << timestamp;
52 }
53 cv::imshow("Display", image_mat);
54 int keystroke = cv::waitKey(1);
55 if ((keystroke & 0xFF) == static_cast<int>('q')) {
56 exit(0);
57 }
58 });
59
60 event_loop.MakeWatcher(
61 "/camera", [&target_data_map](const sift::ImageMatchResult &match) {
62 int64_t timestamp = match.image_monotonic_timestamp_ns();
63 if (match.camera_poses() != NULL && match.camera_poses()->size() > 0) {
64 LOG(INFO) << "Got match!\n";
65 TargetData target_data = {
66 match.camera_poses()->Get(0)->query_target_point_x(),
67 match.camera_poses()->Get(0)->query_target_point_y(),
68 match.camera_poses()->Get(0)->query_target_point_radius()};
69 target_data_map[timestamp] = target_data;
70 while (target_data_map.size() > 10u) {
71 target_data_map.erase(target_data_map.begin());
72 }
73 }
74 });
Jim Ostrowski23eb6582020-03-04 23:15:32 -080075
76 event_loop.Run();
77}
78
79} // namespace
80} // namespace vision
81} // namespace frc971
82
83// Quick and lightweight grayscale viewer for images
84int main(int argc, char **argv) {
85 aos::InitGoogle(&argc, &argv);
86 frc971::vision::ViewerMain();
87}