blob: cbe3d0a6fece3544b9a5382cdbc6b293b552376f [file] [log] [blame]
Parker Schuh2ff1fc22019-02-22 21:33:53 -08001#include "aos/vision/debug/debug_framework.h"
2
3#include <gdk/gdk.h>
4#include <fstream>
5#include <string>
6
7#include "aos/vision/image/camera_params.pb.h"
8#include "aos/vision/image/image_stream.h"
9
10namespace aos {
11namespace vision {
12
13class AveugleImageSource : public ImageSource {
14 public:
15 void Init(const std::string &jpeg_list_filename,
16 DebugFrameworkInterface *interface) override {
17 // TODO: These camera params make this ugly and less generic.
18 image_stream_.reset(new ImageStream(jpeg_list_filename,
19 interface->camera_params(), interface));
20 }
21
22 const char *GetHelpMessage() override {
23 return &R"(
24 format_spec is filename of the camera device.
25 example: jevois:/dev/video1
26 This viewer source will stream video from a jevois camera of your choice.
27)"[1];
28 }
29
30 class ImageStream : public ImageStreamEvent {
31 public:
32 ImageStream(const std::string &fname, aos::vision::CameraParams params,
33 DebugFrameworkInterface *interface)
34 : ImageStreamEvent(fname, params), interface_(interface) {
35 interface_->Loop()->Add(this);
36
37 interface_->InstallKeyPress([this](uint32_t keyval) {
38 // Takes a picture when you press 'a'.
39 // TODO(parker): Allow setting directory.
40 if (keyval == GDK_KEY_a) {
41 std::ofstream ofs(std::string("/tmp/debug_viewer_jpeg_") +
42 std::to_string(i_) + ".yuyv",
43 std::ofstream::out);
44 ofs << prev_data_;
45 ofs.close();
46 ++i_;
47 }
48 });
49 }
50 void ProcessImage(DataRef data, aos::monotonic_clock::time_point) override {
51 prev_data_ = std::string(data);
52 interface_->NewImage({640, 480}, [&](ImagePtr img_data) {
53 for (int y = 0; y < 480; ++y) {
54 for (int x = 0; x < 640; ++x) {
55 uint8_t v = data[y * 640 * 2 + x * 2 + 0];
56 img_data.get_px(x, y) = PixelRef{v, v, v};
57 }
58 }
59 return false;
60 });
61 }
62
63 private:
64 int i_ = 0;
65 std::string prev_data_;
66 DebugFrameworkInterface *interface_;
67 };
68
69 std::unique_ptr<ImageStream> image_stream_;
70};
71
72REGISTER_IMAGE_SOURCE("jevois", AveugleImageSource);
73
74} // namespace vision
75} // namespace aos