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