Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 1 | #ifndef _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_ |
| 2 | #define _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_ |
| 3 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 4 | #include "aos/util/global_factory.h" |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 5 | #include "aos/vision/blob/range_image.h" |
| 6 | #include "aos/vision/events/epoll_events.h" |
Parker Schuh | cd258b8 | 2017-04-09 16:28:29 -0700 | [diff] [blame] | 7 | #include "aos/vision/image/camera_params.pb.h" |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 8 | #include "aos/vision/image/image_types.h" |
| 9 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 10 | namespace aos::vision { |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 11 | |
| 12 | class BlobStreamViewer; |
| 13 | |
| 14 | // Implement per-filter to draw debug viewer information from the filter to |
| 15 | // the debug BlobStreamViewer. |
| 16 | class FilterHarness { |
| 17 | public: |
| 18 | virtual ~FilterHarness() {} |
| 19 | |
| 20 | // Apply the filter-specific thresholding logic. |
| 21 | // Blob sources may not have this called at all. |
| 22 | virtual RangeImage Threshold(ImagePtr image) = 0; |
| 23 | |
| 24 | // Each filter can only be used by one debug viewer. This will |
| 25 | // get called before calling any other methods. |
| 26 | virtual void InstallViewer(BlobStreamViewer * /*viewer*/) {} |
| 27 | |
| 28 | // One frame worth of blobs. Returns if the frame is "interesting". |
| 29 | virtual bool HandleBlobs(BlobList imgs, ImageFormat fmt) = 0; |
Parker Schuh | cd258b8 | 2017-04-09 16:28:29 -0700 | [diff] [blame] | 30 | |
| 31 | // One frame worth of blobs. Returns if the frame is "interesting". |
| 32 | // Fast version that does no drawing. |
| 33 | virtual bool JustCheckForTarget(BlobList imgs, ImageFormat fmt) { |
| 34 | return HandleBlobs(std::move(imgs), fmt); |
| 35 | } |
| 36 | |
| 37 | // Register key press handler. |
| 38 | virtual std::function<void(uint32_t)> RegisterKeyPress() { |
| 39 | return std::function<void(uint32_t)>(); |
| 40 | } |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 41 | |
| 42 | // The DebugFramework will tells us where to call to get the camera. |
| 43 | void InstallSetExposure(std::function<void(int)> set_exp) { |
| 44 | set_exposure_ = set_exp; |
| 45 | } |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 46 | void SetExposure(int abs_exp) { set_exposure_(abs_exp); } |
| 47 | |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 48 | private: |
| 49 | std::function<void(int)> set_exposure_; |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // For ImageSource implementations only. Allows registering key press events |
| 53 | // and installing new blob lists and jpegs. |
| 54 | class DebugFrameworkInterface { |
| 55 | public: |
| 56 | virtual ~DebugFrameworkInterface() {} |
| 57 | |
| 58 | void InstallKeyPress(std::function<void(uint32_t)> key_press_event) { |
| 59 | key_press_events_.emplace_back(std::move(key_press_event)); |
| 60 | } |
| 61 | |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 62 | // The camera will tell us where to call to set exposure. |
| 63 | void InstallSetExposure(std::function<void(int)> set_exp) { |
| 64 | set_exposure_ = std::move(set_exp); |
| 65 | } |
| 66 | void SetExposure(int abs_exp) { set_exposure_(abs_exp); } |
| 67 | |
Parker Schuh | cd258b8 | 2017-04-09 16:28:29 -0700 | [diff] [blame] | 68 | // The return value bool here for all of these is |
| 69 | // if the frame is "interesting" ie has a target. |
| 70 | virtual bool NewJpeg(DataRef data) = 0; |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 71 | |
Parker Schuh | 02f13f6 | 2019-02-16 16:42:41 -0800 | [diff] [blame] | 72 | virtual bool NewImage(ImageFormat fmt, |
| 73 | const std::function<bool(ImagePtr data)> &process) = 0; |
| 74 | |
Parker Schuh | cd258b8 | 2017-04-09 16:28:29 -0700 | [diff] [blame] | 75 | virtual bool NewBlobList(BlobList blob_list, ImageFormat fmt) = 0; |
| 76 | |
| 77 | virtual bool JustCheckForTarget(BlobList imgs, ImageFormat fmt) = 0; |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 78 | |
| 79 | // Expose a EpollLoop to allow waiting for events. |
| 80 | virtual aos::events::EpollLoop *Loop() = 0; |
| 81 | |
Parker Schuh | cd258b8 | 2017-04-09 16:28:29 -0700 | [diff] [blame] | 82 | virtual const CameraParams &camera_params() = 0; |
| 83 | |
| 84 | virtual BlobStreamViewer *viewer() = 0; |
| 85 | |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 86 | protected: |
| 87 | const std::vector<std::function<void(uint32_t)>> &key_press_events() { |
| 88 | return key_press_events_; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | std::vector<std::function<void(uint32_t)>> key_press_events_; |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 93 | |
| 94 | std::function<void(int)> set_exposure_; |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // Implemented by each source type. Will stream frames to |
| 98 | // DebugFrameworkInterface. |
| 99 | class ImageSource { |
| 100 | public: |
| 101 | virtual ~ImageSource() {} |
| 102 | |
| 103 | // Printed when you call: debug_viewer help. |
| 104 | virtual const char *GetHelpMessage() { return " No help string :(\n"; } |
| 105 | |
| 106 | // Start streaming frames to DebugFrameworkInterface. |
| 107 | virtual void Init(const std::string &args, |
| 108 | DebugFrameworkInterface *interface) = 0; |
| 109 | }; |
| 110 | |
| 111 | // Factory for ImageSource. |
| 112 | SETUP_FACTORY(ImageSource); |
| 113 | |
| 114 | #define REGISTER_IMAGE_SOURCE(key, SubClass) \ |
| 115 | REGISTER_SUBCLASS_BY_KEY(key, ::aos::vision::ImageSource, SubClass) |
| 116 | |
| 117 | // Runs loop and never returns. |
| 118 | // Feeds into a generic filter. |
Parker Schuh | cd258b8 | 2017-04-09 16:28:29 -0700 | [diff] [blame] | 119 | void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter, |
| 120 | CameraParams camera_params); |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 121 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 122 | } // namespace aos::vision |
Parker Schuh | 9064111 | 2017-02-25 12:18:36 -0800 | [diff] [blame] | 123 | |
| 124 | #endif // _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_ |