blob: f7c9b750599ea42335b8f75af57490e93e9d88ad [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.");
Austin Schuh69b2f792020-03-15 14:31:55 -070014DEFINE_string(channel, "/camera", "Channel name for the image.");
Jim Ostrowski23eb6582020-03-04 23:15:32 -080015
16namespace frc971 {
17namespace vision {
18namespace {
19
20void ViewerMain() {
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080021 struct TargetData {
22 float x;
23 float y;
24 float radius;
25 };
26
27 std::map<int64_t, TargetData> target_data_map;
28
Jim Ostrowski23eb6582020-03-04 23:15:32 -080029 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
30 aos::configuration::ReadConfig(FLAGS_config);
31
32 aos::ShmEventLoop event_loop(&config.message());
33
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080034 event_loop.MakeWatcher(
Austin Schuh69b2f792020-03-15 14:31:55 -070035 FLAGS_channel, [&target_data_map](const CameraImage &image) {
Jim Ostrowskibaa43692020-03-08 16:25:10 -070036 // 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 Ostrowski23eb6582020-03-04 23:15:32 -080041
Jim Ostrowskibaa43692020-03-08 16:25:10 -070042 unsigned long timestamp = image.monotonic_timestamp_ns();
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080043 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 Ostrowskibaa43692020-03-08 16:25:10 -070048 cv::circle(rgb_image, cv::Point2f(x, y), radius,
49 cv::Scalar(0, 255, 0), 5);
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080050 }
Jim Ostrowskibaa43692020-03-08 16:25:10 -070051
52 cv::imshow("Display", rgb_image);
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080053 int keystroke = cv::waitKey(1);
Jim Ostrowskibaa43692020-03-08 16:25:10 -070054 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 Ostrowskic560cbe2020-03-07 00:29:30 -080062 exit(0);
63 }
64 });
65
66 event_loop.MakeWatcher(
Austin Schuh69b2f792020-03-15 14:31:55 -070067 FLAGS_channel, [&target_data_map](const sift::ImageMatchResult &match) {
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080068 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 Ostrowski23eb6582020-03-04 23:15:32 -080081
82 event_loop.Run();
83}
84
85} // namespace
86} // namespace vision
87} // namespace frc971
88
89// Quick and lightweight grayscale viewer for images
90int main(int argc, char **argv) {
91 aos::InitGoogle(&argc, &argv);
92 frc971::vision::ViewerMain();
93}