blob: c2913c28d568c5e36ddf7d934a594b138af64055 [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
10namespace aos {
11namespace vision {
12
13class BlobStreamViewer;
14
15// Implement per-filter to draw debug viewer information from the filter to
16// the debug BlobStreamViewer.
17class 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 Schuhcd258b82017-04-09 16:28:29 -070031
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 Perry5b1e8e32019-04-07 13:25:31 -070042
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 }
Austin Schuh60e77942022-05-16 17:48:24 -070047 void SetExposure(int abs_exp) { set_exposure_(abs_exp); }
48
Alex Perry5b1e8e32019-04-07 13:25:31 -070049 private:
50 std::function<void(int)> set_exposure_;
Parker Schuh90641112017-02-25 12:18:36 -080051};
52
53// For ImageSource implementations only. Allows registering key press events
54// and installing new blob lists and jpegs.
55class DebugFrameworkInterface {
56 public:
57 virtual ~DebugFrameworkInterface() {}
58
59 void InstallKeyPress(std::function<void(uint32_t)> key_press_event) {
60 key_press_events_.emplace_back(std::move(key_press_event));
61 }
62
Alex Perry5b1e8e32019-04-07 13:25:31 -070063 // The camera will tell us where to call to set exposure.
64 void InstallSetExposure(std::function<void(int)> set_exp) {
65 set_exposure_ = std::move(set_exp);
66 }
67 void SetExposure(int abs_exp) { set_exposure_(abs_exp); }
68
Parker Schuhcd258b82017-04-09 16:28:29 -070069 // The return value bool here for all of these is
70 // if the frame is "interesting" ie has a target.
71 virtual bool NewJpeg(DataRef data) = 0;
Parker Schuh90641112017-02-25 12:18:36 -080072
Parker Schuh02f13f62019-02-16 16:42:41 -080073 virtual bool NewImage(ImageFormat fmt,
74 const std::function<bool(ImagePtr data)> &process) = 0;
75
Parker Schuhcd258b82017-04-09 16:28:29 -070076 virtual bool NewBlobList(BlobList blob_list, ImageFormat fmt) = 0;
77
78 virtual bool JustCheckForTarget(BlobList imgs, ImageFormat fmt) = 0;
Parker Schuh90641112017-02-25 12:18:36 -080079
80 // Expose a EpollLoop to allow waiting for events.
81 virtual aos::events::EpollLoop *Loop() = 0;
82
Parker Schuhcd258b82017-04-09 16:28:29 -070083 virtual const CameraParams &camera_params() = 0;
84
85 virtual BlobStreamViewer *viewer() = 0;
86
Parker Schuh90641112017-02-25 12:18:36 -080087 protected:
88 const std::vector<std::function<void(uint32_t)>> &key_press_events() {
89 return key_press_events_;
90 }
91
92 private:
93 std::vector<std::function<void(uint32_t)>> key_press_events_;
Alex Perry5b1e8e32019-04-07 13:25:31 -070094
95 std::function<void(int)> set_exposure_;
Parker Schuh90641112017-02-25 12:18:36 -080096};
97
98// Implemented by each source type. Will stream frames to
99// DebugFrameworkInterface.
100class ImageSource {
101 public:
102 virtual ~ImageSource() {}
103
104 // Printed when you call: debug_viewer help.
105 virtual const char *GetHelpMessage() { return " No help string :(\n"; }
106
107 // Start streaming frames to DebugFrameworkInterface.
108 virtual void Init(const std::string &args,
109 DebugFrameworkInterface *interface) = 0;
110};
111
112// Factory for ImageSource.
113SETUP_FACTORY(ImageSource);
114
115#define REGISTER_IMAGE_SOURCE(key, SubClass) \
116 REGISTER_SUBCLASS_BY_KEY(key, ::aos::vision::ImageSource, SubClass)
117
118// Runs loop and never returns.
119// Feeds into a generic filter.
Parker Schuhcd258b82017-04-09 16:28:29 -0700120void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter,
121 CameraParams camera_params);
Parker Schuh90641112017-02-25 12:18:36 -0800122
123} // namespace vision
124} // namespace aos
125
126#endif // _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_