Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 1 | #include "y2022/vision/camera_reader.h" |
| 2 | |
| 3 | #include <math.h> |
| 4 | |
| 5 | #include <opencv2/imgproc.hpp> |
| 6 | |
| 7 | #include "aos/events/event_loop.h" |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 8 | #include "aos/events/shm_event_loop.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 9 | #include "aos/flatbuffer_merge.h" |
| 10 | #include "aos/network/team_number.h" |
| 11 | #include "frc971/vision/v4l2_reader.h" |
| 12 | #include "frc971/vision/vision_generated.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 13 | #include "y2022/vision/blob_detector.h" |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 14 | #include "y2022/vision/calibration_generated.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 15 | #include "y2022/vision/target_estimator.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 16 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 17 | DEFINE_string(image_png, "", "A set of PNG images"); |
| 18 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 19 | namespace y2022 { |
| 20 | namespace vision { |
| 21 | |
| 22 | using namespace frc971::vision; |
| 23 | |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 24 | const calibration::CameraCalibration *CameraReader::FindCameraCalibration() |
| 25 | const { |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 26 | const std::string_view node_name = event_loop_->node()->name()->string_view(); |
| 27 | const int team_number = aos::network::GetTeamNumber(); |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 28 | for (const calibration::CameraCalibration *candidate : |
| 29 | *calibration_data_->camera_calibrations()) { |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 30 | if (candidate->node_name()->string_view() != node_name) { |
| 31 | continue; |
| 32 | } |
| 33 | if (candidate->team_number() != team_number) { |
| 34 | continue; |
| 35 | } |
| 36 | return candidate; |
| 37 | } |
| 38 | LOG(FATAL) << ": Failed to find camera calibration for " << node_name |
| 39 | << " on " << team_number; |
| 40 | } |
| 41 | |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame^] | 42 | namespace { |
| 43 | // Converts a vector of cv::Point to PointT for the flatbuffer |
| 44 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Blob>>> |
| 45 | CvBlobsToFbs(const std::vector<std::vector<cv::Point>> &blobs, |
| 46 | aos::Sender<TargetEstimate>::Builder &builder) { |
| 47 | std::vector<flatbuffers::Offset<Blob>> blobs_fbs; |
| 48 | for (auto &blob : blobs) { |
| 49 | std::vector<Point> points_fbs; |
| 50 | for (auto p : blob) { |
| 51 | points_fbs.push_back(Point{p.x, p.y}); |
| 52 | } |
| 53 | auto points_offset = builder.fbb()->CreateVectorOfStructs(points_fbs); |
| 54 | auto blob_builder = builder.MakeBuilder<Blob>(); |
| 55 | blob_builder.add_points(points_offset); |
| 56 | blobs_fbs.emplace_back(blob_builder.Finish()); |
| 57 | } |
| 58 | return builder.fbb()->CreateVector(blobs_fbs.data(), blobs_fbs.size()); |
| 59 | } |
| 60 | |
| 61 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<BlobStatsFbs>>> |
| 62 | BlobStatsToFbs(const std::vector<BlobDetector::BlobStats> blob_stats, |
| 63 | aos::Sender<TargetEstimate>::Builder &builder) { |
| 64 | std::vector<flatbuffers::Offset<BlobStatsFbs>> stats_fbs; |
| 65 | for (auto &stats : blob_stats) { |
| 66 | // Make BlobStatsFbs builder then fill each field using the BlobStats |
| 67 | // struct, then you finish it and add it to stats_fbs. |
| 68 | auto stats_builder = builder.MakeBuilder<BlobStatsFbs>(); |
| 69 | Point centroid_fbs = Point{stats.centroid.x, stats.centroid.y}; |
| 70 | stats_builder.add_centroid(¢roid_fbs); |
| 71 | stats_builder.add_aspect_ratio(stats.aspect_ratio); |
| 72 | stats_builder.add_area(stats.area); |
| 73 | stats_builder.add_num_points(stats.num_points); |
| 74 | |
| 75 | auto current_stats = stats_builder.Finish(); |
| 76 | stats_fbs.emplace_back(current_stats); |
| 77 | } |
| 78 | return builder.fbb()->CreateVector(stats_fbs.data(), stats_fbs.size()); |
| 79 | } |
| 80 | } // namespace |
| 81 | |
Milind Upadhyay | 2b4404c | 2022-02-04 21:20:57 -0800 | [diff] [blame] | 82 | void CameraReader::ProcessImage(cv::Mat image_mat) { |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 83 | // Remember, we're getting YUYV images, so we start by converting to RGB |
| 84 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 85 | std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 86 | std::vector<BlobDetector::BlobStats> blob_stats; |
| 87 | cv::Mat binarized_image = |
| 88 | cv::Mat::zeros(cv::Size(image_mat.cols, image_mat.rows), CV_8UC1); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 89 | cv::Point centroid; |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame^] | 90 | BlobDetector::ExtractBlobs(image_mat, binarized_image, filtered_blobs, |
| 91 | unfiltered_blobs, blob_stats, centroid); |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 92 | auto builder = target_estimate_sender_.MakeBuilder(); |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame^] | 93 | flatbuffers::Offset<BlobResult> blob_result_offset; |
| 94 | { |
| 95 | const auto filtered_blobs_offset = CvBlobsToFbs(filtered_blobs, builder); |
| 96 | const auto unfiltered_blobs_offset = |
| 97 | CvBlobsToFbs(unfiltered_blobs, builder); |
| 98 | const auto blob_stats_offset = BlobStatsToFbs(blob_stats, builder); |
| 99 | const Point centroid_fbs = Point{centroid.x, centroid.y}; |
| 100 | |
| 101 | auto blob_result_builder = builder.MakeBuilder<BlobResult>(); |
| 102 | blob_result_builder.add_filtered_blobs(filtered_blobs_offset); |
| 103 | blob_result_builder.add_unfiltered_blobs(unfiltered_blobs_offset); |
| 104 | blob_result_builder.add_blob_stats(blob_stats_offset); |
| 105 | blob_result_builder.add_centroid(¢roid_fbs); |
| 106 | blob_result_offset = blob_result_builder.Finish(); |
| 107 | } |
| 108 | |
| 109 | auto target_estimate_builder = builder.MakeBuilder<TargetEstimate>(); |
| 110 | TargetEstimator::EstimateTargetLocation(centroid, CameraIntrinsics(), |
| 111 | CameraExtrinsics(), |
| 112 | &target_estimate_builder); |
| 113 | target_estimate_builder.add_blob_result(blob_result_offset); |
| 114 | |
| 115 | builder.CheckOk(builder.Send(target_estimate_builder.Finish())); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void CameraReader::ReadImage() { |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 119 | // Path is for reading from the Disk. |
| 120 | if (FLAGS_image_png != "") { |
| 121 | std::vector<cv::String> file_list; |
| 122 | cv::glob(FLAGS_image_png + "/*.png", file_list, false); |
| 123 | |
| 124 | for (auto file : file_list) { |
| 125 | LOG(INFO) << "Reading file " << file; |
| 126 | cv::Mat rgb_image = cv::imread(file.c_str()); |
| 127 | ProcessImage(rgb_image); |
| 128 | } |
| 129 | event_loop_->Exit(); |
| 130 | return; |
| 131 | } |
| 132 | // If we are not reading from the disk, we read the live camera stream. |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 133 | if (!reader_->ReadLatestImage()) { |
| 134 | read_image_timer_->Setup(event_loop_->monotonic_now() + |
| 135 | std::chrono::milliseconds(10)); |
| 136 | return; |
| 137 | } |
| 138 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 139 | const CameraImage &image = reader_->LatestImage(); |
| 140 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
| 141 | CHECK(image_mat.isContinuous()); |
| 142 | |
| 143 | const int number_pixels = image.rows() * image.cols(); |
| 144 | for (int i = 0; i < number_pixels; ++i) { |
| 145 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 146 | image.data()->data()[i * 2]; |
| 147 | } |
| 148 | |
| 149 | ProcessImage(image_mat); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 150 | |
| 151 | reader_->SendLatestImage(); |
| 152 | read_image_timer_->Setup(event_loop_->monotonic_now()); |
| 153 | } |
| 154 | |
| 155 | } // namespace vision |
| 156 | } // namespace y2022 |