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 | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 42 | void CameraReader::ProcessImage(const cv::Mat &image_mat) { |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 43 | // Remember, we're getting YUYV images, so we start by converting to RGB |
| 44 | |
| 45 | // TOOD: Need to code this up for blob detection |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 46 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 47 | std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 48 | std::vector<BlobDetector::BlobStats> blob_stats; |
| 49 | cv::Mat binarized_image = |
| 50 | cv::Mat::zeros(cv::Size(image_mat.cols, image_mat.rows), CV_8UC1); |
| 51 | cv::Mat ret_image = |
| 52 | cv::Mat::zeros(cv::Size(image_mat.cols, image_mat.rows), CV_8UC3); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 53 | cv::Point centroid; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 54 | BlobDetector::ExtractBlobs(image_mat, binarized_image, ret_image, |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 55 | filtered_blobs, unfiltered_blobs, blob_stats, |
| 56 | centroid); |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 57 | TargetEstimateT target = TargetEstimator::EstimateTargetLocation( |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 58 | centroid, CameraIntrinsics(), CameraExtrinsics()); |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 59 | |
| 60 | auto builder = target_estimate_sender_.MakeBuilder(); |
| 61 | builder.CheckOk(builder.Send(TargetEstimate::Pack(*builder.fbb(), &target))); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void CameraReader::ReadImage() { |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 65 | // Path is for reading from the Disk. |
| 66 | if (FLAGS_image_png != "") { |
| 67 | std::vector<cv::String> file_list; |
| 68 | cv::glob(FLAGS_image_png + "/*.png", file_list, false); |
| 69 | |
| 70 | for (auto file : file_list) { |
| 71 | LOG(INFO) << "Reading file " << file; |
| 72 | cv::Mat rgb_image = cv::imread(file.c_str()); |
| 73 | ProcessImage(rgb_image); |
| 74 | } |
| 75 | event_loop_->Exit(); |
| 76 | return; |
| 77 | } |
| 78 | // 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] | 79 | if (!reader_->ReadLatestImage()) { |
| 80 | read_image_timer_->Setup(event_loop_->monotonic_now() + |
| 81 | std::chrono::milliseconds(10)); |
| 82 | return; |
| 83 | } |
| 84 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 85 | const CameraImage &image = reader_->LatestImage(); |
| 86 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
| 87 | CHECK(image_mat.isContinuous()); |
| 88 | |
| 89 | const int number_pixels = image.rows() * image.cols(); |
| 90 | for (int i = 0; i < number_pixels; ++i) { |
| 91 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 92 | image.data()->data()[i * 2]; |
| 93 | } |
| 94 | |
| 95 | ProcessImage(image_mat); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 96 | |
| 97 | reader_->SendLatestImage(); |
| 98 | read_image_timer_->Setup(event_loop_->monotonic_now()); |
| 99 | } |
| 100 | |
| 101 | } // namespace vision |
| 102 | } // namespace y2022 |