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