Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 1 | #include "y2023/vision/aprilrobotics.h" |
| 2 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 3 | #include "absl/flags/flag.h" |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 4 | #include <opencv2/highgui.hpp> |
| 5 | |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 6 | #include "y2023/vision/vision_util.h" |
| 7 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 8 | ABSL_FLAG( |
| 9 | bool, debug, false, |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 10 | "If true, dump a ton of debug and crash on the first valid detection."); |
| 11 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 12 | ABSL_FLAG(double, min_decision_margin, 50.0, |
| 13 | "Minimum decision margin (confidence) for an apriltag detection"); |
| 14 | ABSL_FLAG(int32_t, pixel_border, 10, |
| 15 | "Size of image border within which to reject detected corners"); |
| 16 | ABSL_FLAG( |
| 17 | double, max_expected_distortion, 0.314, |
milind-u | 60e7fe5 | 2023-02-26 16:13:50 -0800 | [diff] [blame] | 18 | "Maximum expected value for unscaled distortion factors. Will scale " |
| 19 | "distortion factors so that this value (and a higher distortion) maps to " |
| 20 | "1.0."); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 21 | ABSL_FLAG(uint64_t, pose_estimation_iterations, 50, |
| 22 | "Number of iterations for apriltag pose estimation."); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 23 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 24 | namespace y2023::vision { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 25 | |
Austin Schuh | d266793 | 2023-02-04 16:22:39 -0800 | [diff] [blame] | 26 | namespace chrono = std::chrono; |
| 27 | |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 28 | // Set max age on image for processing at 20 ms. For 60Hz, we should be |
| 29 | // processing at least every 16.7ms |
| 30 | constexpr aos::monotonic_clock::duration kMaxImageAge = |
| 31 | std::chrono::milliseconds(20); |
| 32 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 33 | AprilRoboticsDetector::AprilRoboticsDetector(aos::EventLoop *event_loop, |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 34 | std::string_view channel_name, |
| 35 | bool flip_image) |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 36 | : calibration_data_(event_loop), |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 37 | image_size_(0, 0), |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 38 | flip_image_(flip_image), |
| 39 | node_name_(event_loop->node()->name()->string_view()), |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 40 | ftrace_(), |
milind-u | fbc5c81 | 2023-04-06 21:24:29 -0700 | [diff] [blame] | 41 | image_callback_( |
| 42 | event_loop, channel_name, |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 43 | [this](cv::Mat image_color_mat, |
| 44 | const aos::monotonic_clock::time_point eof) { |
milind-u | fbc5c81 | 2023-04-06 21:24:29 -0700 | [diff] [blame] | 45 | HandleImage(image_color_mat, eof); |
| 46 | }, |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 47 | kMaxImageAge), |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 48 | target_map_sender_( |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 49 | event_loop->MakeSender<frc971::vision::TargetMap>("/camera")), |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 50 | image_annotations_sender_( |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 51 | event_loop->MakeSender<foxglove::ImageAnnotations>("/camera")), |
| 52 | rejections_(0) { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 53 | tag_family_ = tag16h5_create(); |
| 54 | tag_detector_ = apriltag_detector_create(); |
| 55 | |
| 56 | apriltag_detector_add_family_bits(tag_detector_, tag_family_, 1); |
| 57 | tag_detector_->nthreads = 6; |
| 58 | tag_detector_->wp = workerpool_create(tag_detector_->nthreads); |
| 59 | tag_detector_->qtp.min_white_black_diff = 5; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 60 | tag_detector_->debug = absl::GetFlag(FLAGS_debug); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 61 | |
| 62 | std::string hostname = aos::network::GetHostname(); |
| 63 | |
| 64 | // Check team string is valid |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 65 | calibration_ = FindCameraCalibration( |
| 66 | calibration_data_.constants(), event_loop->node()->name()->string_view()); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 67 | |
| 68 | extrinsics_ = CameraExtrinsics(calibration_); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 69 | intrinsics_ = CameraIntrinsics(calibration_); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 70 | // Create an undistort projection matrix using the intrinsics |
| 71 | projection_matrix_ = cv::Mat::zeros(3, 4, CV_64F); |
| 72 | intrinsics_.rowRange(0, 3).colRange(0, 3).copyTo( |
| 73 | projection_matrix_.rowRange(0, 3).colRange(0, 3)); |
| 74 | |
| 75 | dist_coeffs_ = CameraDistCoeffs(calibration_); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 76 | |
| 77 | image_callback_.set_format(frc971::vision::ImageCallback::Format::GRAYSCALE); |
| 78 | } |
| 79 | |
| 80 | AprilRoboticsDetector::~AprilRoboticsDetector() { |
| 81 | apriltag_detector_destroy(tag_detector_); |
| 82 | free(tag_family_); |
| 83 | } |
| 84 | |
| 85 | void AprilRoboticsDetector::SetWorkerpoolAffinities() { |
| 86 | for (int i = 0; i < tag_detector_->wp->nthreads; i++) { |
| 87 | cpu_set_t affinity; |
| 88 | CPU_ZERO(&affinity); |
| 89 | CPU_SET(i, &affinity); |
| 90 | pthread_setaffinity_np(tag_detector_->wp->threads[i], sizeof(affinity), |
| 91 | &affinity); |
| 92 | struct sched_param param; |
| 93 | param.sched_priority = 20; |
| 94 | int res = pthread_setschedparam(tag_detector_->wp->threads[i], SCHED_FIFO, |
| 95 | ¶m); |
| 96 | PCHECK(res == 0) << "Failed to set priority of threadpool threads"; |
| 97 | } |
| 98 | } |
| 99 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 100 | void AprilRoboticsDetector::HandleImage(cv::Mat image_grayscale, |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 101 | aos::monotonic_clock::time_point eof) { |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 102 | image_size_ = image_grayscale.size(); |
| 103 | |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 104 | DetectionResult result = DetectTags(image_grayscale, eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 105 | |
| 106 | auto builder = target_map_sender_.MakeBuilder(); |
| 107 | std::vector<flatbuffers::Offset<frc971::vision::TargetPoseFbs>> target_poses; |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 108 | for (auto &detection : result.detections) { |
| 109 | auto *fbb = builder.fbb(); |
| 110 | auto pose = BuildTargetPose(detection, fbb); |
| 111 | DestroyPose(&detection.pose); |
| 112 | target_poses.emplace_back(pose); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 113 | } |
| 114 | const auto target_poses_offset = builder.fbb()->CreateVector(target_poses); |
| 115 | auto target_map_builder = builder.MakeBuilder<frc971::vision::TargetMap>(); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 116 | target_map_builder.add_target_poses(target_poses_offset); |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 117 | target_map_builder.add_monotonic_timestamp_ns(eof.time_since_epoch().count()); |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 118 | target_map_builder.add_rejections(result.rejections); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 119 | builder.CheckOk(builder.Send(target_map_builder.Finish())); |
| 120 | } |
| 121 | |
| 122 | flatbuffers::Offset<frc971::vision::TargetPoseFbs> |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 123 | AprilRoboticsDetector::BuildTargetPose(const Detection &detection, |
milind-u | 681c471 | 2023-02-23 21:22:50 -0800 | [diff] [blame] | 124 | flatbuffers::FlatBufferBuilder *fbb) { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 125 | const auto T = |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 126 | Eigen::Translation3d(detection.pose.t->data[0], detection.pose.t->data[1], |
| 127 | detection.pose.t->data[2]); |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 128 | const auto position_offset = |
| 129 | frc971::vision::CreatePosition(*fbb, T.x(), T.y(), T.z()); |
| 130 | |
milind-u | 633c4c0 | 2023-02-25 22:51:45 -0800 | [diff] [blame] | 131 | // Aprilrobotics stores the rotation matrix in row-major order |
| 132 | const auto orientation = Eigen::Quaterniond( |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 133 | Eigen::Matrix<double, 3, 3, Eigen::RowMajor>(detection.pose.R->data)); |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 134 | const auto orientation_offset = frc971::vision::CreateQuaternion( |
| 135 | *fbb, orientation.w(), orientation.x(), orientation.y(), orientation.z()); |
| 136 | |
milind-u | 681c471 | 2023-02-23 21:22:50 -0800 | [diff] [blame] | 137 | return frc971::vision::CreateTargetPoseFbs( |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 138 | *fbb, detection.det.id, position_offset, orientation_offset, |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 139 | detection.det.decision_margin, detection.pose_error, |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 140 | detection.distortion_factor, detection.pose_error_ratio); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 141 | } |
| 142 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 143 | void AprilRoboticsDetector::UndistortDetection( |
| 144 | apriltag_detection_t *det) const { |
| 145 | // 4 corners |
| 146 | constexpr size_t kRows = 4; |
| 147 | // 2d points |
| 148 | constexpr size_t kCols = 2; |
| 149 | |
| 150 | cv::Mat distorted_points(kRows, kCols, CV_64F, det->p); |
| 151 | cv::Mat undistorted_points = cv::Mat::zeros(kRows, kCols, CV_64F); |
| 152 | |
| 153 | // Undistort the april tag points |
| 154 | cv::undistortPoints(distorted_points, undistorted_points, intrinsics_, |
| 155 | dist_coeffs_, cv::noArray(), projection_matrix_); |
| 156 | |
| 157 | // Copy the undistorted points into det |
| 158 | for (size_t i = 0; i < kRows; i++) { |
| 159 | for (size_t j = 0; j < kCols; j++) { |
| 160 | det->p[i][j] = undistorted_points.at<double>(i, j); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 165 | double AprilRoboticsDetector::ComputeDistortionFactor( |
| 166 | const std::vector<cv::Point2f> &orig_corners, |
| 167 | const std::vector<cv::Point2f> &corners) { |
| 168 | CHECK_EQ(orig_corners.size(), 4ul); |
| 169 | CHECK_EQ(corners.size(), 4ul); |
| 170 | |
| 171 | double avg_distance = 0.0; |
| 172 | for (size_t i = 0; i < corners.size(); i++) { |
| 173 | avg_distance += cv::norm(orig_corners[i] - corners[i]); |
| 174 | } |
| 175 | avg_distance /= corners.size(); |
| 176 | |
milind-u | 8773c23 | 2023-03-11 14:59:06 -0800 | [diff] [blame] | 177 | // Normalize avg_distance by dividing by the image diagonal, |
| 178 | // and then the maximum expected distortion |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 179 | double distortion_factor = |
| 180 | avg_distance / |
milind-u | 8773c23 | 2023-03-11 14:59:06 -0800 | [diff] [blame] | 181 | cv::norm(cv::Point2d(image_size_.width, image_size_.height)); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 182 | return std::min( |
| 183 | distortion_factor / absl::GetFlag(FLAGS_max_expected_distortion), 1.0); |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 186 | std::vector<cv::Point2f> AprilRoboticsDetector::MakeCornerVector( |
| 187 | const apriltag_detection_t *det) { |
| 188 | std::vector<cv::Point2f> corner_points; |
| 189 | corner_points.emplace_back(det->p[0][0], det->p[0][1]); |
| 190 | corner_points.emplace_back(det->p[1][0], det->p[1][1]); |
| 191 | corner_points.emplace_back(det->p[2][0], det->p[2][1]); |
| 192 | corner_points.emplace_back(det->p[3][0], det->p[3][1]); |
| 193 | |
| 194 | return corner_points; |
| 195 | } |
| 196 | |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 197 | void AprilRoboticsDetector::DestroyPose(apriltag_pose_t *pose) const { |
| 198 | matd_destroy(pose->R); |
| 199 | matd_destroy(pose->t); |
| 200 | } |
| 201 | |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 202 | AprilRoboticsDetector::DetectionResult AprilRoboticsDetector::DetectTags( |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 203 | cv::Mat image, aos::monotonic_clock::time_point eof) { |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 204 | cv::Mat color_image; |
| 205 | cvtColor(image, color_image, cv::COLOR_GRAY2RGB); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 206 | const aos::monotonic_clock::time_point start_time = |
| 207 | aos::monotonic_clock::now(); |
| 208 | |
| 209 | image_u8_t im = { |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 210 | .width = image.cols, |
| 211 | .height = image.rows, |
| 212 | .stride = image.cols, |
| 213 | .buf = image.data, |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 214 | }; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 215 | const uint32_t min_x = absl::GetFlag(FLAGS_pixel_border); |
| 216 | const uint32_t max_x = image.cols - absl::GetFlag(FLAGS_pixel_border); |
| 217 | const uint32_t min_y = absl::GetFlag(FLAGS_pixel_border); |
| 218 | const uint32_t max_y = image.rows - absl::GetFlag(FLAGS_pixel_border); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 219 | |
| 220 | ftrace_.FormatMessage("Starting detect\n"); |
| 221 | zarray_t *detections = apriltag_detector_detect(tag_detector_, &im); |
| 222 | ftrace_.FormatMessage("Done detecting\n"); |
| 223 | |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 224 | std::vector<Detection> results; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 225 | |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 226 | auto builder = image_annotations_sender_.MakeBuilder(); |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 227 | std::vector<flatbuffers::Offset<foxglove::PointsAnnotation>> foxglove_corners; |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 228 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 229 | for (int i = 0; i < zarray_size(detections); i++) { |
| 230 | apriltag_detection_t *det; |
| 231 | zarray_get(detections, i, &det); |
| 232 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 233 | if (det->decision_margin > absl::GetFlag(FLAGS_min_decision_margin)) { |
Jim Ostrowski | 437b1fb | 2023-02-26 10:12:01 -0800 | [diff] [blame] | 234 | if (det->p[0][0] < min_x || det->p[0][0] > max_x || |
| 235 | det->p[1][0] < min_x || det->p[1][0] > max_x || |
| 236 | det->p[2][0] < min_x || det->p[2][0] > max_x || |
Jim Ostrowski | 0197d6b | 2023-03-04 12:41:13 -0800 | [diff] [blame] | 237 | det->p[3][0] < min_x || det->p[3][0] > max_x || |
| 238 | det->p[0][1] < min_y || det->p[0][1] > max_y || |
| 239 | det->p[1][1] < min_y || det->p[1][1] > max_y || |
| 240 | det->p[2][1] < min_y || det->p[2][1] > max_y || |
| 241 | det->p[3][1] < min_y || det->p[3][1] > max_y) { |
Jim Ostrowski | 437b1fb | 2023-02-26 10:12:01 -0800 | [diff] [blame] | 242 | VLOG(1) << "Rejecting detection because corner is outside pixel border"; |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 243 | // Send rejected corner points in red |
| 244 | std::vector<cv::Point2f> rejected_corner_points = MakeCornerVector(det); |
| 245 | foxglove_corners.push_back(frc971::vision::BuildPointsAnnotation( |
| 246 | builder.fbb(), eof, rejected_corner_points, |
| 247 | std::vector<double>{1.0, 0.0, 0.0, 0.5})); |
Jim Ostrowski | 437b1fb | 2023-02-26 10:12:01 -0800 | [diff] [blame] | 248 | continue; |
| 249 | } |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 250 | VLOG(1) << "Found tag number " << det->id << " hamming: " << det->hamming |
| 251 | << " margin: " << det->decision_margin; |
| 252 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 253 | // First create an apriltag_detection_info_t struct using your known |
| 254 | // parameters. |
| 255 | apriltag_detection_info_t info; |
| 256 | info.det = det; |
| 257 | info.tagsize = 0.1524; |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 258 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 259 | info.fx = intrinsics_.at<double>(0, 0); |
| 260 | info.fy = intrinsics_.at<double>(1, 1); |
| 261 | info.cx = intrinsics_.at<double>(0, 2); |
| 262 | info.cy = intrinsics_.at<double>(1, 2); |
| 263 | |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 264 | // Send original corner points in green |
| 265 | std::vector<cv::Point2f> orig_corner_points = MakeCornerVector(det); |
| 266 | foxglove_corners.push_back(frc971::vision::BuildPointsAnnotation( |
| 267 | builder.fbb(), eof, orig_corner_points, |
| 268 | std::vector<double>{0.0, 1.0, 0.0, 0.5})); |
Jim Ostrowski | d2b5c91 | 2023-02-26 00:59:35 -0800 | [diff] [blame] | 269 | |
milind-u | eccac3c | 2023-02-25 20:54:47 -0800 | [diff] [blame] | 270 | UndistortDetection(det); |
| 271 | |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 272 | const aos::monotonic_clock::time_point before_pose_estimation = |
| 273 | aos::monotonic_clock::now(); |
| 274 | |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 275 | apriltag_pose_t pose_1; |
| 276 | apriltag_pose_t pose_2; |
| 277 | double pose_error_1; |
| 278 | double pose_error_2; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 279 | estimate_tag_pose_orthogonal_iteration( |
| 280 | &info, &pose_error_1, &pose_1, &pose_error_2, &pose_2, |
| 281 | absl::GetFlag(FLAGS_pose_estimation_iterations)); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 282 | |
| 283 | const aos::monotonic_clock::time_point after_pose_estimation = |
| 284 | aos::monotonic_clock::now(); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 285 | VLOG(1) << "Took " |
Austin Schuh | d266793 | 2023-02-04 16:22:39 -0800 | [diff] [blame] | 286 | << chrono::duration<double>(after_pose_estimation - |
| 287 | before_pose_estimation) |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 288 | .count() |
| 289 | << " seconds for pose estimation"; |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 290 | VLOG(1) << "Pose err 1: " << pose_error_1; |
| 291 | VLOG(1) << "Pose err 2: " << pose_error_2; |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 292 | |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 293 | // Send undistorted corner points in pink |
| 294 | std::vector<cv::Point2f> corner_points = MakeCornerVector(det); |
| 295 | foxglove_corners.push_back(frc971::vision::BuildPointsAnnotation( |
| 296 | builder.fbb(), eof, corner_points, |
| 297 | std::vector<double>{1.0, 0.75, 0.8, 1.0})); |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 298 | |
| 299 | double distortion_factor = |
| 300 | ComputeDistortionFactor(orig_corner_points, corner_points); |
| 301 | |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 302 | // We get two estimates for poses. |
| 303 | // Choose the one with the lower estimation error |
| 304 | bool use_pose_1 = (pose_error_1 < pose_error_2); |
| 305 | auto best_pose = (use_pose_1 ? pose_1 : pose_2); |
| 306 | auto secondary_pose = (use_pose_1 ? pose_2 : pose_1); |
| 307 | double best_pose_error = (use_pose_1 ? pose_error_1 : pose_error_2); |
| 308 | double secondary_pose_error = (use_pose_1 ? pose_error_2 : pose_error_1); |
| 309 | |
| 310 | CHECK_NE(best_pose_error, std::numeric_limits<double>::infinity()) |
| 311 | << "Got no valid pose estimations, this should not be possible."; |
| 312 | double pose_error_ratio = best_pose_error / secondary_pose_error; |
| 313 | |
| 314 | // Destroy the secondary pose if we got one |
| 315 | if (secondary_pose_error != std::numeric_limits<double>::infinity()) { |
| 316 | DestroyPose(&secondary_pose); |
| 317 | } |
| 318 | |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 319 | results.emplace_back(Detection{.det = *det, |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 320 | .pose = best_pose, |
| 321 | .pose_error = best_pose_error, |
| 322 | .distortion_factor = distortion_factor, |
| 323 | .pose_error_ratio = pose_error_ratio}); |
| 324 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 325 | if (absl::GetFlag(FLAGS_visualize)) { |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 326 | // Draw raw (distorted) corner points in green |
| 327 | cv::line(color_image, orig_corner_points[0], orig_corner_points[1], |
| 328 | cv::Scalar(0, 255, 0), 2); |
| 329 | cv::line(color_image, orig_corner_points[1], orig_corner_points[2], |
| 330 | cv::Scalar(0, 255, 0), 2); |
| 331 | cv::line(color_image, orig_corner_points[2], orig_corner_points[3], |
| 332 | cv::Scalar(0, 255, 0), 2); |
| 333 | cv::line(color_image, orig_corner_points[3], orig_corner_points[0], |
| 334 | cv::Scalar(0, 255, 0), 2); |
| 335 | |
| 336 | // Draw undistorted corner points in red |
| 337 | cv::line(color_image, corner_points[0], corner_points[1], |
| 338 | cv::Scalar(0, 0, 255), 2); |
| 339 | cv::line(color_image, corner_points[2], corner_points[1], |
| 340 | cv::Scalar(0, 0, 255), 2); |
| 341 | cv::line(color_image, corner_points[2], corner_points[3], |
| 342 | cv::Scalar(0, 0, 255), 2); |
| 343 | cv::line(color_image, corner_points[0], corner_points[3], |
| 344 | cv::Scalar(0, 0, 255), 2); |
| 345 | } |
| 346 | |
| 347 | VLOG(1) << "Found tag number " << det->id << " hamming: " << det->hamming |
| 348 | << " margin: " << det->decision_margin; |
| 349 | |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 350 | } else { |
| 351 | rejections_++; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 354 | if (absl::GetFlag(FLAGS_visualize)) { |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 355 | // Display the result |
| 356 | // Rotate by 180 degrees to make it upright |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 357 | if (flip_image_) { |
| 358 | cv::rotate(color_image, color_image, 1); |
| 359 | } |
| 360 | cv::imshow(absl::StrCat("AprilRoboticsDetector Image ", node_name_), |
| 361 | color_image); |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 362 | } |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 363 | |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 364 | const auto corners_offset = builder.fbb()->CreateVector(foxglove_corners); |
Yash Chainani | 10b7b02 | 2023-02-22 14:34:04 -0800 | [diff] [blame] | 365 | foxglove::ImageAnnotations::Builder annotation_builder(*builder.fbb()); |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 366 | annotation_builder.add_points(corners_offset); |
| 367 | builder.CheckOk(builder.Send(annotation_builder.Finish())); |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 368 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 369 | apriltag_detections_destroy(detections); |
| 370 | |
| 371 | const aos::monotonic_clock::time_point end_time = aos::monotonic_clock::now(); |
| 372 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 373 | if (absl::GetFlag(FLAGS_debug)) { |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 374 | timeprofile_display(tag_detector_->tp); |
| 375 | } |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 376 | |
Austin Schuh | d266793 | 2023-02-04 16:22:39 -0800 | [diff] [blame] | 377 | VLOG(1) << "Took " << chrono::duration<double>(end_time - start_time).count() |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 378 | << " seconds to detect overall"; |
| 379 | |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 380 | return {.detections = results, .rejections = rejections_}; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 383 | } // namespace y2023::vision |