Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 1 | #include <map> |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 2 | #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 Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 9 | #include "y2020/vision/sift/sift_generated.h" |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 10 | #include "y2020/vision/vision_generated.h" |
| 11 | |
| 12 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
| 13 | |
| 14 | namespace frc971 { |
| 15 | namespace vision { |
| 16 | namespace { |
| 17 | |
| 18 | void ViewerMain() { |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 19 | struct TargetData { |
| 20 | float x; |
| 21 | float y; |
| 22 | float radius; |
| 23 | }; |
| 24 | |
| 25 | std::map<int64_t, TargetData> target_data_map; |
| 26 | |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 27 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 28 | aos::configuration::ReadConfig(FLAGS_config); |
| 29 | |
| 30 | aos::ShmEventLoop event_loop(&config.message()); |
| 31 | |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 32 | 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 Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 41 | |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 42 | 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 Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 75 | |
| 76 | event_loop.Run(); |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | } // namespace vision |
| 81 | } // namespace frc971 |
| 82 | |
| 83 | // Quick and lightweight grayscale viewer for images |
| 84 | int main(int argc, char **argv) { |
| 85 | aos::InitGoogle(&argc, &argv); |
| 86 | frc971::vision::ViewerMain(); |
| 87 | } |