Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 1 | #ifndef Y2022_VISION_CAMERA_READER_H_ |
| 2 | #define Y2022_VISION_CAMERA_READER_H_ |
| 3 | |
| 4 | #include <math.h> |
| 5 | |
| 6 | #include <opencv2/calib3d.hpp> |
| 7 | #include <opencv2/features2d.hpp> |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 8 | #include <opencv2/imgcodecs.hpp> |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 9 | #include <opencv2/imgproc.hpp> |
| 10 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 11 | #include "aos/events/shm_event_loop.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 12 | #include "aos/flatbuffer_merge.h" |
| 13 | #include "aos/network/team_number.h" |
| 14 | #include "frc971/vision/v4l2_reader.h" |
| 15 | #include "frc971/vision/vision_generated.h" |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 16 | #include "y2022/vision/calibration_data.h" |
| 17 | #include "y2022/vision/calibration_generated.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 18 | #include "y2022/vision/target_estimate_generated.h" |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 19 | |
| 20 | namespace y2022 { |
| 21 | namespace vision { |
| 22 | |
| 23 | using namespace frc971::vision; |
| 24 | |
| 25 | // TODO<Jim/Milind>: Need to add in senders to send out the blob data/stats |
| 26 | |
| 27 | class CameraReader { |
| 28 | public: |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 29 | CameraReader(aos::ShmEventLoop *event_loop, |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 30 | const calibration::CalibrationData *calibration_data, |
| 31 | V4L2Reader *reader) |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 32 | : event_loop_(event_loop), |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 33 | calibration_data_(calibration_data), |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 34 | camera_calibration_(FindCameraCalibration()), |
| 35 | reader_(reader), |
| 36 | image_sender_(event_loop->MakeSender<CameraImage>("/camera")), |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 37 | target_estimate_sender_( |
| 38 | event_loop->MakeSender<TargetEstimate>("/camera")), |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 39 | read_image_timer_(event_loop->AddTimer([this]() { ReadImage(); })) { |
| 40 | event_loop->OnRun( |
| 41 | [this]() { read_image_timer_->Setup(event_loop_->monotonic_now()); }); |
| 42 | } |
| 43 | |
| 44 | private: |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 45 | const calibration::CameraCalibration *FindCameraCalibration() const; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 46 | |
| 47 | // Processes an image (including sending the results). |
Milind Upadhyay | 2b4404c | 2022-02-04 21:20:57 -0800 | [diff] [blame] | 48 | void ProcessImage(cv::Mat image); |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 49 | |
| 50 | // Reads an image, and then performs all of our processing on it. |
| 51 | void ReadImage(); |
| 52 | |
| 53 | cv::Mat CameraIntrinsics() const { |
| 54 | const cv::Mat result(3, 3, CV_32F, |
| 55 | const_cast<void *>(static_cast<const void *>( |
| 56 | camera_calibration_->intrinsics()->data()))); |
| 57 | CHECK_EQ(result.total(), camera_calibration_->intrinsics()->size()); |
| 58 | return result; |
| 59 | } |
| 60 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 61 | cv::Mat CameraExtrinsics() const { |
| 62 | const cv::Mat result( |
| 63 | 4, 4, CV_32F, |
| 64 | const_cast<void *>(static_cast<const void *>( |
| 65 | camera_calibration_->fixed_extrinsics()->data()->data()))); |
| 66 | CHECK_EQ(result.total(), |
| 67 | camera_calibration_->fixed_extrinsics()->data()->size()); |
| 68 | return result; |
| 69 | } |
| 70 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 71 | cv::Mat CameraDistCoeffs() const { |
| 72 | const cv::Mat result(5, 1, CV_32F, |
| 73 | const_cast<void *>(static_cast<const void *>( |
| 74 | camera_calibration_->dist_coeffs()->data()))); |
| 75 | CHECK_EQ(result.total(), camera_calibration_->dist_coeffs()->size()); |
| 76 | return result; |
| 77 | } |
| 78 | |
Henry Speiser | 1f34eea | 2022-01-30 14:35:21 -0800 | [diff] [blame] | 79 | aos::ShmEventLoop *const event_loop_; |
Jim Ostrowski | 007e2ea | 2022-01-30 13:13:26 -0800 | [diff] [blame] | 80 | const calibration::CalibrationData *const calibration_data_; |
| 81 | const calibration::CameraCalibration *const camera_calibration_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 82 | V4L2Reader *const reader_; |
| 83 | aos::Sender<CameraImage> image_sender_; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 84 | aos::Sender<TargetEstimate> target_estimate_sender_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 85 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 86 | // We schedule this immediately to read an image. Having it on a timer |
| 87 | // means other things can run on the event loop in between. |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 88 | aos::TimerHandler *const read_image_timer_; |
| 89 | }; |
| 90 | |
| 91 | } // namespace vision |
| 92 | } // namespace y2022 |
| 93 | #endif // Y2022_VISION_CAMERA_READER_H_ |