blob: 1d94217336329d322e119feac91deba1c838621d [file] [log] [blame]
Parker Schuh90641112017-02-25 12:18:36 -08001#include "aos/vision/debug/debug_framework.h"
2
3#include <gtk/gtk.h>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/implementations.h"
6#include "aos/logging/logging.h"
Parker Schuh90641112017-02-25 12:18:36 -08007#include "aos/vision/blob/find_blob.h"
8#include "aos/vision/blob/stream_view.h"
Parker Schuh90641112017-02-25 12:18:36 -08009#include "aos/vision/events/epoll_events.h"
10#include "aos/vision/image/jpeg_routines.h"
11
12namespace aos {
13namespace vision {
14
Parker Schuhcd258b82017-04-09 16:28:29 -070015// Detect screen height on smaller monitors.
16int GetScreenHeight() {
17 fprintf(stderr, "gtk version_info: %d.%d.%d\n", gtk_get_major_version(),
18 gtk_get_minor_version(), gtk_get_micro_version());
19
20 GdkScreen *screen = gdk_screen_get_default();
21 GdkRectangle dimensions;
22// Deprecated in newer versions of GTK and missing from older versions.
23#if GTK_CHECK_VERSION(3, 22, 7)
24 GdkDisplay *display = gdk_screen_get_display(screen);
25 GdkMonitor *monitor = gdk_display_get_primary_monitor(display);
26 gdk_monitor_get_geometry(monitor, &dimensions);
27#else
28 dimensions.height = gdk_screen_get_height(screen);
29 dimensions.width = gdk_screen_get_width(screen);
30#endif
31 fprintf(stdout, "Monitor dimensions: %dx%d\n", dimensions.width,
32 dimensions.height);
33 return dimensions.height;
34}
35
Parker Schuh90641112017-02-25 12:18:36 -080036bool DecodeJpeg(aos::vision::DataRef data,
37 aos::vision::BlobStreamViewer *view) {
38 auto fmt = aos::vision::GetFmt(data);
39 auto value = view->img();
40 if (!value.fmt().Equals(fmt)) {
41 view->SetFormatAndClear(fmt);
42 }
43 return aos::vision::ProcessJpeg(data, view->img().data());
44}
45
46class DebugFramework : public DebugFrameworkInterface {
47 public:
Parker Schuhcd258b82017-04-09 16:28:29 -070048 explicit DebugFramework(FilterHarness *filter, CameraParams camera_params)
49 : camera_params_(camera_params), filter_(filter) {
Parker Schuh90641112017-02-25 12:18:36 -080050 view_.key_press_event = [this](uint32_t keyval) {
51 for (const auto &event : key_press_events()) {
52 event(keyval);
53 }
54 };
55 filter->InstallViewer(&view_);
Parker Schuhcd258b82017-04-09 16:28:29 -070056 auto key_press = filter->RegisterKeyPress();
57 if (key_press) {
58 InstallKeyPress(key_press);
59 }
60 if (GetScreenHeight() < 1024) {
Parker Schuh02f13f62019-02-16 16:42:41 -080061 view_.SetScale(1.0);
Parker Schuhcd258b82017-04-09 16:28:29 -070062 }
Parker Schuh90641112017-02-25 12:18:36 -080063 }
64
65 // This the first stage in the pipeline that takes
Parker Schuhcd258b82017-04-09 16:28:29 -070066 bool NewJpeg(DataRef data) override {
Parker Schuh90641112017-02-25 12:18:36 -080067 DecodeJpeg(data, &view_);
68
69 auto fmt = view_.img().fmt();
Parker Schuhcd258b82017-04-09 16:28:29 -070070 return HandleBlobs(FindBlobs(filter_->Threshold(view_.img())), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080071 }
72
Parker Schuh02f13f62019-02-16 16:42:41 -080073 bool NewImage(ImageFormat fmt,
74 const std::function<bool(ImagePtr data)> &process) override {
75 auto value = view_.img();
76 if (!value.fmt().Equals(fmt)) {
77 view_.SetFormatAndClear(fmt);
78 }
79 process(view_.img());
80
81 return HandleBlobs(FindBlobs(filter_->Threshold(view_.img())), fmt);
82 }
83
Parker Schuhcd258b82017-04-09 16:28:29 -070084 bool NewBlobList(BlobList blob_list, ImageFormat fmt) override {
Parker Schuh90641112017-02-25 12:18:36 -080085 view_.SetFormatAndClear(fmt);
86
Parker Schuhcd258b82017-04-09 16:28:29 -070087 return HandleBlobs(std::move(blob_list), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080088 }
89
Parker Schuhcd258b82017-04-09 16:28:29 -070090 bool JustCheckForTarget(BlobList blob_list, ImageFormat fmt) override {
91 return filter_->JustCheckForTarget(std::move(blob_list), fmt);
92 }
93
94 bool HandleBlobs(BlobList blob_list, ImageFormat fmt) {
95 bool result = filter_->HandleBlobs(std::move(blob_list), fmt);
Parker Schuh90641112017-02-25 12:18:36 -080096 view_.Redraw();
Parker Schuhcd258b82017-04-09 16:28:29 -070097 return result;
Parker Schuh90641112017-02-25 12:18:36 -080098 }
99
100 aos::events::EpollLoop *Loop() override { return &loop_; }
101
Parker Schuhcd258b82017-04-09 16:28:29 -0700102 const CameraParams &camera_params() override { return camera_params_; }
103
104 BlobStreamViewer *viewer() override { return &view_; }
105
Parker Schuh90641112017-02-25 12:18:36 -0800106 private:
Parker Schuhcd258b82017-04-09 16:28:29 -0700107 CameraParams camera_params_;
Parker Schuh90641112017-02-25 12:18:36 -0800108 FilterHarness *filter_;
109 BlobStreamViewer view_;
110
111 aos::events::EpollLoop loop_;
112};
113
114std::unique_ptr<ImageSource> MakeImageSource(
115 const std::string &image_source_string,
116 DebugFrameworkInterface *interface) {
117 (void)interface;
118 // Each of the image_source strings is of the form format_type:format_spec
119 auto it = image_source_string.find(':');
120 if (it == std::string::npos) {
121 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
122 exit(-1);
123 }
124 auto image_source_type = image_source_string.substr(0, it);
125 // Get std::function<std::unique_ptr<ImageSource>()> from the registration
126 // factory.
127 const auto &factory = ImageSourceGlobalFactory::Get(image_source_type);
128 if (!factory) {
129 fprintf(stderr, "invalid ImageSource: %s.\n", image_source_string.c_str());
130 exit(-1);
131 }
132 auto result = factory();
133 // Construct the image source.
134 result->Init(image_source_string.substr(it + 1), interface);
135 return result;
136}
137
138const char *kHelpMessage = R"(
139
140image_source is parsed out and selects where to get the images
141from. Each source type has a different configuration format string listed
142below. The colon separates the source specifier and the source config
143parameter. A single command line argument help will print this message.
144)";
145
Parker Schuhcd258b82017-04-09 16:28:29 -0700146void DebugFrameworkMain(int argc, char **argv, FilterHarness *filter,
147 CameraParams camera_params) {
Parker Schuh90641112017-02-25 12:18:36 -0800148 ::aos::logging::Init();
149 ::aos::logging::AddImplementation(
150 new ::aos::logging::StreamLogImplementation(stdout));
151
152 gtk_init(&argc, &argv);
153
154 // Use fprintf because it is only supposed to be used interactively.
155 // This uses a registration system to pick out the individual file type
156 // registered by REGISTER_IMAGE_SOURCE.
157 // see jpeg_list-source.cc for ane sample of this.
158 if (argc < 2 || argv[1] == std::string("help")) {
159 fprintf(stderr, "Usage %s image_source:format_spec\n", argv[0]);
160 fprintf(stderr, "%s", kHelpMessage);
161 // Iterate through all registered entities in ImageSourceGlobalFactory
162 // and print out their individual help messages.
163 for (const auto &type : ImageSourceGlobalFactory::GetAll()) {
164 fprintf(stderr, " %s:\n", type.first.c_str());
165 fprintf(stderr, "%s", type.second()->GetHelpMessage());
166 }
167 exit(-1);
168 }
169
Parker Schuhcd258b82017-04-09 16:28:29 -0700170 DebugFramework replay(filter, camera_params);
Parker Schuh90641112017-02-25 12:18:36 -0800171
172 std::unique_ptr<ImageSource> image_source = MakeImageSource(argv[1], &replay);
173
174 replay.Loop()->RunWithGtkMain();
175}
176
177} // namespace vision
178} // namespace aos