Parker Schuh | 2ff1fc2 | 2019-02-22 21:33:53 -0800 | [diff] [blame] | 1 | #include <gdk/gdk.h> |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 2 | |
Parker Schuh | 2ff1fc2 | 2019-02-22 21:33:53 -0800 | [diff] [blame] | 3 | #include <fstream> |
| 4 | #include <string> |
| 5 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 6 | #include "aos/vision/debug/debug_framework.h" |
Parker Schuh | 2ff1fc2 | 2019-02-22 21:33:53 -0800 | [diff] [blame] | 7 | #include "aos/vision/image/camera_params.pb.h" |
| 8 | #include "aos/vision/image/image_stream.h" |
| 9 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 10 | namespace aos::vision { |
Parker Schuh | 2ff1fc2 | 2019-02-22 21:33:53 -0800 | [diff] [blame] | 11 | |
| 12 | class 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 Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 48 | interface_->InstallSetExposure( |
| 49 | [this](int abs_exp) { this->SetExposure(abs_exp); }); |
Parker Schuh | 2ff1fc2 | 2019-02-22 21:33:53 -0800 | [diff] [blame] | 50 | } |
| 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 | |
| 73 | REGISTER_IMAGE_SOURCE("jevois", AveugleImageSource); |
| 74 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 75 | } // namespace aos::vision |