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 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <netdb.h> |
| 8 | #include <poll.h> |
| 9 | #include <string.h> |
| 10 | #include <gtk/gtk.h> |
| 11 | #include <vector> |
| 12 | #include <memory> |
| 13 | #include <fstream> |
| 14 | |
| 15 | #include "aos/common/logging/logging.h" |
| 16 | #include "aos/common/logging/implementations.h" |
| 17 | #include "aos/vision/math/vector.h" |
| 18 | #include "aos/vision/image/reader.h" |
| 19 | #include "aos/vision/image/jpeg_routines.h" |
| 20 | #include "aos/vision/blob/threshold.h" |
| 21 | #include "aos/vision/blob/range_image.h" |
| 22 | #include "aos/vision/blob/stream_view.h" |
| 23 | #include "aos/vision/events/epoll_events.h" |
| 24 | #include "aos/vision/image/image_stream.h" |
| 25 | #include "aos/vision/events/tcp_server.h" |
| 26 | |
| 27 | namespace aos { |
| 28 | namespace vision { |
| 29 | |
| 30 | // Connects up a camera with our processing. |
| 31 | class ChannelImageStream : public ImageStreamEvent { |
| 32 | public: |
| 33 | ChannelImageStream(const std::string &fname, |
| 34 | const camera::CameraParams ¶ms) |
| 35 | : ImageStreamEvent(fname, params), view_(true) { |
| 36 | // Lambda to record image data to a file on key press. |
| 37 | view_.view()->key_press_event = [this](uint32_t /*keyval*/) { |
| 38 | std::ofstream ofs("/tmp/test.jpg", std::ofstream::out); |
| 39 | ofs << prev_data_; |
| 40 | ofs.close(); |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | // Handle an image from the camera. |
| 45 | void ProcessImage(DataRef data, |
| 46 | aos::monotonic_clock::time_point /*timestamp*/) { |
| 47 | ImageFormat fmt = GetFmt(data); |
| 48 | if (!fmt.Equals(view_.img().fmt())) view_.SetFormatAndClear(fmt); |
| 49 | if (!ProcessJpeg(data, view_.img().data())) return; |
| 50 | |
| 51 | ImagePtr img_ptr = view_.img(); |
| 52 | prev_data_ = data.to_string(); |
| 53 | |
| 54 | |
| 55 | // Threshold the image with the given lambda. |
| 56 | RangeImage rimg = DoThreshold(img_ptr, [](PixelRef &px) { |
| 57 | if (px.g > 88) { |
| 58 | uint8_t min = std::min(px.b, px.r); |
| 59 | uint8_t max = std::max(px.b, px.r); |
| 60 | if (min >= px.g || max >= px.g) return false; |
| 61 | uint8_t a = px.g - min; |
| 62 | uint8_t b = px.g - max; |
| 63 | return (a > 10 && b > 10); |
| 64 | } |
| 65 | return false; |
| 66 | }); |
| 67 | |
| 68 | view_.DrawBlobList({rimg}, {255, 255, 255}); |
| 69 | |
| 70 | view_.Redraw(); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | std::string prev_data_; |
| 75 | |
| 76 | // responsible for handling drawing |
| 77 | BlobStreamViewer view_; |
| 78 | }; |
| 79 | } // namespace aos |
| 80 | } // namespace vision |
| 81 | |
| 82 | int main(int argc, char *argv[]) { |
| 83 | ::aos::logging::Init(); |
| 84 | ::aos::logging::AddImplementation( |
| 85 | new ::aos::logging::StreamLogImplementation(stdout)); |
| 86 | aos::events::EpollLoop loop; |
| 87 | gtk_init(&argc, &argv); |
| 88 | |
| 89 | camera::CameraParams params = {.width = 640 * 2, |
| 90 | .height = 480 * 2, |
| 91 | .exposure = 10, |
| 92 | .brightness = 128, |
| 93 | .gain = 0, |
| 94 | .fps = 10}; |
| 95 | |
| 96 | aos::vision::ChannelImageStream strm1("/dev/video1", params); |
| 97 | |
| 98 | loop.Add(&strm1); |
| 99 | loop.RunWithGtkMain(); |
| 100 | } |