blob: d8ed4a19ca9ae85cfef17bf5e1e833b0277ea4b6 [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 }
47 void SetExposure(int abs_exp) {
48 set_exposure_(abs_exp);
49 }
50 private:
51 std::function<void(int)> set_exposure_;
Parker Schuh90641112017-02-25 12:18:36 -080052};
53
54// For ImageSource implementations only. Allows registering key press events
55// and installing new blob lists and jpegs.
56class 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 Perry5b1e8e32019-04-07 13:25:31 -070064 // 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 Schuhcd258b82017-04-09 16:28:29 -070070 // 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 Schuh90641112017-02-25 12:18:36 -080073
Parker Schuh02f13f62019-02-16 16:42:41 -080074 virtual bool NewImage(ImageFormat fmt,
75 const std::function<bool(ImagePtr data)> &process) = 0;
76
Parker Schuhcd258b82017-04-09 16:28:29 -070077 virtual bool NewBlobList(BlobList blob_list, ImageFormat fmt) = 0;
78
79 virtual bool JustCheckForTarget(BlobList imgs, ImageFormat fmt) = 0;
Parker Schuh90641112017-02-25 12:18:36 -080080
81 // Expose a EpollLoop to allow waiting for events.
82 virtual aos::events::EpollLoop *Loop() = 0;
83
Parker Schuhcd258b82017-04-09 16:28:29 -070084 virtual const CameraParams &camera_params() = 0;
85
86 virtual BlobStreamViewer *viewer() = 0;
87
Parker Schuh90641112017-02-25 12:18:36 -080088 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 Perry5b1e8e32019-04-07 13:25:31 -070095
96 std::function<void(int)> set_exposure_;
Parker Schuh90641112017-02-25 12:18:36 -080097};
98
99// Implemented by each source type. Will stream frames to
100// DebugFrameworkInterface.
101class 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.
114SETUP_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 Schuhcd258b82017-04-09 16:28:29 -0700121void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter,
122 CameraParams camera_params);
Parker Schuh90641112017-02-25 12:18:36 -0800123
124} // namespace vision
125} // namespace aos
126
127#endif // _AOS_VISION_DEBUG_DEBUG_FRAMEWORK_H_