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