Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 1 | #include <opencv2/calib3d.hpp> |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 2 | #include <opencv2/features2d.hpp> |
| 3 | #include <opencv2/imgproc.hpp> |
| 4 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 5 | #include "aos/events/shm_event_loop.h" |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 6 | #include "aos/flatbuffer_merge.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 7 | #include "aos/init.h" |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 8 | #include "aos/network/team_number.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 9 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 10 | #include "y2020/vision/sift/sift971.h" |
| 11 | #include "y2020/vision/sift/sift_generated.h" |
| 12 | #include "y2020/vision/sift/sift_training_generated.h" |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 13 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 14 | #include "y2020/vision/v4l2_reader.h" |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 15 | #include "y2020/vision/vision_generated.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 16 | |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 17 | // config used to allow running camera_reader independently. E.g., |
| 18 | // bazel run //y2020/vision:camera_reader -- --config y2020/config.json |
| 19 | // --override_hostname pi-7971-1 --ignore_timestamps true |
| 20 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
| 21 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 22 | namespace frc971 { |
| 23 | namespace vision { |
| 24 | namespace { |
| 25 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 26 | class CameraReader { |
| 27 | public: |
| 28 | CameraReader(aos::EventLoop *event_loop, |
| 29 | const sift::TrainingData *training_data, V4L2Reader *reader, |
| 30 | cv::FlannBasedMatcher *matcher) |
| 31 | : event_loop_(event_loop), |
| 32 | training_data_(training_data), |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 33 | camera_calibration_(FindCameraCalibration()), |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 34 | reader_(reader), |
| 35 | matcher_(matcher), |
| 36 | image_sender_(event_loop->MakeSender<CameraImage>("/camera")), |
| 37 | result_sender_( |
| 38 | event_loop->MakeSender<sift::ImageMatchResult>("/camera")), |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 39 | detailed_result_sender_( |
| 40 | event_loop->MakeSender<sift::ImageMatchResult>("/camera/detailed")), |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 41 | read_image_timer_(event_loop->AddTimer([this]() { |
| 42 | ReadImage(); |
| 43 | read_image_timer_->Setup(event_loop_->monotonic_now()); |
| 44 | })) { |
| 45 | CopyTrainingFeatures(); |
| 46 | // Technically we don't need to do this, but doing it now avoids the first |
| 47 | // match attempt being slow. |
| 48 | matcher_->train(); |
| 49 | |
| 50 | event_loop->OnRun( |
| 51 | [this]() { read_image_timer_->Setup(event_loop_->monotonic_now()); }); |
| 52 | } |
| 53 | |
| 54 | private: |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 55 | const sift::CameraCalibration *FindCameraCalibration() const; |
| 56 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 57 | // Copies the information from training_data_ into matcher_. |
| 58 | void CopyTrainingFeatures(); |
| 59 | // Processes an image (including sending the results). |
| 60 | void ProcessImage(const CameraImage &image); |
| 61 | // Reads an image, and then performs all of our processing on it. |
| 62 | void ReadImage(); |
| 63 | |
| 64 | flatbuffers::Offset< |
| 65 | flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 66 | PackImageMatches(flatbuffers::FlatBufferBuilder *fbb, |
| 67 | const std::vector<std::vector<cv::DMatch>> &matches); |
| 68 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 69 | PackFeatures(flatbuffers::FlatBufferBuilder *fbb, |
| 70 | const std::vector<cv::KeyPoint> &keypoints, |
| 71 | const cv::Mat &descriptors); |
| 72 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 73 | void SendImageMatchResult(const CameraImage &image, |
| 74 | const std::vector<cv::KeyPoint> &keypoints, |
| 75 | const cv::Mat &descriptors, |
| 76 | const std::vector<std::vector<cv::DMatch>> &matches, |
| 77 | const std::vector<cv::Mat> &camera_target_list, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 78 | const std::vector<cv::Mat> &field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 79 | const std::vector<cv::Point2f> &target_point_vector, |
| 80 | const std::vector<float> &target_radius_vector, |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 81 | aos::Sender<sift::ImageMatchResult> *result_sender, |
| 82 | bool send_details); |
| 83 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 84 | // Returns the 2D (image) location for the specified training feature. |
| 85 | cv::Point2f Training2dPoint(int training_image_index, |
| 86 | int feature_index) const { |
| 87 | const float x = training_data_->images() |
| 88 | ->Get(training_image_index) |
| 89 | ->features() |
| 90 | ->Get(feature_index) |
| 91 | ->x(); |
| 92 | const float y = training_data_->images() |
| 93 | ->Get(training_image_index) |
| 94 | ->features() |
| 95 | ->Get(feature_index) |
| 96 | ->y(); |
| 97 | return cv::Point2f(x, y); |
| 98 | } |
| 99 | |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 100 | // Returns the 3D location for the specified training feature. |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 101 | cv::Point3f Training3dPoint(int training_image_index, |
| 102 | int feature_index) const { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 103 | const sift::KeypointFieldLocation *const location = |
| 104 | training_data_->images() |
| 105 | ->Get(training_image_index) |
| 106 | ->features() |
| 107 | ->Get(feature_index) |
| 108 | ->field_location(); |
| 109 | return cv::Point3f(location->x(), location->y(), location->z()); |
| 110 | } |
| 111 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 112 | const sift::TransformationMatrix *FieldToTarget(int training_image_index) { |
| 113 | return training_data_->images() |
| 114 | ->Get(training_image_index) |
| 115 | ->field_to_target(); |
| 116 | } |
| 117 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 118 | void TargetLocation(int training_image_index, cv::Point2f &target_location, |
| 119 | float &target_radius) { |
| 120 | target_location.x = |
| 121 | training_data_->images()->Get(training_image_index)->target_point_x(); |
| 122 | target_location.y = |
| 123 | training_data_->images()->Get(training_image_index)->target_point_y(); |
| 124 | target_radius = training_data_->images() |
| 125 | ->Get(training_image_index) |
| 126 | ->target_point_radius(); |
| 127 | } |
| 128 | |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 129 | int number_training_images() const { |
| 130 | return training_data_->images()->size(); |
| 131 | } |
| 132 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 133 | cv::Mat CameraIntrinsics() const { |
| 134 | const cv::Mat result(3, 3, CV_32F, |
| 135 | const_cast<void *>(static_cast<const void *>( |
| 136 | camera_calibration_->intrinsics()->data()))); |
| 137 | CHECK_EQ(result.total(), camera_calibration_->intrinsics()->size()); |
| 138 | return result; |
| 139 | } |
| 140 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 141 | cv::Mat CameraDistCoeffs() const { |
| 142 | const cv::Mat result(5, 1, CV_32F, |
| 143 | const_cast<void *>(static_cast<const void *>( |
| 144 | camera_calibration_->dist_coeffs()->data()))); |
| 145 | CHECK_EQ(result.total(), camera_calibration_->dist_coeffs()->size()); |
| 146 | return result; |
| 147 | } |
| 148 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 149 | aos::EventLoop *const event_loop_; |
| 150 | const sift::TrainingData *const training_data_; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 151 | const sift::CameraCalibration *const camera_calibration_; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 152 | V4L2Reader *const reader_; |
| 153 | cv::FlannBasedMatcher *const matcher_; |
| 154 | aos::Sender<CameraImage> image_sender_; |
| 155 | aos::Sender<sift::ImageMatchResult> result_sender_; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 156 | aos::Sender<sift::ImageMatchResult> detailed_result_sender_; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 157 | // We schedule this immediately to read an image. Having it on a timer means |
| 158 | // other things can run on the event loop in between. |
| 159 | aos::TimerHandler *const read_image_timer_; |
| 160 | |
| 161 | const std::unique_ptr<frc971::vision::SIFT971_Impl> sift_{ |
| 162 | new frc971::vision::SIFT971_Impl()}; |
| 163 | }; |
| 164 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 165 | const sift::CameraCalibration *CameraReader::FindCameraCalibration() const { |
| 166 | const std::string_view node_name = event_loop_->node()->name()->string_view(); |
| 167 | const int team_number = aos::network::GetTeamNumber(); |
| 168 | for (const sift::CameraCalibration *candidate : |
| 169 | *training_data_->camera_calibrations()) { |
| 170 | if (candidate->node_name()->string_view() != node_name) { |
| 171 | continue; |
| 172 | } |
| 173 | if (candidate->team_number() != team_number) { |
| 174 | continue; |
| 175 | } |
| 176 | return candidate; |
| 177 | } |
| 178 | LOG(FATAL) << ": Failed to find camera calibration for " << node_name |
| 179 | << " on " << team_number; |
| 180 | } |
| 181 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 182 | void CameraReader::CopyTrainingFeatures() { |
| 183 | for (const sift::TrainingImage *training_image : *training_data_->images()) { |
| 184 | cv::Mat features(training_image->features()->size(), 128, CV_32F); |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 185 | for (size_t i = 0; i < training_image->features()->size(); ++i) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 186 | const sift::Feature *feature_table = training_image->features()->Get(i); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 187 | |
| 188 | // We don't need this information right now, but make sure it's here to |
| 189 | // avoid crashes that only occur when specific features are matched. |
| 190 | CHECK(feature_table->has_field_location()); |
| 191 | |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 192 | const flatbuffers::Vector<uint8_t> *const descriptor = |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 193 | feature_table->descriptor(); |
| 194 | CHECK_EQ(descriptor->size(), 128u) << ": Unsupported feature size"; |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 195 | const auto in_mat = cv::Mat( |
| 196 | 1, descriptor->size(), CV_8U, |
| 197 | const_cast<void *>(static_cast<const void *>(descriptor->data()))); |
| 198 | const auto out_mat = features(cv::Range(i, i + 1), cv::Range(0, 128)); |
| 199 | in_mat.convertTo(out_mat, CV_32F); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 200 | } |
| 201 | matcher_->add(features); |
| 202 | } |
| 203 | } |
| 204 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 205 | void CameraReader::SendImageMatchResult( |
| 206 | const CameraImage &image, const std::vector<cv::KeyPoint> &keypoints, |
| 207 | const cv::Mat &descriptors, |
| 208 | const std::vector<std::vector<cv::DMatch>> &matches, |
| 209 | const std::vector<cv::Mat> &camera_target_list, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 210 | const std::vector<cv::Mat> &field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 211 | const std::vector<cv::Point2f> &target_point_vector, |
| 212 | const std::vector<float> &target_radius_vector, |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 213 | aos::Sender<sift::ImageMatchResult> *result_sender, bool send_details) { |
| 214 | auto builder = result_sender->MakeBuilder(); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 215 | const auto camera_calibration_offset = |
| 216 | aos::CopyFlatBuffer(camera_calibration_, builder.fbb()); |
| 217 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 218 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 219 | features_offset; |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 220 | flatbuffers::Offset< |
| 221 | flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 222 | image_matches_offset; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 223 | if (send_details) { |
| 224 | features_offset = PackFeatures(builder.fbb(), keypoints, descriptors); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 225 | image_matches_offset = PackImageMatches(builder.fbb(), matches); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 228 | std::vector<flatbuffers::Offset<sift::CameraPose>> camera_poses; |
| 229 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 230 | CHECK_EQ(camera_target_list.size(), field_camera_list.size()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 231 | for (size_t i = 0; i < camera_target_list.size(); ++i) { |
| 232 | cv::Mat camera_target = camera_target_list[i]; |
| 233 | CHECK(camera_target.isContinuous()); |
| 234 | const auto data_offset = builder.fbb()->CreateVector<float>( |
| 235 | reinterpret_cast<float *>(camera_target.data), camera_target.total()); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 236 | const flatbuffers::Offset<sift::TransformationMatrix> transform_offset = |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 237 | sift::CreateTransformationMatrix(*builder.fbb(), data_offset); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 238 | |
| 239 | cv::Mat field_camera = field_camera_list[i]; |
| 240 | CHECK(field_camera.isContinuous()); |
| 241 | const auto fc_data_offset = builder.fbb()->CreateVector<float>( |
| 242 | reinterpret_cast<float *>(field_camera.data), field_camera.total()); |
| 243 | const flatbuffers::Offset<sift::TransformationMatrix> fc_transform_offset = |
| 244 | sift::CreateTransformationMatrix(*builder.fbb(), fc_data_offset); |
| 245 | |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 246 | const flatbuffers::Offset<sift::TransformationMatrix> |
| 247 | field_to_target_offset = |
| 248 | aos::CopyFlatBuffer(FieldToTarget(i), builder.fbb()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 249 | |
| 250 | sift::CameraPose::Builder pose_builder(*builder.fbb()); |
| 251 | pose_builder.add_camera_to_target(transform_offset); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 252 | pose_builder.add_field_to_camera(fc_transform_offset); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 253 | pose_builder.add_field_to_target(field_to_target_offset); |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 254 | pose_builder.add_query_target_point_x(target_point_vector[i].x); |
| 255 | pose_builder.add_query_target_point_y(target_point_vector[i].y); |
| 256 | pose_builder.add_query_target_point_radius(target_radius_vector[i]); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 257 | camera_poses.emplace_back(pose_builder.Finish()); |
| 258 | } |
| 259 | const auto camera_poses_offset = builder.fbb()->CreateVector(camera_poses); |
| 260 | |
| 261 | sift::ImageMatchResult::Builder result_builder(*builder.fbb()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 262 | result_builder.add_camera_poses(camera_poses_offset); |
| 263 | if (send_details) { |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 264 | result_builder.add_image_matches(image_matches_offset); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 265 | result_builder.add_features(features_offset); |
| 266 | } |
| 267 | result_builder.add_image_monotonic_timestamp_ns( |
| 268 | image.monotonic_timestamp_ns()); |
| 269 | result_builder.add_camera_calibration(camera_calibration_offset); |
| 270 | |
| 271 | // TODO<Jim>: Need to add target point computed from matches and |
| 272 | // mapped by homography |
| 273 | builder.Send(result_builder.Finish()); |
| 274 | } |
| 275 | |
| 276 | void CameraReader::ProcessImage(const CameraImage &image) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 277 | // First, we need to extract the brightness information. This can't really be |
| 278 | // fused into the beginning of the SIFT algorithm because the algorithm needs |
| 279 | // to look at the base image directly. It also only takes 2ms on our images. |
| 280 | // This is converting from YUYV to a grayscale image. |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 281 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 282 | CHECK(image_mat.isContinuous()); |
| 283 | const int number_pixels = image.rows() * image.cols(); |
| 284 | for (int i = 0; i < number_pixels; ++i) { |
| 285 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 286 | image.data()->data()[i * 2]; |
| 287 | } |
| 288 | |
| 289 | // Next, grab the features from the image. |
| 290 | std::vector<cv::KeyPoint> keypoints; |
| 291 | cv::Mat descriptors; |
| 292 | sift_->detectAndCompute(image_mat, cv::noArray(), keypoints, descriptors); |
| 293 | |
| 294 | // Then, match those features against our training data. |
| 295 | std::vector<std::vector<cv::DMatch>> matches; |
| 296 | matcher_->knnMatch(/* queryDescriptors */ descriptors, matches, /* k */ 2); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 297 | |
| 298 | struct PerImageMatches { |
| 299 | std::vector<const std::vector<cv::DMatch> *> matches; |
| 300 | std::vector<cv::Point3f> training_points_3d; |
| 301 | std::vector<cv::Point2f> query_points; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 302 | std::vector<cv::Point2f> training_points; |
| 303 | cv::Mat homography; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 304 | }; |
| 305 | std::vector<PerImageMatches> per_image_matches(number_training_images()); |
| 306 | |
| 307 | // Pull out the good matches which we want for each image. |
| 308 | // Discard the bad matches per Lowe's ratio test. |
| 309 | // (Lowe originally proposed 0.7 ratio, but 0.75 was later proposed as a |
| 310 | // better option. We'll go with the more conservative (fewer, better matches) |
| 311 | // for now). |
| 312 | for (const std::vector<cv::DMatch> &match : matches) { |
| 313 | CHECK_EQ(2u, match.size()); |
| 314 | CHECK_LE(match[0].distance, match[1].distance); |
| 315 | CHECK_LT(match[0].imgIdx, number_training_images()); |
| 316 | CHECK_LT(match[1].imgIdx, number_training_images()); |
| 317 | CHECK_EQ(match[0].queryIdx, match[1].queryIdx); |
| 318 | if (!(match[0].distance < 0.7 * match[1].distance)) { |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | const int training_image = match[0].imgIdx; |
| 323 | CHECK_LT(training_image, static_cast<int>(per_image_matches.size())); |
| 324 | PerImageMatches *const per_image = &per_image_matches[training_image]; |
| 325 | per_image->matches.push_back(&match); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 326 | per_image->training_points.push_back( |
| 327 | Training2dPoint(training_image, match[0].trainIdx)); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 328 | per_image->training_points_3d.push_back( |
| 329 | Training3dPoint(training_image, match[0].trainIdx)); |
| 330 | |
| 331 | const cv::KeyPoint &keypoint = keypoints[match[0].queryIdx]; |
| 332 | per_image->query_points.push_back(keypoint.pt); |
| 333 | } |
| 334 | |
| 335 | // The minimum number of matches in a training image for us to use it. |
| 336 | static constexpr int kMinimumMatchCount = 10; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 337 | std::vector<cv::Mat> camera_target_list; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 338 | std::vector<cv::Mat> field_camera_list; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 339 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 340 | // Rebuild the matches and store them here |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 341 | std::vector<std::vector<cv::DMatch>> all_good_matches; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 342 | // Build list of target point and radius for each good match |
| 343 | std::vector<cv::Point2f> target_point_vector; |
| 344 | std::vector<float> target_radius_vector; |
| 345 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 346 | // Iterate through matches for each training image |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 347 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
| 348 | const PerImageMatches &per_image = per_image_matches[i]; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 349 | |
| 350 | VLOG(2) << "Number of matches to start: " << per_image_matches.size() |
| 351 | << "\n"; |
| 352 | // If we don't have enough matches to start, skip this set of matches |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 353 | if (per_image.matches.size() < kMinimumMatchCount) { |
| 354 | continue; |
| 355 | } |
| 356 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 357 | // Use homography to determine which matches make sense physically |
| 358 | cv::Mat mask; |
| 359 | cv::Mat homography = |
| 360 | cv::findHomography(per_image.training_points, per_image.query_points, |
| 361 | CV_RANSAC, 3.0, mask); |
| 362 | |
| 363 | // If mask doesn't have enough leftover matches, skip these matches |
| 364 | if (cv::countNonZero(mask) < kMinimumMatchCount) { |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | VLOG(2) << "Number of matches after homography: " << cv::countNonZero(mask) |
| 369 | << "\n"; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 370 | |
| 371 | // Fill our match info for each good match based on homography result |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 372 | PerImageMatches per_image_good_match; |
| 373 | CHECK_EQ(per_image.training_points.size(), |
| 374 | (unsigned long)mask.size().height); |
| 375 | for (size_t j = 0; j < per_image.matches.size(); j++) { |
| 376 | // Skip if we masked out by homography |
| 377 | if (mask.at<uchar>(0, j) != 1) { |
| 378 | continue; |
| 379 | } |
| 380 | |
| 381 | // Add this to our collection of all matches that passed our criteria |
| 382 | all_good_matches.push_back( |
| 383 | static_cast<std::vector<cv::DMatch>>(*per_image.matches[j])); |
| 384 | |
| 385 | // Fill out the data for matches per image that made it past |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 386 | // homography check, for later use |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 387 | per_image_good_match.matches.push_back(per_image.matches[j]); |
| 388 | per_image_good_match.training_points.push_back( |
| 389 | per_image.training_points[j]); |
| 390 | per_image_good_match.training_points_3d.push_back( |
| 391 | per_image.training_points_3d[j]); |
| 392 | per_image_good_match.query_points.push_back(per_image.query_points[j]); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 393 | } |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 394 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 395 | // Returns from opencv are doubles (CV_64F), which don't play well |
| 396 | // with our floats |
| 397 | homography.convertTo(homography, CV_32F); |
| 398 | per_image_good_match.homography = homography; |
| 399 | |
| 400 | CHECK_GT(per_image_good_match.matches.size(), 0u); |
| 401 | |
| 402 | // Collect training target location, so we can map it to matched image |
| 403 | cv::Point2f target_point; |
| 404 | float target_radius; |
| 405 | TargetLocation((*(per_image_good_match.matches[0]))[0].imgIdx, target_point, |
| 406 | target_radius); |
| 407 | |
| 408 | // Store target_point in vector for use by perspectiveTransform |
| 409 | std::vector<cv::Point2f> src_target_pt; |
| 410 | src_target_pt.push_back(target_point); |
| 411 | std::vector<cv::Point2f> query_target_pt; |
| 412 | |
| 413 | cv::perspectiveTransform(src_target_pt, query_target_pt, homography); |
| 414 | |
| 415 | float query_target_radius = |
| 416 | target_radius * |
| 417 | abs(homography.at<float>(0, 0) + homography.at<float>(1, 1)) / 2.; |
| 418 | |
| 419 | CHECK_EQ(query_target_pt.size(), 1u); |
| 420 | target_point_vector.push_back(query_target_pt[0]); |
| 421 | target_radius_vector.push_back(query_target_radius); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 422 | |
| 423 | // Pose transformations (rotations and translations) for various |
| 424 | // coordinate frames. R_X_Y_vec is the Rodrigues (angle-axis) |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 425 | // representation of the 3x3 rotation R_X_Y from frame X to frame Y |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 426 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 427 | // Tranform from camera to target frame |
Jim Ostrowski | 295f2a1 | 2020-02-26 22:22:44 -0800 | [diff] [blame] | 428 | cv::Mat R_camera_target_vec, R_camera_target, T_camera_target; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 429 | // Tranform from camera to field origin (global) reference frame |
| 430 | cv::Mat R_camera_field_vec, R_camera_field, T_camera_field; |
| 431 | // Inverse of camera to field-- defines location of camera in |
| 432 | // global (field) reference frame |
| 433 | cv::Mat R_field_camera_vec, R_field_camera, T_field_camera; |
| 434 | |
Jim Ostrowski | 295f2a1 | 2020-02-26 22:22:44 -0800 | [diff] [blame] | 435 | // Compute the pose of the camera (global origin relative to camera) |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 436 | cv::solvePnPRansac(per_image_good_match.training_points_3d, |
| 437 | per_image_good_match.query_points, CameraIntrinsics(), |
| 438 | CameraDistCoeffs(), R_camera_field_vec, T_camera_field); |
| 439 | CHECK_EQ(cv::Size(1, 3), T_camera_field.size()); |
| 440 | |
| 441 | // Convert to float32's (from float64) to be compatible with the rest |
| 442 | R_camera_field_vec.convertTo(R_camera_field_vec, CV_32F); |
| 443 | T_camera_field.convertTo(T_camera_field, CV_32F); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 444 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 445 | // Get matrix version of R_camera_field |
| 446 | cv::Rodrigues(R_camera_field_vec, R_camera_field); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 447 | CHECK_EQ(cv::Size(3, 3), R_camera_field.size()); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 448 | |
| 449 | // Compute H_field_camera = H_camera_field^-1 |
| 450 | R_field_camera = R_camera_field.t(); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 451 | T_field_camera = -R_field_camera * (T_camera_field); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 452 | |
| 453 | // Extract the field_target transformation |
| 454 | const cv::Mat H_field_target(4, 4, CV_32F, |
| 455 | const_cast<void *>(static_cast<const void *>( |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 456 | FieldToTarget(i)->data()->data()))); |
| 457 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 458 | const cv::Mat R_field_target = |
| 459 | H_field_target(cv::Range(0, 3), cv::Range(0, 3)); |
| 460 | const cv::Mat T_field_target = |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 461 | H_field_target(cv::Range(0, 3), cv::Range(3, 4)); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 462 | |
| 463 | // Use it to get the relative pose from camera to target |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 464 | R_camera_target = R_camera_field * (R_field_target); |
| 465 | T_camera_target = R_camera_field * (T_field_target) + T_camera_field; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 466 | |
| 467 | // Set H_camera_target |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 468 | { |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 469 | CHECK_EQ(cv::Size(3, 3), R_camera_target.size()); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 470 | CHECK_EQ(cv::Size(1, 3), T_camera_target.size()); |
| 471 | cv::Mat H_camera_target = cv::Mat::zeros(4, 4, CV_32F); |
| 472 | R_camera_target.copyTo(H_camera_target(cv::Range(0, 3), cv::Range(0, 3))); |
| 473 | T_camera_target.copyTo(H_camera_target(cv::Range(0, 3), cv::Range(3, 4))); |
| 474 | H_camera_target.at<float>(3, 3) = 1; |
| 475 | CHECK(H_camera_target.isContinuous()); |
| 476 | camera_target_list.push_back(H_camera_target.clone()); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // Set H_field_camera |
| 480 | { |
| 481 | CHECK_EQ(cv::Size(3, 3), R_field_camera.size()); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 482 | CHECK_EQ(cv::Size(1, 3), T_field_camera.size()); |
| 483 | cv::Mat H_field_camera = cv::Mat::zeros(4, 4, CV_32F); |
| 484 | R_field_camera.copyTo(H_field_camera(cv::Range(0, 3), cv::Range(0, 3))); |
| 485 | T_field_camera.copyTo(H_field_camera(cv::Range(0, 3), cv::Range(3, 4))); |
| 486 | H_field_camera.at<float>(3, 3) = 1; |
| 487 | CHECK(H_field_camera.isContinuous()); |
| 488 | field_camera_list.push_back(H_field_camera.clone()); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 489 | } |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 490 | } |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 491 | // Now, send our two messages-- one large, with details for remote |
| 492 | // debugging(features), and one smaller |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 493 | SendImageMatchResult(image, keypoints, descriptors, all_good_matches, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 494 | camera_target_list, field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 495 | target_point_vector, target_radius_vector, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 496 | &detailed_result_sender_, true); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 497 | SendImageMatchResult(image, keypoints, descriptors, all_good_matches, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 498 | camera_target_list, field_camera_list, |
| 499 | target_point_vector, target_radius_vector, |
| 500 | &result_sender_, false); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | void CameraReader::ReadImage() { |
| 504 | if (!reader_->ReadLatestImage()) { |
| 505 | LOG(INFO) << "No image, sleeping"; |
| 506 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | ProcessImage(reader_->LatestImage()); |
| 511 | |
| 512 | reader_->SendLatestImage(); |
| 513 | } |
| 514 | |
| 515 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 516 | CameraReader::PackImageMatches( |
| 517 | flatbuffers::FlatBufferBuilder *fbb, |
| 518 | const std::vector<std::vector<cv::DMatch>> &matches) { |
| 519 | // First, we need to pull out all the matches for each image. Might as well |
| 520 | // build up the Match tables at the same time. |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 521 | std::vector<std::vector<sift::Match>> per_image_matches( |
| 522 | number_training_images()); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 523 | for (const std::vector<cv::DMatch> &image_matches : matches) { |
Jim Ostrowski | ce02ebe | 2020-03-07 20:39:02 -0800 | [diff] [blame] | 524 | CHECK_GT(image_matches.size(), 0u); |
| 525 | // We're only using the first of the two matches |
| 526 | const cv::DMatch &image_match = image_matches[0]; |
| 527 | CHECK_LT(image_match.imgIdx, number_training_images()); |
| 528 | per_image_matches[image_match.imgIdx].emplace_back(); |
| 529 | sift::Match *const match = &per_image_matches[image_match.imgIdx].back(); |
| 530 | match->mutate_query_feature(image_match.queryIdx); |
| 531 | match->mutate_train_feature(image_match.trainIdx); |
| 532 | match->mutate_distance(image_match.distance); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Then, we need to build up each ImageMatch table. |
| 536 | std::vector<flatbuffers::Offset<sift::ImageMatch>> image_match_tables; |
| 537 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 538 | const std::vector<sift::Match> &this_image_matches = per_image_matches[i]; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 539 | if (this_image_matches.empty()) { |
| 540 | continue; |
| 541 | } |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 542 | const auto vector_offset = fbb->CreateVectorOfStructs(this_image_matches); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 543 | sift::ImageMatch::Builder image_builder(*fbb); |
| 544 | image_builder.add_train_image(i); |
| 545 | image_builder.add_matches(vector_offset); |
| 546 | image_match_tables.emplace_back(image_builder.Finish()); |
| 547 | } |
| 548 | |
| 549 | return fbb->CreateVector(image_match_tables); |
| 550 | } |
| 551 | |
| 552 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 553 | CameraReader::PackFeatures(flatbuffers::FlatBufferBuilder *fbb, |
| 554 | const std::vector<cv::KeyPoint> &keypoints, |
| 555 | const cv::Mat &descriptors) { |
| 556 | const int number_features = keypoints.size(); |
| 557 | CHECK_EQ(descriptors.rows, number_features); |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 558 | CHECK_EQ(descriptors.cols, 128); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 559 | std::vector<flatbuffers::Offset<sift::Feature>> features_vector( |
| 560 | number_features); |
| 561 | for (int i = 0; i < number_features; ++i) { |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 562 | const auto submat = |
| 563 | descriptors(cv::Range(i, i + 1), cv::Range(0, descriptors.cols)); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 564 | CHECK(submat.isContinuous()); |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 565 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> descriptor_offset; |
| 566 | { |
| 567 | uint8_t *data; |
| 568 | descriptor_offset = fbb->CreateUninitializedVector(128, &data); |
| 569 | submat.convertTo( |
| 570 | cv::Mat(1, descriptors.cols, CV_8U, static_cast<void *>(data)), |
| 571 | CV_8U); |
| 572 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 573 | sift::Feature::Builder feature_builder(*fbb); |
| 574 | feature_builder.add_descriptor(descriptor_offset); |
| 575 | feature_builder.add_x(keypoints[i].pt.x); |
| 576 | feature_builder.add_y(keypoints[i].pt.y); |
| 577 | feature_builder.add_size(keypoints[i].size); |
| 578 | feature_builder.add_angle(keypoints[i].angle); |
| 579 | feature_builder.add_response(keypoints[i].response); |
| 580 | feature_builder.add_octave(keypoints[i].octave); |
| 581 | CHECK_EQ(-1, keypoints[i].class_id) |
| 582 | << ": Not sure what to do with a class id"; |
| 583 | features_vector[i] = feature_builder.Finish(); |
| 584 | } |
| 585 | return fbb->CreateVector(features_vector); |
| 586 | } |
| 587 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 588 | void CameraReaderMain() { |
| 589 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 590 | aos::configuration::ReadConfig(FLAGS_config); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 591 | |
Austin Schuh | 75589cb | 2020-02-26 22:21:37 -0800 | [diff] [blame] | 592 | const auto training_data_bfbs = SiftTrainingData(); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 593 | const sift::TrainingData *const training_data = |
| 594 | flatbuffers::GetRoot<sift::TrainingData>(training_data_bfbs.data()); |
| 595 | { |
| 596 | flatbuffers::Verifier verifier( |
| 597 | reinterpret_cast<const uint8_t *>(training_data_bfbs.data()), |
| 598 | training_data_bfbs.size()); |
| 599 | CHECK(training_data->Verify(verifier)); |
| 600 | } |
| 601 | |
| 602 | const auto index_params = cv::makePtr<cv::flann::IndexParams>(); |
| 603 | index_params->setAlgorithm(cvflann::FLANN_INDEX_KDTREE); |
| 604 | index_params->setInt("trees", 5); |
| 605 | const auto search_params = |
| 606 | cv::makePtr<cv::flann::SearchParams>(/* checks */ 50); |
| 607 | cv::FlannBasedMatcher matcher(index_params, search_params); |
| 608 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 609 | aos::ShmEventLoop event_loop(&config.message()); |
Brian Silverman | 62956e7 | 2020-02-26 21:04:05 -0800 | [diff] [blame] | 610 | |
| 611 | // First, log the data for future reference. |
| 612 | { |
| 613 | aos::Sender<sift::TrainingData> training_data_sender = |
| 614 | event_loop.MakeSender<sift::TrainingData>("/camera"); |
| 615 | training_data_sender.Send( |
| 616 | aos::FlatbufferString<sift::TrainingData>(training_data_bfbs)); |
| 617 | } |
| 618 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 619 | V4L2Reader v4l2_reader(&event_loop, "/dev/video0"); |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 620 | CameraReader camera_reader(&event_loop, training_data, &v4l2_reader, |
| 621 | &matcher); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 622 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 623 | event_loop.Run(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | } // namespace |
| 627 | } // namespace vision |
| 628 | } // namespace frc971 |
| 629 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 630 | int main(int argc, char **argv) { |
| 631 | aos::InitGoogle(&argc, &argv); |
| 632 | frc971::vision::CameraReaderMain(); |
| 633 | } |