blob: ef2ec6da00e5c10fae0dc8f6ce1ae373bb16abec [file] [log] [blame]
Parker Schuhd7db83d2017-02-08 20:49:15 -08001// This file is to collect data from a camera to use for debug and testing. This
2// should not placed on a robot. This is okay as it is a utility of limited use
3// only.
4
Parker Schuh309dd722017-02-25 11:31:18 -08005#include <gtk/gtk.h>
Parker Schuhd7db83d2017-02-08 20:49:15 -08006#include <netdb.h>
7#include <poll.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
Parker Schuhd7db83d2017-02-08 20:49:15 -080012#include <fstream>
Parker Schuh309dd722017-02-25 11:31:18 -080013#include <memory>
14#include <vector>
Parker Schuhd7db83d2017-02-08 20:49:15 -080015
John Park33858a32018-09-28 23:05:48 -070016#include "aos/logging/implementations.h"
17#include "aos/logging/logging.h"
Parker Schuhd7db83d2017-02-08 20:49:15 -080018#include "aos/vision/blob/range_image.h"
19#include "aos/vision/blob/stream_view.h"
Parker Schuh309dd722017-02-25 11:31:18 -080020#include "aos/vision/blob/threshold.h"
Parker Schuhd7db83d2017-02-08 20:49:15 -080021#include "aos/vision/events/epoll_events.h"
Parker Schuhd7db83d2017-02-08 20:49:15 -080022#include "aos/vision/events/tcp_server.h"
Parker Schuh309dd722017-02-25 11:31:18 -080023#include "aos/vision/image/image_stream.h"
24#include "aos/vision/image/jpeg_routines.h"
25#include "aos/vision/image/reader.h"
26#include "aos/vision/math/vector.h"
Parker Schuhd7db83d2017-02-08 20:49:15 -080027
28namespace aos {
29namespace vision {
30
Parker Schuh309dd722017-02-25 11:31:18 -080031void DrawVLine(ImagePtr ptr, int x, PixelRef color = {255, 0, 0}) {
32 for (int y = 0; y < ptr.fmt().h; ++y) {
33 ptr.get_px(x, y) = color;
34 }
35}
36void DrawHLine(ImagePtr ptr, int y, PixelRef color = {255, 0, 0}) {
37 for (int x = 0; x < ptr.fmt().w; ++x) {
38 ptr.get_px(x, y) = color;
39 }
40}
41
Parker Schuhd7db83d2017-02-08 20:49:15 -080042// Connects up a camera with our processing.
43class ChannelImageStream : public ImageStreamEvent {
44 public:
45 ChannelImageStream(const std::string &fname,
Parker Schuh24ee58d2017-03-11 16:13:23 -080046 const aos::vision::CameraParams &params)
Parker Schuh309dd722017-02-25 11:31:18 -080047 : ImageStreamEvent(fname, params), view_(false) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080048 // Lambda to record image data to a file on key press.
Parker Schuh309dd722017-02-25 11:31:18 -080049 view_.view()->key_press_event = [this](uint32_t keyval) {
50 if (keyval == 'j') {
51 std::ofstream ofs("/tmp/test.jpg", std::ofstream::out);
52 ofs << prev_data_;
53 ofs.close();
54 } else if (keyval == 'a') {
55 ++dx;
56 } else if (keyval == 'd') {
57 --dx;
58 } else if (keyval == 'w') {
59 ++dy;
60 } else if (keyval == 's') {
61 --dy;
62 }
63 fprintf(stderr, "dx: %d dy: %d\n", dx, dy);
Parker Schuhd7db83d2017-02-08 20:49:15 -080064 };
65 }
66
67 // Handle an image from the camera.
68 void ProcessImage(DataRef data,
69 aos::monotonic_clock::time_point /*timestamp*/) {
70 ImageFormat fmt = GetFmt(data);
71 if (!fmt.Equals(view_.img().fmt())) view_.SetFormatAndClear(fmt);
72 if (!ProcessJpeg(data, view_.img().data())) return;
73
74 ImagePtr img_ptr = view_.img();
75 prev_data_ = data.to_string();
76
Parker Schuhd7db83d2017-02-08 20:49:15 -080077 // Threshold the image with the given lambda.
Brian Silverman69a68402019-03-24 11:47:54 -070078 RangeImage rimg = ThresholdImageWithFunction(img_ptr, [](PixelRef px) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080079 if (px.g > 88) {
80 uint8_t min = std::min(px.b, px.r);
81 uint8_t max = std::max(px.b, px.r);
82 if (min >= px.g || max >= px.g) return false;
83 uint8_t a = px.g - min;
84 uint8_t b = px.g - max;
85 return (a > 10 && b > 10);
86 }
87 return false;
88 });
89
Parker Schuh309dd722017-02-25 11:31:18 -080090 DrawVLine(img_ptr, fmt.w / 4);
91 DrawVLine(img_ptr, fmt.w / 2 + dy, {0, 255, 0});
92 DrawVLine(img_ptr, fmt.w - fmt.w / 4);
93
94 DrawHLine(img_ptr, fmt.h / 4);
95 DrawHLine(img_ptr, fmt.h / 2 + dx, {0, 255, 0});
96 DrawHLine(img_ptr, fmt.h - fmt.h / 4);
Parker Schuhd7db83d2017-02-08 20:49:15 -080097 view_.DrawBlobList({rimg}, {255, 255, 255});
98
99 view_.Redraw();
100 }
101
102 private:
103 std::string prev_data_;
104
105 // responsible for handling drawing
106 BlobStreamViewer view_;
Parker Schuh309dd722017-02-25 11:31:18 -0800107
108 int dx = 0;
109 int dy = 0;
Parker Schuhd7db83d2017-02-08 20:49:15 -0800110};
Parker Schuhd7db83d2017-02-08 20:49:15 -0800111} // namespace vision
Austin Schuha0c41ba2020-09-10 22:59:14 -0700112} // namespace aos
Parker Schuhd7db83d2017-02-08 20:49:15 -0800113
114int main(int argc, char *argv[]) {
115 ::aos::logging::Init();
Parker Schuhd7db83d2017-02-08 20:49:15 -0800116 aos::events::EpollLoop loop;
117 gtk_init(&argc, &argv);
118
Parker Schuh24ee58d2017-03-11 16:13:23 -0800119 aos::vision::CameraParams params;
Parker Schuhd7db83d2017-02-08 20:49:15 -0800120
121 aos::vision::ChannelImageStream strm1("/dev/video1", params);
122
123 loop.Add(&strm1);
124 loop.RunWithGtkMain();
125}