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 | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 9 | #include "aos/time/time.h" |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 10 | #include "y2020/vision/sift/sift_generated.h" |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 11 | #include "y2020/vision/vision_generated.h" |
| 12 | |
| 13 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
Austin Schuh | 69b2f79 | 2020-03-15 14:31:55 -0700 | [diff] [blame^] | 14 | DEFINE_string(channel, "/camera", "Channel name for the image."); |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 15 | |
| 16 | namespace frc971 { |
| 17 | namespace vision { |
| 18 | namespace { |
| 19 | |
| 20 | void ViewerMain() { |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 21 | struct TargetData { |
| 22 | float x; |
| 23 | float y; |
| 24 | float radius; |
| 25 | }; |
| 26 | |
| 27 | std::map<int64_t, TargetData> target_data_map; |
| 28 | |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 29 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 30 | aos::configuration::ReadConfig(FLAGS_config); |
| 31 | |
| 32 | aos::ShmEventLoop event_loop(&config.message()); |
| 33 | |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 34 | event_loop.MakeWatcher( |
Austin Schuh | 69b2f79 | 2020-03-15 14:31:55 -0700 | [diff] [blame^] | 35 | FLAGS_channel, [&target_data_map](const CameraImage &image) { |
Jim Ostrowski | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 36 | // Create color image: |
| 37 | cv::Mat image_color_mat(cv::Size(image.cols(), image.rows()), CV_8UC2, |
| 38 | (void *)image.data()->data()); |
| 39 | cv::Mat rgb_image(cv::Size(image.cols(), image.rows()), CV_8UC3); |
| 40 | cv::cvtColor(image_color_mat, rgb_image, CV_YUV2BGR_YUYV); |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 41 | |
Jim Ostrowski | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 42 | unsigned long timestamp = image.monotonic_timestamp_ns(); |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 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; |
Jim Ostrowski | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 48 | cv::circle(rgb_image, cv::Point2f(x, y), radius, |
| 49 | cv::Scalar(0, 255, 0), 5); |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 50 | } |
Jim Ostrowski | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 51 | |
| 52 | cv::imshow("Display", rgb_image); |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 53 | int keystroke = cv::waitKey(1); |
Jim Ostrowski | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 54 | if ((keystroke & 0xFF) == static_cast<int>('c')) { |
| 55 | // Convert again, to get clean image |
| 56 | cv::cvtColor(image_color_mat, rgb_image, CV_YUV2BGR_YUYV); |
| 57 | std::stringstream name; |
| 58 | name << "capture-" << aos::realtime_clock::now() << ".png"; |
| 59 | cv::imwrite(name.str(), rgb_image); |
| 60 | LOG(INFO) << "Saved image file: " << name.str(); |
| 61 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 62 | exit(0); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | event_loop.MakeWatcher( |
Austin Schuh | 69b2f79 | 2020-03-15 14:31:55 -0700 | [diff] [blame^] | 67 | FLAGS_channel, [&target_data_map](const sift::ImageMatchResult &match) { |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 68 | int64_t timestamp = match.image_monotonic_timestamp_ns(); |
| 69 | if (match.camera_poses() != NULL && match.camera_poses()->size() > 0) { |
| 70 | LOG(INFO) << "Got match!\n"; |
| 71 | TargetData target_data = { |
| 72 | match.camera_poses()->Get(0)->query_target_point_x(), |
| 73 | match.camera_poses()->Get(0)->query_target_point_y(), |
| 74 | match.camera_poses()->Get(0)->query_target_point_radius()}; |
| 75 | target_data_map[timestamp] = target_data; |
| 76 | while (target_data_map.size() > 10u) { |
| 77 | target_data_map.erase(target_data_map.begin()); |
| 78 | } |
| 79 | } |
| 80 | }); |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 81 | |
| 82 | event_loop.Run(); |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | } // namespace vision |
| 87 | } // namespace frc971 |
| 88 | |
| 89 | // Quick and lightweight grayscale viewer for images |
| 90 | int main(int argc, char **argv) { |
| 91 | aos::InitGoogle(&argc, &argv); |
| 92 | frc971::vision::ViewerMain(); |
| 93 | } |