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 | |
Austin Schuh | 75589cb | 2020-02-26 22:21:37 -0800 | [diff] [blame^] | 10 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 11 | #include "y2020/vision/sift/sift971.h" |
| 12 | #include "y2020/vision/sift/sift_generated.h" |
| 13 | #include "y2020/vision/sift/sift_training_generated.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 | |
| 17 | namespace frc971 { |
| 18 | namespace vision { |
| 19 | namespace { |
| 20 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 21 | class CameraReader { |
| 22 | public: |
| 23 | CameraReader(aos::EventLoop *event_loop, |
| 24 | const sift::TrainingData *training_data, V4L2Reader *reader, |
| 25 | cv::FlannBasedMatcher *matcher) |
| 26 | : event_loop_(event_loop), |
| 27 | training_data_(training_data), |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 28 | camera_calibration_(FindCameraCalibration()), |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 29 | reader_(reader), |
| 30 | matcher_(matcher), |
| 31 | image_sender_(event_loop->MakeSender<CameraImage>("/camera")), |
| 32 | result_sender_( |
| 33 | event_loop->MakeSender<sift::ImageMatchResult>("/camera")), |
| 34 | read_image_timer_(event_loop->AddTimer([this]() { |
| 35 | ReadImage(); |
| 36 | read_image_timer_->Setup(event_loop_->monotonic_now()); |
| 37 | })) { |
| 38 | CopyTrainingFeatures(); |
| 39 | // Technically we don't need to do this, but doing it now avoids the first |
| 40 | // match attempt being slow. |
| 41 | matcher_->train(); |
| 42 | |
| 43 | event_loop->OnRun( |
| 44 | [this]() { read_image_timer_->Setup(event_loop_->monotonic_now()); }); |
| 45 | } |
| 46 | |
| 47 | private: |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 48 | const sift::CameraCalibration *FindCameraCalibration() const; |
| 49 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 50 | // Copies the information from training_data_ into matcher_. |
| 51 | void CopyTrainingFeatures(); |
| 52 | // Processes an image (including sending the results). |
| 53 | void ProcessImage(const CameraImage &image); |
| 54 | // Reads an image, and then performs all of our processing on it. |
| 55 | void ReadImage(); |
| 56 | |
| 57 | flatbuffers::Offset< |
| 58 | flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 59 | PackImageMatches(flatbuffers::FlatBufferBuilder *fbb, |
| 60 | const std::vector<std::vector<cv::DMatch>> &matches); |
| 61 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 62 | PackFeatures(flatbuffers::FlatBufferBuilder *fbb, |
| 63 | const std::vector<cv::KeyPoint> &keypoints, |
| 64 | const cv::Mat &descriptors); |
| 65 | |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 66 | // Returns the 3D location for the specified training feature. |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 67 | cv::Point3f Training3dPoint(int training_image_index, |
| 68 | int feature_index) const { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 69 | const sift::KeypointFieldLocation *const location = |
| 70 | training_data_->images() |
| 71 | ->Get(training_image_index) |
| 72 | ->features() |
| 73 | ->Get(feature_index) |
| 74 | ->field_location(); |
| 75 | return cv::Point3f(location->x(), location->y(), location->z()); |
| 76 | } |
| 77 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 78 | const sift::TransformationMatrix *FieldToTarget(int training_image_index) { |
| 79 | return training_data_->images() |
| 80 | ->Get(training_image_index) |
| 81 | ->field_to_target(); |
| 82 | } |
| 83 | |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 84 | int number_training_images() const { |
| 85 | return training_data_->images()->size(); |
| 86 | } |
| 87 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 88 | cv::Mat CameraIntrinsics() const { |
| 89 | const cv::Mat result(3, 3, CV_32F, |
| 90 | const_cast<void *>(static_cast<const void *>( |
| 91 | camera_calibration_->intrinsics()->data()))); |
| 92 | CHECK_EQ(result.total(), camera_calibration_->intrinsics()->size()); |
| 93 | return result; |
| 94 | } |
| 95 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 96 | aos::EventLoop *const event_loop_; |
| 97 | const sift::TrainingData *const training_data_; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 98 | const sift::CameraCalibration *const camera_calibration_; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 99 | V4L2Reader *const reader_; |
| 100 | cv::FlannBasedMatcher *const matcher_; |
| 101 | aos::Sender<CameraImage> image_sender_; |
| 102 | aos::Sender<sift::ImageMatchResult> result_sender_; |
| 103 | // We schedule this immediately to read an image. Having it on a timer means |
| 104 | // other things can run on the event loop in between. |
| 105 | aos::TimerHandler *const read_image_timer_; |
| 106 | |
| 107 | const std::unique_ptr<frc971::vision::SIFT971_Impl> sift_{ |
| 108 | new frc971::vision::SIFT971_Impl()}; |
| 109 | }; |
| 110 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 111 | const sift::CameraCalibration *CameraReader::FindCameraCalibration() const { |
| 112 | const std::string_view node_name = event_loop_->node()->name()->string_view(); |
| 113 | const int team_number = aos::network::GetTeamNumber(); |
| 114 | for (const sift::CameraCalibration *candidate : |
| 115 | *training_data_->camera_calibrations()) { |
| 116 | if (candidate->node_name()->string_view() != node_name) { |
| 117 | continue; |
| 118 | } |
| 119 | if (candidate->team_number() != team_number) { |
| 120 | continue; |
| 121 | } |
| 122 | return candidate; |
| 123 | } |
| 124 | LOG(FATAL) << ": Failed to find camera calibration for " << node_name |
| 125 | << " on " << team_number; |
| 126 | } |
| 127 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 128 | void CameraReader::CopyTrainingFeatures() { |
| 129 | for (const sift::TrainingImage *training_image : *training_data_->images()) { |
| 130 | cv::Mat features(training_image->features()->size(), 128, CV_32F); |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 131 | for (size_t i = 0; i < training_image->features()->size(); ++i) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 132 | const sift::Feature *feature_table = training_image->features()->Get(i); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 133 | |
| 134 | // We don't need this information right now, but make sure it's here to |
| 135 | // avoid crashes that only occur when specific features are matched. |
| 136 | CHECK(feature_table->has_field_location()); |
| 137 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 138 | const flatbuffers::Vector<float> *const descriptor = |
| 139 | feature_table->descriptor(); |
| 140 | CHECK_EQ(descriptor->size(), 128u) << ": Unsupported feature size"; |
| 141 | cv::Mat(1, descriptor->size(), CV_32F, |
| 142 | const_cast<void *>(static_cast<const void *>(descriptor->data()))) |
| 143 | .copyTo(features(cv::Range(i, i + 1), cv::Range(0, 128))); |
| 144 | } |
| 145 | matcher_->add(features); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void CameraReader::ProcessImage(const CameraImage &image) { |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 150 | // Be ready to pack the results up and send them out. We can pack things into |
| 151 | // this memory as we go to allow reusing temporaries better. |
| 152 | auto builder = result_sender_.MakeBuilder(); |
| 153 | const auto camera_calibration_offset = |
| 154 | aos::CopyFlatBuffer(camera_calibration_, builder.fbb()); |
| 155 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 156 | // First, we need to extract the brightness information. This can't really be |
| 157 | // fused into the beginning of the SIFT algorithm because the algorithm needs |
| 158 | // to look at the base image directly. It also only takes 2ms on our images. |
| 159 | // This is converting from YUYV to a grayscale image. |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 160 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 161 | CHECK(image_mat.isContinuous()); |
| 162 | const int number_pixels = image.rows() * image.cols(); |
| 163 | for (int i = 0; i < number_pixels; ++i) { |
| 164 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 165 | image.data()->data()[i * 2]; |
| 166 | } |
| 167 | |
| 168 | // Next, grab the features from the image. |
| 169 | std::vector<cv::KeyPoint> keypoints; |
| 170 | cv::Mat descriptors; |
| 171 | sift_->detectAndCompute(image_mat, cv::noArray(), keypoints, descriptors); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 172 | const auto features_offset = |
| 173 | PackFeatures(builder.fbb(), keypoints, descriptors); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 174 | |
| 175 | // Then, match those features against our training data. |
| 176 | std::vector<std::vector<cv::DMatch>> matches; |
| 177 | matcher_->knnMatch(/* queryDescriptors */ descriptors, matches, /* k */ 2); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 178 | const auto image_matches_offset = PackImageMatches(builder.fbb(), matches); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 179 | |
| 180 | struct PerImageMatches { |
| 181 | std::vector<const std::vector<cv::DMatch> *> matches; |
| 182 | std::vector<cv::Point3f> training_points_3d; |
| 183 | std::vector<cv::Point2f> query_points; |
| 184 | }; |
| 185 | std::vector<PerImageMatches> per_image_matches(number_training_images()); |
| 186 | |
| 187 | // Pull out the good matches which we want for each image. |
| 188 | // Discard the bad matches per Lowe's ratio test. |
| 189 | // (Lowe originally proposed 0.7 ratio, but 0.75 was later proposed as a |
| 190 | // better option. We'll go with the more conservative (fewer, better matches) |
| 191 | // for now). |
| 192 | for (const std::vector<cv::DMatch> &match : matches) { |
| 193 | CHECK_EQ(2u, match.size()); |
| 194 | CHECK_LE(match[0].distance, match[1].distance); |
| 195 | CHECK_LT(match[0].imgIdx, number_training_images()); |
| 196 | CHECK_LT(match[1].imgIdx, number_training_images()); |
| 197 | CHECK_EQ(match[0].queryIdx, match[1].queryIdx); |
| 198 | if (!(match[0].distance < 0.7 * match[1].distance)) { |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | const int training_image = match[0].imgIdx; |
| 203 | CHECK_LT(training_image, static_cast<int>(per_image_matches.size())); |
| 204 | PerImageMatches *const per_image = &per_image_matches[training_image]; |
| 205 | per_image->matches.push_back(&match); |
| 206 | per_image->training_points_3d.push_back( |
| 207 | Training3dPoint(training_image, match[0].trainIdx)); |
| 208 | |
| 209 | const cv::KeyPoint &keypoint = keypoints[match[0].queryIdx]; |
| 210 | per_image->query_points.push_back(keypoint.pt); |
| 211 | } |
| 212 | |
| 213 | // The minimum number of matches in a training image for us to use it. |
| 214 | static constexpr int kMinimumMatchCount = 10; |
| 215 | |
| 216 | std::vector<flatbuffers::Offset<sift::CameraPose>> camera_poses; |
| 217 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
| 218 | const PerImageMatches &per_image = per_image_matches[i]; |
| 219 | if (per_image.matches.size() < kMinimumMatchCount) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | cv::Mat R_camera_target, T_camera_target; |
| 224 | cv::solvePnPRansac(per_image.training_points_3d, per_image.query_points, |
| 225 | CameraIntrinsics(), cv::noArray(), R_camera_target, |
| 226 | T_camera_target); |
| 227 | |
| 228 | sift::CameraPose::Builder pose_builder(*builder.fbb()); |
| 229 | { |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 230 | CHECK_EQ(cv::Size(3, 3), R_camera_target.size()); |
| 231 | CHECK_EQ(cv::Size(3, 1), T_camera_target.size()); |
| 232 | cv::Mat camera_target = cv::Mat::zeros(4, 4, CV_32F); |
| 233 | R_camera_target.copyTo(camera_target(cv::Range(0, 3), cv::Range(0, 3))); |
| 234 | T_camera_target.copyTo(camera_target(cv::Range(3, 4), cv::Range(0, 3))); |
| 235 | camera_target.at<float>(3, 3) = 1; |
| 236 | CHECK(camera_target.isContinuous()); |
| 237 | const auto data_offset = builder.fbb()->CreateVector<float>( |
| 238 | reinterpret_cast<float *>(camera_target.data), camera_target.total()); |
| 239 | pose_builder.add_camera_to_target( |
| 240 | sift::CreateTransformationMatrix(*builder.fbb(), data_offset)); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 241 | } |
| 242 | pose_builder.add_field_to_target( |
| 243 | aos::CopyFlatBuffer(FieldToTarget(i), builder.fbb())); |
| 244 | camera_poses.emplace_back(pose_builder.Finish()); |
| 245 | } |
| 246 | const auto camera_poses_offset = builder.fbb()->CreateVector(camera_poses); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 247 | |
| 248 | sift::ImageMatchResult::Builder result_builder(*builder.fbb()); |
| 249 | result_builder.add_image_matches(image_matches_offset); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 250 | result_builder.add_camera_poses(camera_poses_offset); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 251 | result_builder.add_features(features_offset); |
| 252 | result_builder.add_image_monotonic_timestamp_ns( |
| 253 | image.monotonic_timestamp_ns()); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 254 | result_builder.add_camera_calibration(camera_calibration_offset); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 255 | builder.Send(result_builder.Finish()); |
| 256 | } |
| 257 | |
| 258 | void CameraReader::ReadImage() { |
| 259 | if (!reader_->ReadLatestImage()) { |
| 260 | LOG(INFO) << "No image, sleeping"; |
| 261 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | ProcessImage(reader_->LatestImage()); |
| 266 | |
| 267 | reader_->SendLatestImage(); |
| 268 | } |
| 269 | |
| 270 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 271 | CameraReader::PackImageMatches( |
| 272 | flatbuffers::FlatBufferBuilder *fbb, |
| 273 | const std::vector<std::vector<cv::DMatch>> &matches) { |
| 274 | // First, we need to pull out all the matches for each image. Might as well |
| 275 | // build up the Match tables at the same time. |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 276 | std::vector<std::vector<sift::Match>> per_image_matches( |
| 277 | number_training_images()); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 278 | for (const std::vector<cv::DMatch> &image_matches : matches) { |
| 279 | for (const cv::DMatch &image_match : image_matches) { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 280 | CHECK_LT(image_match.imgIdx, number_training_images()); |
| 281 | per_image_matches[image_match.imgIdx].emplace_back(); |
| 282 | sift::Match *const match = &per_image_matches[image_match.imgIdx].back(); |
| 283 | match->mutate_query_feature(image_match.queryIdx); |
| 284 | match->mutate_train_feature(image_match.trainIdx); |
| 285 | match->mutate_distance(image_match.distance); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | // Then, we need to build up each ImageMatch table. |
| 290 | std::vector<flatbuffers::Offset<sift::ImageMatch>> image_match_tables; |
| 291 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 292 | const std::vector<sift::Match> &this_image_matches = per_image_matches[i]; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 293 | if (this_image_matches.empty()) { |
| 294 | continue; |
| 295 | } |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 296 | const auto vector_offset = fbb->CreateVectorOfStructs(this_image_matches); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 297 | sift::ImageMatch::Builder image_builder(*fbb); |
| 298 | image_builder.add_train_image(i); |
| 299 | image_builder.add_matches(vector_offset); |
| 300 | image_match_tables.emplace_back(image_builder.Finish()); |
| 301 | } |
| 302 | |
| 303 | return fbb->CreateVector(image_match_tables); |
| 304 | } |
| 305 | |
| 306 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 307 | CameraReader::PackFeatures(flatbuffers::FlatBufferBuilder *fbb, |
| 308 | const std::vector<cv::KeyPoint> &keypoints, |
| 309 | const cv::Mat &descriptors) { |
| 310 | const int number_features = keypoints.size(); |
| 311 | CHECK_EQ(descriptors.rows, number_features); |
| 312 | std::vector<flatbuffers::Offset<sift::Feature>> features_vector( |
| 313 | number_features); |
| 314 | for (int i = 0; i < number_features; ++i) { |
| 315 | const auto submat = descriptors(cv::Range(i, i + 1), cv::Range(0, 128)); |
| 316 | CHECK(submat.isContinuous()); |
| 317 | const auto descriptor_offset = |
| 318 | fbb->CreateVector(reinterpret_cast<float *>(submat.data), 128); |
| 319 | sift::Feature::Builder feature_builder(*fbb); |
| 320 | feature_builder.add_descriptor(descriptor_offset); |
| 321 | feature_builder.add_x(keypoints[i].pt.x); |
| 322 | feature_builder.add_y(keypoints[i].pt.y); |
| 323 | feature_builder.add_size(keypoints[i].size); |
| 324 | feature_builder.add_angle(keypoints[i].angle); |
| 325 | feature_builder.add_response(keypoints[i].response); |
| 326 | feature_builder.add_octave(keypoints[i].octave); |
| 327 | CHECK_EQ(-1, keypoints[i].class_id) |
| 328 | << ": Not sure what to do with a class id"; |
| 329 | features_vector[i] = feature_builder.Finish(); |
| 330 | } |
| 331 | return fbb->CreateVector(features_vector); |
| 332 | } |
| 333 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 334 | void CameraReaderMain() { |
| 335 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 336 | aos::configuration::ReadConfig("config.json"); |
| 337 | |
Austin Schuh | 75589cb | 2020-02-26 22:21:37 -0800 | [diff] [blame^] | 338 | const auto training_data_bfbs = SiftTrainingData(); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 339 | const sift::TrainingData *const training_data = |
| 340 | flatbuffers::GetRoot<sift::TrainingData>(training_data_bfbs.data()); |
| 341 | { |
| 342 | flatbuffers::Verifier verifier( |
| 343 | reinterpret_cast<const uint8_t *>(training_data_bfbs.data()), |
| 344 | training_data_bfbs.size()); |
| 345 | CHECK(training_data->Verify(verifier)); |
| 346 | } |
| 347 | |
| 348 | const auto index_params = cv::makePtr<cv::flann::IndexParams>(); |
| 349 | index_params->setAlgorithm(cvflann::FLANN_INDEX_KDTREE); |
| 350 | index_params->setInt("trees", 5); |
| 351 | const auto search_params = |
| 352 | cv::makePtr<cv::flann::SearchParams>(/* checks */ 50); |
| 353 | cv::FlannBasedMatcher matcher(index_params, search_params); |
| 354 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 355 | aos::ShmEventLoop event_loop(&config.message()); |
Brian Silverman | 62956e7 | 2020-02-26 21:04:05 -0800 | [diff] [blame] | 356 | |
| 357 | // First, log the data for future reference. |
| 358 | { |
| 359 | aos::Sender<sift::TrainingData> training_data_sender = |
| 360 | event_loop.MakeSender<sift::TrainingData>("/camera"); |
| 361 | training_data_sender.Send( |
| 362 | aos::FlatbufferString<sift::TrainingData>(training_data_bfbs)); |
| 363 | } |
| 364 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 365 | V4L2Reader v4l2_reader(&event_loop, "/dev/video0"); |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 366 | CameraReader camera_reader(&event_loop, training_data, &v4l2_reader, |
| 367 | &matcher); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 368 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 369 | event_loop.Run(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | } // namespace |
| 373 | } // namespace vision |
| 374 | } // namespace frc971 |
| 375 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 376 | int main(int argc, char **argv) { |
| 377 | aos::InitGoogle(&argc, &argv); |
| 378 | frc971::vision::CameraReaderMain(); |
| 379 | } |