blob: 08409b2dcff3968991a8218b35c79a5821cf06c6 [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 Ostrowskibaa43692020-03-08 16:25:10 -07009#include "aos/time/time.h"
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080010#include "y2020/vision/sift/sift_generated.h"
Jim Ostrowski23eb6582020-03-04 23:15:32 -080011#include "y2020/vision/vision_generated.h"
12
13DEFINE_string(config, "config.json", "Path to the config file to use.");
14
15namespace frc971 {
16namespace vision {
17namespace {
18
19void ViewerMain() {
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080020 struct TargetData {
21 float x;
22 float y;
23 float radius;
24 };
25
26 std::map<int64_t, TargetData> target_data_map;
27
Jim Ostrowski23eb6582020-03-04 23:15:32 -080028 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
29 aos::configuration::ReadConfig(FLAGS_config);
30
31 aos::ShmEventLoop event_loop(&config.message());
32
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080033 event_loop.MakeWatcher(
34 "/camera", [&target_data_map](const CameraImage &image) {
Jim Ostrowskibaa43692020-03-08 16:25:10 -070035 // Create color image:
36 cv::Mat image_color_mat(cv::Size(image.cols(), image.rows()), CV_8UC2,
37 (void *)image.data()->data());
38 cv::Mat rgb_image(cv::Size(image.cols(), image.rows()), CV_8UC3);
39 cv::cvtColor(image_color_mat, rgb_image, CV_YUV2BGR_YUYV);
Jim Ostrowski23eb6582020-03-04 23:15:32 -080040
Jim Ostrowskibaa43692020-03-08 16:25:10 -070041 unsigned long timestamp = image.monotonic_timestamp_ns();
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080042 auto target_it = target_data_map.find(timestamp);
43 if (target_it != target_data_map.end()) {
44 float x = target_it->second.x;
45 float y = target_it->second.y;
46 float radius = target_it->second.radius;
Jim Ostrowskibaa43692020-03-08 16:25:10 -070047 cv::circle(rgb_image, cv::Point2f(x, y), radius,
48 cv::Scalar(0, 255, 0), 5);
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080049 }
Jim Ostrowskibaa43692020-03-08 16:25:10 -070050
51 cv::imshow("Display", rgb_image);
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080052 int keystroke = cv::waitKey(1);
Jim Ostrowskibaa43692020-03-08 16:25:10 -070053 if ((keystroke & 0xFF) == static_cast<int>('c')) {
54 // Convert again, to get clean image
55 cv::cvtColor(image_color_mat, rgb_image, CV_YUV2BGR_YUYV);
56 std::stringstream name;
57 name << "capture-" << aos::realtime_clock::now() << ".png";
58 cv::imwrite(name.str(), rgb_image);
59 LOG(INFO) << "Saved image file: " << name.str();
60 } else if ((keystroke & 0xFF) == static_cast<int>('q')) {
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080061 exit(0);
62 }
63 });
64
65 event_loop.MakeWatcher(
66 "/camera", [&target_data_map](const sift::ImageMatchResult &match) {
67 int64_t timestamp = match.image_monotonic_timestamp_ns();
68 if (match.camera_poses() != NULL && match.camera_poses()->size() > 0) {
69 LOG(INFO) << "Got match!\n";
70 TargetData target_data = {
71 match.camera_poses()->Get(0)->query_target_point_x(),
72 match.camera_poses()->Get(0)->query_target_point_y(),
73 match.camera_poses()->Get(0)->query_target_point_radius()};
74 target_data_map[timestamp] = target_data;
75 while (target_data_map.size() > 10u) {
76 target_data_map.erase(target_data_map.begin());
77 }
78 }
79 });
Jim Ostrowski23eb6582020-03-04 23:15:32 -080080
81 event_loop.Run();
82}
83
84} // namespace
85} // namespace vision
86} // namespace frc971
87
88// Quick and lightweight grayscale viewer for images
89int main(int argc, char **argv) {
90 aos::InitGoogle(&argc, &argv);
91 frc971::vision::ViewerMain();
92}