blob: 059166e83b41c83de310a463d5d79ebd42b04a10 [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>
Parker Schuh309dd722017-02-25 11:31:18 -08008#include <stdio.h>
9#include <stdlib.h>
Parker Schuhd7db83d2017-02-08 20:49:15 -080010#include <string.h>
Parker Schuhd7db83d2017-02-08 20:49:15 -080011#include <fstream>
Parker Schuh309dd722017-02-25 11:31:18 -080012#include <memory>
13#include <vector>
Parker Schuhd7db83d2017-02-08 20:49:15 -080014
Parker Schuhd7db83d2017-02-08 20:49:15 -080015#include "aos/common/logging/implementations.h"
Parker Schuh309dd722017-02-25 11:31:18 -080016#include "aos/common/logging/logging.h"
Parker Schuhd7db83d2017-02-08 20:49:15 -080017#include "aos/vision/blob/range_image.h"
18#include "aos/vision/blob/stream_view.h"
Parker Schuh309dd722017-02-25 11:31:18 -080019#include "aos/vision/blob/threshold.h"
Parker Schuhd7db83d2017-02-08 20:49:15 -080020#include "aos/vision/events/epoll_events.h"
Parker Schuhd7db83d2017-02-08 20:49:15 -080021#include "aos/vision/events/tcp_server.h"
Parker Schuh309dd722017-02-25 11:31:18 -080022#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 Schuhd7db83d2017-02-08 20:49:15 -080026
27namespace aos {
28namespace vision {
29
Parker Schuh309dd722017-02-25 11:31:18 -080030void 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}
35void 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 Schuhd7db83d2017-02-08 20:49:15 -080041// Connects up a camera with our processing.
42class ChannelImageStream : public ImageStreamEvent {
43 public:
44 ChannelImageStream(const std::string &fname,
Parker Schuh24ee58d2017-03-11 16:13:23 -080045 const aos::vision::CameraParams &params)
Parker Schuh309dd722017-02-25 11:31:18 -080046 : ImageStreamEvent(fname, params), view_(false) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080047 // Lambda to record image data to a file on key press.
Parker Schuh309dd722017-02-25 11:31:18 -080048 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 Schuhd7db83d2017-02-08 20:49:15 -080063 };
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 Schuhd7db83d2017-02-08 20:49:15 -080076 // 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 Schuh309dd722017-02-25 11:31:18 -080089 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 Schuhd7db83d2017-02-08 20:49:15 -080096 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 Schuh309dd722017-02-25 11:31:18 -0800106
107 int dx = 0;
108 int dy = 0;
Parker Schuhd7db83d2017-02-08 20:49:15 -0800109};
110} // namespace aos
111} // namespace vision
112
113int 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 Schuh24ee58d2017-03-11 16:13:23 -0800120 aos::vision::CameraParams params;
Parker Schuhd7db83d2017-02-08 20:49:15 -0800121
122 aos::vision::ChannelImageStream strm1("/dev/video1", params);
123
124 loop.Add(&strm1);
125 loop.RunWithGtkMain();
126}