Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 1 | // 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 Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 5 | #include <gtk/gtk.h> |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 6 | #include <netdb.h> |
| 7 | #include <poll.h> |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 10 | #include <string.h> |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 11 | #include <fstream> |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 12 | #include <memory> |
| 13 | #include <vector> |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 14 | |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 15 | #include "aos/common/logging/implementations.h" |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 16 | #include "aos/common/logging/logging.h" |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 17 | #include "aos/vision/blob/range_image.h" |
| 18 | #include "aos/vision/blob/stream_view.h" |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 19 | #include "aos/vision/blob/threshold.h" |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 20 | #include "aos/vision/events/epoll_events.h" |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 21 | #include "aos/vision/events/tcp_server.h" |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 22 | #include "aos/vision/image/image_stream.h" |
| 23 | #include "aos/vision/image/jpeg_routines.h" |
| 24 | #include "aos/vision/image/reader.h" |
| 25 | #include "aos/vision/math/vector.h" |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 26 | |
| 27 | namespace aos { |
| 28 | namespace vision { |
| 29 | |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 30 | void DrawVLine(ImagePtr ptr, int x, PixelRef color = {255, 0, 0}) { |
| 31 | for (int y = 0; y < ptr.fmt().h; ++y) { |
| 32 | ptr.get_px(x, y) = color; |
| 33 | } |
| 34 | } |
| 35 | void DrawHLine(ImagePtr ptr, int y, PixelRef color = {255, 0, 0}) { |
| 36 | for (int x = 0; x < ptr.fmt().w; ++x) { |
| 37 | ptr.get_px(x, y) = color; |
| 38 | } |
| 39 | } |
| 40 | |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 41 | // Connects up a camera with our processing. |
| 42 | class ChannelImageStream : public ImageStreamEvent { |
| 43 | public: |
| 44 | ChannelImageStream(const std::string &fname, |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 45 | const aos::vision::CameraParams ¶ms) |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 46 | : ImageStreamEvent(fname, params), view_(false) { |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 47 | // Lambda to record image data to a file on key press. |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 48 | view_.view()->key_press_event = [this](uint32_t keyval) { |
| 49 | if (keyval == 'j') { |
| 50 | std::ofstream ofs("/tmp/test.jpg", std::ofstream::out); |
| 51 | ofs << prev_data_; |
| 52 | ofs.close(); |
| 53 | } else if (keyval == 'a') { |
| 54 | ++dx; |
| 55 | } else if (keyval == 'd') { |
| 56 | --dx; |
| 57 | } else if (keyval == 'w') { |
| 58 | ++dy; |
| 59 | } else if (keyval == 's') { |
| 60 | --dy; |
| 61 | } |
| 62 | fprintf(stderr, "dx: %d dy: %d\n", dx, dy); |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 63 | }; |
| 64 | } |
| 65 | |
| 66 | // Handle an image from the camera. |
| 67 | void ProcessImage(DataRef data, |
| 68 | aos::monotonic_clock::time_point /*timestamp*/) { |
| 69 | ImageFormat fmt = GetFmt(data); |
| 70 | if (!fmt.Equals(view_.img().fmt())) view_.SetFormatAndClear(fmt); |
| 71 | if (!ProcessJpeg(data, view_.img().data())) return; |
| 72 | |
| 73 | ImagePtr img_ptr = view_.img(); |
| 74 | prev_data_ = data.to_string(); |
| 75 | |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 76 | // Threshold the image with the given lambda. |
| 77 | RangeImage rimg = DoThreshold(img_ptr, [](PixelRef &px) { |
| 78 | if (px.g > 88) { |
| 79 | uint8_t min = std::min(px.b, px.r); |
| 80 | uint8_t max = std::max(px.b, px.r); |
| 81 | if (min >= px.g || max >= px.g) return false; |
| 82 | uint8_t a = px.g - min; |
| 83 | uint8_t b = px.g - max; |
| 84 | return (a > 10 && b > 10); |
| 85 | } |
| 86 | return false; |
| 87 | }); |
| 88 | |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 89 | DrawVLine(img_ptr, fmt.w / 4); |
| 90 | DrawVLine(img_ptr, fmt.w / 2 + dy, {0, 255, 0}); |
| 91 | DrawVLine(img_ptr, fmt.w - fmt.w / 4); |
| 92 | |
| 93 | DrawHLine(img_ptr, fmt.h / 4); |
| 94 | DrawHLine(img_ptr, fmt.h / 2 + dx, {0, 255, 0}); |
| 95 | DrawHLine(img_ptr, fmt.h - fmt.h / 4); |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 96 | view_.DrawBlobList({rimg}, {255, 255, 255}); |
| 97 | |
| 98 | view_.Redraw(); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | std::string prev_data_; |
| 103 | |
| 104 | // responsible for handling drawing |
| 105 | BlobStreamViewer view_; |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 106 | |
| 107 | int dx = 0; |
| 108 | int dy = 0; |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 109 | }; |
| 110 | } // namespace aos |
| 111 | } // namespace vision |
| 112 | |
| 113 | int main(int argc, char *argv[]) { |
| 114 | ::aos::logging::Init(); |
| 115 | ::aos::logging::AddImplementation( |
| 116 | new ::aos::logging::StreamLogImplementation(stdout)); |
| 117 | aos::events::EpollLoop loop; |
| 118 | gtk_init(&argc, &argv); |
| 119 | |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame] | 120 | aos::vision::CameraParams params; |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 121 | |
| 122 | aos::vision::ChannelImageStream strm1("/dev/video1", params); |
| 123 | |
| 124 | loop.Add(&strm1); |
| 125 | loop.RunWithGtkMain(); |
| 126 | } |