Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 1 | #include <map> |
| 2 | #include <opencv2/calib3d.hpp> |
| 3 | #include <opencv2/features2d.hpp> |
| 4 | #include <opencv2/highgui/highgui.hpp> |
| 5 | #include <opencv2/imgproc.hpp> |
| 6 | #include <random> |
| 7 | |
| 8 | #include "aos/events/shm_event_loop.h" |
| 9 | #include "aos/init.h" |
| 10 | #include "aos/time/time.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 11 | #include "frc971/vision/vision_generated.h" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 12 | #include "y2022/vision/blob_detector.h" |
milind-u | cafdd5d | 2022-03-01 19:58:57 -0800 | [diff] [blame] | 13 | #include "y2022/vision/calibration_data.h" |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 14 | #include "y2022/vision/target_estimate_generated.h" |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 15 | #include "y2022/vision/target_estimator.h" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 16 | |
| 17 | DEFINE_string(capture, "", |
| 18 | "If set, capture a single image and save it to this filename."); |
| 19 | DEFINE_string(channel, "/camera", "Channel name for the image."); |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 20 | DEFINE_string(config, "aos_config.json", "Path to the config file to use."); |
Jim Ostrowski | 5caf81c | 2022-01-30 21:02:19 -0800 | [diff] [blame] | 21 | DEFINE_string(png_dir, "", "Path to a set of images to display."); |
milind-u | cafdd5d | 2022-03-01 19:58:57 -0800 | [diff] [blame] | 22 | DEFINE_string(calibration_node, "", |
| 23 | "If reading locally, use the calibration for this node"); |
| 24 | DEFINE_int32( |
| 25 | calibration_team_number, 971, |
| 26 | "If reading locally, use the calibration for a node with this team number"); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 27 | DEFINE_uint64(skip, 0, |
| 28 | "Number of images to skip if doing local reading (png_dir set)."); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 29 | DEFINE_bool(show_features, true, "Show the blobs."); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 30 | DEFINE_bool(display_estimation, false, |
| 31 | "If true, display the target estimation graphically"); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 32 | |
| 33 | namespace y2022 { |
| 34 | namespace vision { |
| 35 | namespace { |
| 36 | |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 37 | using namespace frc971::vision; |
| 38 | |
| 39 | std::map<int64_t, BlobDetector::BlobResult> target_est_map; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 40 | aos::Fetcher<frc971::vision::CameraImage> image_fetcher; |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 41 | aos::Fetcher<y2022::vision::TargetEstimate> target_estimate_fetcher; |
| 42 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 43 | std::vector<cv::Point> FbsToCvPoints( |
| 44 | const flatbuffers::Vector<const Point *> &points_fbs) { |
| 45 | std::vector<cv::Point> points; |
| 46 | for (const Point *point : points_fbs) { |
| 47 | points.emplace_back(point->x(), point->y()); |
| 48 | } |
| 49 | return points; |
| 50 | } |
| 51 | |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 52 | std::vector<std::vector<cv::Point>> FbsToCvBlobs( |
James Kuszmaul | d230d7a | 2022-03-06 15:00:43 -0800 | [diff] [blame] | 53 | const flatbuffers::Vector<flatbuffers::Offset<Blob>> *blobs_fbs) { |
| 54 | if (blobs_fbs == nullptr) { |
| 55 | return {}; |
| 56 | } |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 57 | std::vector<std::vector<cv::Point>> blobs; |
James Kuszmaul | d230d7a | 2022-03-06 15:00:43 -0800 | [diff] [blame] | 58 | for (const auto blob : *blobs_fbs) { |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 59 | blobs.emplace_back(FbsToCvPoints(*blob->points())); |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 60 | } |
| 61 | return blobs; |
| 62 | } |
| 63 | |
| 64 | std::vector<BlobDetector::BlobStats> FbsToBlobStats( |
| 65 | const flatbuffers::Vector<flatbuffers::Offset<BlobStatsFbs>> |
| 66 | &blob_stats_fbs) { |
| 67 | std::vector<BlobDetector::BlobStats> blob_stats; |
| 68 | for (const auto stats_fbs : blob_stats_fbs) { |
| 69 | cv::Point centroid{stats_fbs->centroid()->x(), stats_fbs->centroid()->y()}; |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame] | 70 | cv::Size size{stats_fbs->size()->width(), stats_fbs->size()->height()}; |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 71 | blob_stats.emplace_back(BlobDetector::BlobStats{ |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame] | 72 | centroid, size, stats_fbs->aspect_ratio(), stats_fbs->area(), |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 73 | static_cast<size_t>(stats_fbs->num_points())}); |
| 74 | } |
| 75 | return blob_stats; |
| 76 | } |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 77 | |
| 78 | bool DisplayLoop() { |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 79 | int64_t target_timestamp = 0; |
| 80 | if (target_estimate_fetcher.Fetch()) { |
| 81 | const TargetEstimate *target_est = target_estimate_fetcher.get(); |
| 82 | CHECK(target_est != nullptr) |
| 83 | << "Got null when trying to fetch target estimate"; |
| 84 | |
| 85 | target_timestamp = target_est->image_monotonic_timestamp_ns(); |
| 86 | if (target_est->blob_result()->filtered_blobs()->size() > 0) { |
| 87 | VLOG(2) << "Got blobs for timestamp " << target_est << "\n"; |
| 88 | } |
| 89 | // Store the TargetEstimate data so we can match timestamp with image |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 90 | target_est_map[target_timestamp] = BlobDetector::BlobResult{ |
| 91 | cv::Mat(), |
James Kuszmaul | d230d7a | 2022-03-06 15:00:43 -0800 | [diff] [blame] | 92 | FbsToCvBlobs(target_est->blob_result()->filtered_blobs()), |
| 93 | FbsToCvBlobs(target_est->blob_result()->unfiltered_blobs()), |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 94 | FbsToBlobStats(*target_est->blob_result()->blob_stats()), |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame] | 95 | FbsToBlobStats(*target_est->blob_result()->filtered_stats()), |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 96 | cv::Point{target_est->blob_result()->centroid()->x(), |
| 97 | target_est->blob_result()->centroid()->y()}}; |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 98 | // Only keep last 10 matches |
| 99 | while (target_est_map.size() > 10u) { |
| 100 | target_est_map.erase(target_est_map.begin()); |
| 101 | } |
| 102 | } |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 103 | int64_t image_timestamp = 0; |
Jim Ostrowski | 5caf81c | 2022-01-30 21:02:19 -0800 | [diff] [blame] | 104 | if (!image_fetcher.Fetch()) { |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 105 | VLOG(2) << "Couldn't fetch image"; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 106 | return true; |
| 107 | } |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 108 | const CameraImage *image = image_fetcher.get(); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 109 | CHECK(image != nullptr) << "Couldn't read image"; |
| 110 | image_timestamp = image->monotonic_timestamp_ns(); |
| 111 | VLOG(2) << "Got image at timestamp: " << image_timestamp; |
| 112 | |
| 113 | // Create color image: |
| 114 | cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2, |
| 115 | (void *)image->data()->data()); |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 116 | cv::Mat bgr_image(cv::Size(image->cols(), image->rows()), CV_8UC3); |
Milind Upadhyay | 4052294 | 2022-02-19 15:30:31 -0800 | [diff] [blame] | 117 | cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 118 | |
| 119 | if (!FLAGS_capture.empty()) { |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 120 | cv::imwrite(FLAGS_capture, bgr_image); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 124 | auto target_est_it = target_est_map.find(image_timestamp); |
| 125 | if (target_est_it != target_est_map.end()) { |
| 126 | LOG(INFO) << image->monotonic_timestamp_ns() << ": # unfiltered blobs: " |
| 127 | << target_est_it->second.unfiltered_blobs.size() |
| 128 | << "; # filtered blobs: " |
| 129 | << target_est_it->second.filtered_blobs.size(); |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 130 | |
Jim Ostrowski | 210765a | 2022-02-27 12:52:14 -0800 | [diff] [blame] | 131 | cv::Mat ret_image = |
| 132 | cv::Mat::zeros(cv::Size(image->cols(), image->rows()), CV_8UC3); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 133 | BlobDetector::DrawBlobs(target_est_it->second, ret_image); |
Jim Ostrowski | 46a7838 | 2022-02-06 14:05:58 -0800 | [diff] [blame] | 134 | cv::imshow("blobs", ret_image); |
| 135 | } |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 136 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 137 | cv::imshow("image", bgr_image); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 138 | |
| 139 | int keystroke = cv::waitKey(1); |
| 140 | if ((keystroke & 0xFF) == static_cast<int>('c')) { |
| 141 | // Convert again, to get clean image |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 142 | cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 143 | std::stringstream name; |
| 144 | name << "capture-" << aos::realtime_clock::now() << ".png"; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 145 | cv::imwrite(name.str(), bgr_image); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 146 | LOG(INFO) << "Saved image file: " << name.str(); |
| 147 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
| 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | void ViewerMain() { |
| 154 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 155 | aos::configuration::ReadConfig(FLAGS_config); |
| 156 | |
| 157 | aos::ShmEventLoop event_loop(&config.message()); |
| 158 | |
| 159 | image_fetcher = |
| 160 | event_loop.MakeFetcher<frc971::vision::CameraImage>(FLAGS_channel); |
| 161 | |
Jim Ostrowski | 46a7838 | 2022-02-06 14:05:58 -0800 | [diff] [blame] | 162 | target_estimate_fetcher = |
| 163 | event_loop.MakeFetcher<y2022::vision::TargetEstimate>(FLAGS_channel); |
| 164 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 165 | // Run the display loop |
| 166 | event_loop.AddPhasedLoop( |
| 167 | [&event_loop](int) { |
| 168 | if (!DisplayLoop()) { |
| 169 | LOG(INFO) << "Calling event_loop Exit"; |
| 170 | event_loop.Exit(); |
| 171 | }; |
| 172 | }, |
| 173 | ::std::chrono::milliseconds(100)); |
| 174 | |
| 175 | event_loop.Run(); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 176 | } |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 177 | |
| 178 | // TODO(milind): delete this when viewer can accumulate local images and results |
| 179 | void ViewerLocal() { |
| 180 | std::vector<cv::String> file_list; |
| 181 | cv::glob(FLAGS_png_dir + "/*.png", file_list, false); |
| 182 | |
milind-u | cafdd5d | 2022-03-01 19:58:57 -0800 | [diff] [blame] | 183 | const aos::FlatbufferSpan<calibration::CalibrationData> calibration_data( |
| 184 | CalibrationData()); |
| 185 | |
| 186 | const calibration::CameraCalibration *calibration = nullptr; |
| 187 | for (const calibration::CameraCalibration *candidate : |
| 188 | *calibration_data.message().camera_calibrations()) { |
| 189 | if ((candidate->node_name()->string_view() == FLAGS_calibration_node) && |
| 190 | (candidate->team_number() == FLAGS_calibration_team_number)) { |
| 191 | calibration = candidate; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | CHECK(calibration) << "No calibration data found for node \"" |
| 197 | << FLAGS_calibration_node << "\" with team number " |
| 198 | << FLAGS_calibration_team_number; |
| 199 | |
| 200 | auto intrinsics_float = cv::Mat(3, 3, CV_32F, |
| 201 | const_cast<void *>(static_cast<const void *>( |
| 202 | calibration->intrinsics()->data()))); |
| 203 | cv::Mat intrinsics; |
| 204 | intrinsics_float.convertTo(intrinsics, CV_64F); |
| 205 | |
| 206 | const auto extrinsics_float = |
| 207 | cv::Mat(4, 4, CV_32F, |
| 208 | const_cast<void *>(static_cast<const void *>( |
| 209 | calibration->fixed_extrinsics()->data()->data()))); |
| 210 | cv::Mat extrinsics; |
| 211 | extrinsics_float.convertTo(extrinsics, CV_64F); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 212 | |
| 213 | TargetEstimator estimator(intrinsics, extrinsics); |
| 214 | |
Milind Upadhyay | ae99872 | 2022-03-13 12:45:55 -0700 | [diff] [blame^] | 215 | for (auto it = file_list.begin() + FLAGS_skip; it < file_list.end(); it++) { |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 216 | LOG(INFO) << "Reading file " << *it; |
| 217 | cv::Mat image_mat = cv::imread(it->c_str()); |
| 218 | BlobDetector::BlobResult blob_result; |
| 219 | blob_result.binarized_image = |
| 220 | cv::Mat::zeros(cv::Size(image_mat.cols, image_mat.rows), CV_8UC1); |
| 221 | BlobDetector::ExtractBlobs(image_mat, &blob_result); |
| 222 | |
| 223 | cv::Mat ret_image = |
| 224 | cv::Mat::zeros(cv::Size(image_mat.cols, image_mat.rows), CV_8UC3); |
| 225 | BlobDetector::DrawBlobs(blob_result, ret_image); |
| 226 | |
| 227 | LOG(INFO) << ": # blobs: " << blob_result.filtered_blobs.size() |
| 228 | << " (# removed: " |
| 229 | << blob_result.unfiltered_blobs.size() - |
| 230 | blob_result.filtered_blobs.size() |
| 231 | << ")"; |
| 232 | |
| 233 | if (blob_result.filtered_blobs.size() > 0) { |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame] | 234 | estimator.Solve(blob_result.filtered_stats, |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 235 | FLAGS_display_estimation ? std::make_optional(ret_image) |
| 236 | : std::nullopt); |
| 237 | estimator.DrawEstimate(ret_image); |
| 238 | } |
| 239 | |
| 240 | cv::imshow("image", image_mat); |
| 241 | cv::imshow("mask", blob_result.binarized_image); |
| 242 | cv::imshow("blobs", ret_image); |
| 243 | |
| 244 | int keystroke = cv::waitKey(0); |
| 245 | if ((keystroke & 0xFF) == static_cast<int>('q')) { |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | } |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 250 | } // namespace |
| 251 | } // namespace vision |
| 252 | } // namespace y2022 |
| 253 | |
| 254 | // Quick and lightweight viewer for images |
| 255 | int main(int argc, char **argv) { |
| 256 | aos::InitGoogle(&argc, &argv); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 257 | if (FLAGS_png_dir != "") |
| 258 | y2022::vision::ViewerLocal(); |
| 259 | else |
| 260 | y2022::vision::ViewerMain(); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 261 | } |