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" |
| 8 | #include "aos/flatbuffer_merge.h" |
| 9 | #include "aos/network/team_number.h" |
| 10 | #include "frc971/vision/v4l2_reader.h" |
| 11 | #include "frc971/vision/vision_generated.h" |
| 12 | #include "y2020/vision/sift/sift_generated.h" |
| 13 | #include "y2020/vision/sift/sift_training_generated.h" |
| 14 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
| 15 | |
| 16 | namespace y2022 { |
| 17 | namespace vision { |
| 18 | |
| 19 | using namespace frc971::vision; |
| 20 | |
| 21 | const sift::CameraCalibration *CameraReader::FindCameraCalibration() const { |
| 22 | const std::string_view node_name = event_loop_->node()->name()->string_view(); |
| 23 | const int team_number = aos::network::GetTeamNumber(); |
| 24 | for (const sift::CameraCalibration *candidate : |
| 25 | *training_data_->camera_calibrations()) { |
| 26 | if (candidate->node_name()->string_view() != node_name) { |
| 27 | continue; |
| 28 | } |
| 29 | if (candidate->team_number() != team_number) { |
| 30 | continue; |
| 31 | } |
| 32 | return candidate; |
| 33 | } |
| 34 | LOG(FATAL) << ": Failed to find camera calibration for " << node_name |
| 35 | << " on " << team_number; |
| 36 | } |
| 37 | |
| 38 | void CameraReader::ProcessImage(const CameraImage &image) { |
| 39 | // Remember, we're getting YUYV images, so we start by converting to RGB |
| 40 | |
| 41 | // TOOD: Need to code this up for blob detection |
| 42 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
| 43 | CHECK(image_mat.isContinuous()); |
| 44 | const int number_pixels = image.rows() * image.cols(); |
| 45 | for (int i = 0; i < number_pixels; ++i) { |
| 46 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 47 | image.data()->data()[i * 2]; |
| 48 | } |
| 49 | |
| 50 | // Now, send our two messages-- one large, with details for remote |
| 51 | // debugging(features), and one smaller |
| 52 | // TODO: Send blobdetection and pose results |
| 53 | } |
| 54 | |
| 55 | void CameraReader::ReadImage() { |
| 56 | if (!reader_->ReadLatestImage()) { |
| 57 | read_image_timer_->Setup(event_loop_->monotonic_now() + |
| 58 | std::chrono::milliseconds(10)); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | ProcessImage(reader_->LatestImage()); |
| 63 | |
| 64 | reader_->SendLatestImage(); |
| 65 | read_image_timer_->Setup(event_loop_->monotonic_now()); |
| 66 | } |
| 67 | |
| 68 | } // namespace vision |
| 69 | } // namespace y2022 |