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> |
| 8 | #include <opencv2/imgproc.hpp> |
| 9 | |
| 10 | #include "aos/events/event_loop.h" |
| 11 | #include "aos/flatbuffer_merge.h" |
| 12 | #include "aos/network/team_number.h" |
| 13 | #include "frc971/vision/v4l2_reader.h" |
| 14 | #include "frc971/vision/vision_generated.h" |
| 15 | #include "y2020/vision/sift/sift_generated.h" |
| 16 | #include "y2020/vision/sift/sift_training_generated.h" |
| 17 | #include "y2020/vision/tools/python_code/sift_training_data.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: |
| 29 | CameraReader(aos::EventLoop *event_loop, |
| 30 | const sift::TrainingData *training_data, V4L2Reader *reader) |
| 31 | : event_loop_(event_loop), |
| 32 | training_data_(training_data), |
| 33 | camera_calibration_(FindCameraCalibration()), |
| 34 | reader_(reader), |
| 35 | image_sender_(event_loop->MakeSender<CameraImage>("/camera")), |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 36 | target_estimate_sender_( |
| 37 | event_loop->MakeSender<TargetEstimate>("/camera")), |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 38 | read_image_timer_(event_loop->AddTimer([this]() { ReadImage(); })) { |
| 39 | event_loop->OnRun( |
| 40 | [this]() { read_image_timer_->Setup(event_loop_->monotonic_now()); }); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | const sift::CameraCalibration *FindCameraCalibration() const; |
| 45 | |
| 46 | // Processes an image (including sending the results). |
| 47 | void ProcessImage(const CameraImage &image); |
| 48 | |
| 49 | // Reads an image, and then performs all of our processing on it. |
| 50 | void ReadImage(); |
| 51 | |
| 52 | cv::Mat CameraIntrinsics() const { |
| 53 | const cv::Mat result(3, 3, CV_32F, |
| 54 | const_cast<void *>(static_cast<const void *>( |
| 55 | camera_calibration_->intrinsics()->data()))); |
| 56 | CHECK_EQ(result.total(), camera_calibration_->intrinsics()->size()); |
| 57 | return result; |
| 58 | } |
| 59 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 60 | cv::Mat CameraExtrinsics() const { |
| 61 | const cv::Mat result( |
| 62 | 4, 4, CV_32F, |
| 63 | const_cast<void *>(static_cast<const void *>( |
| 64 | camera_calibration_->fixed_extrinsics()->data()->data()))); |
| 65 | CHECK_EQ(result.total(), |
| 66 | camera_calibration_->fixed_extrinsics()->data()->size()); |
| 67 | return result; |
| 68 | } |
| 69 | |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 70 | cv::Mat CameraDistCoeffs() const { |
| 71 | const cv::Mat result(5, 1, CV_32F, |
| 72 | const_cast<void *>(static_cast<const void *>( |
| 73 | camera_calibration_->dist_coeffs()->data()))); |
| 74 | CHECK_EQ(result.total(), camera_calibration_->dist_coeffs()->size()); |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | aos::EventLoop *const event_loop_; |
| 79 | const sift::TrainingData *const training_data_; |
| 80 | const sift::CameraCalibration *const camera_calibration_; |
| 81 | V4L2Reader *const reader_; |
| 82 | aos::Sender<CameraImage> image_sender_; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 83 | aos::Sender<TargetEstimate> target_estimate_sender_; |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 84 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 85 | // We schedule this immediately to read an image. Having it on a timer |
| 86 | // means other things can run on the event loop in between. |
Jim Ostrowski | ff7b3de | 2022-01-22 22:20:26 -0800 | [diff] [blame] | 87 | aos::TimerHandler *const read_image_timer_; |
| 88 | }; |
| 89 | |
| 90 | } // namespace vision |
| 91 | } // namespace y2022 |
| 92 | #endif // Y2022_VISION_CAMERA_READER_H_ |