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