blob: b9fa166da725307739e1c40d561112ed34a492b9 [file] [log] [blame]
Parker Schuh90641112017-02-25 12:18:36 -08001#ifndef _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_
2#define _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_
3
John Park33858a32018-09-28 23:05:48 -07004#include "aos/util/global_factory.h"
Parker Schuh90641112017-02-25 12:18:36 -08005#include "aos/vision/blob/range_image.h"
6#include "aos/vision/events/epoll_events.h"
Parker Schuhcd258b82017-04-09 16:28:29 -07007#include "aos/vision/image/camera_params.pb.h"
Parker Schuh90641112017-02-25 12:18:36 -08008#include "aos/vision/image/image_types.h"
9
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080010namespace aos::vision {
Parker Schuh90641112017-02-25 12:18:36 -080011
12class BlobStreamViewer;
13
14// Implement per-filter to draw debug viewer information from the filter to
15// the debug BlobStreamViewer.
16class 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 Schuhcd258b82017-04-09 16:28:29 -070030
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 Perry5b1e8e32019-04-07 13:25:31 -070041
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 Schuh60e77942022-05-16 17:48:24 -070046 void SetExposure(int abs_exp) { set_exposure_(abs_exp); }
47
Alex Perry5b1e8e32019-04-07 13:25:31 -070048 private:
49 std::function<void(int)> set_exposure_;
Parker Schuh90641112017-02-25 12:18:36 -080050};
51
52// For ImageSource implementations only. Allows registering key press events
53// and installing new blob lists and jpegs.
54class 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 Perry5b1e8e32019-04-07 13:25:31 -070062 // 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 Schuhcd258b82017-04-09 16:28:29 -070068 // 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 Schuh90641112017-02-25 12:18:36 -080071
Parker Schuh02f13f62019-02-16 16:42:41 -080072 virtual bool NewImage(ImageFormat fmt,
73 const std::function<bool(ImagePtr data)> &process) = 0;
74
Parker Schuhcd258b82017-04-09 16:28:29 -070075 virtual bool NewBlobList(BlobList blob_list, ImageFormat fmt) = 0;
76
77 virtual bool JustCheckForTarget(BlobList imgs, ImageFormat fmt) = 0;
Parker Schuh90641112017-02-25 12:18:36 -080078
79 // Expose a EpollLoop to allow waiting for events.
80 virtual aos::events::EpollLoop *Loop() = 0;
81
Parker Schuhcd258b82017-04-09 16:28:29 -070082 virtual const CameraParams &camera_params() = 0;
83
84 virtual BlobStreamViewer *viewer() = 0;
85
Parker Schuh90641112017-02-25 12:18:36 -080086 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 Perry5b1e8e32019-04-07 13:25:31 -070093
94 std::function<void(int)> set_exposure_;
Parker Schuh90641112017-02-25 12:18:36 -080095};
96
97// Implemented by each source type. Will stream frames to
98// DebugFrameworkInterface.
99class 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.
112SETUP_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 Schuhcd258b82017-04-09 16:28:29 -0700119void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter,
120 CameraParams camera_params);
Parker Schuh90641112017-02-25 12:18:36 -0800121
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800122} // namespace aos::vision
Parker Schuh90641112017-02-25 12:18:36 -0800123
124#endif // _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_