blob: 685b7eb2b47b83636e3c9ae8ac63493ab398f35a [file] [log] [blame]
Parker Schuh90641112017-02-25 12:18:36 -08001#include <gdk/gdk.h>
Austin Schuh60e77942022-05-16 17:48:24 -07002
Parker Schuh90641112017-02-25 12:18:36 -08003#include <fstream>
4#include <string>
5
Austin Schuh60e77942022-05-16 17:48:24 -07006#include "aos/vision/debug/debug_framework.h"
Parker Schuh24ee58d2017-03-11 16:13:23 -08007#include "aos/vision/image/camera_params.pb.h"
Parker Schuh90641112017-02-25 12:18:36 -08008#include "aos/vision/image/image_stream.h"
9
Stephan Pleinesf63bde82024-01-13 15:59:33 -080010namespace aos::vision {
Parker Schuh90641112017-02-25 12:18:36 -080011
12class CameraImageSource : public ImageSource {
13 public:
14 void Init(const std::string &jpeg_list_filename,
15 DebugFrameworkInterface *interface) override {
Parker Schuhcd258b82017-04-09 16:28:29 -070016 // TODO: These camera params make this ugly and less generic.
17 image_stream_.reset(new ImageStream(jpeg_list_filename,
18 interface->camera_params(), interface));
Parker Schuh90641112017-02-25 12:18:36 -080019 }
20
21 const char *GetHelpMessage() override {
22 return &R"(
23 format_spec is filename of the camera device.
24 example: camera:/dev/video0
Parker Schuhcd258b82017-04-09 16:28:29 -070025 This viewer source will stream video from a usb camera of your choice.
26)"[1];
Parker Schuh90641112017-02-25 12:18:36 -080027 }
28
29 class ImageStream : public ImageStreamEvent {
30 public:
Parker Schuh24ee58d2017-03-11 16:13:23 -080031 ImageStream(const std::string &fname, aos::vision::CameraParams params,
Parker Schuh90641112017-02-25 12:18:36 -080032 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) {
Parker Schuhcd258b82017-04-09 16:28:29 -070040 std::ofstream ofs(std::string("/tmp/debug_viewer_jpeg_") +
41 std::to_string(i_) + ".jpg",
42 std::ofstream::out);
Parker Schuh90641112017-02-25 12:18:36 -080043 ofs << prev_data_;
44 ofs.close();
45 ++i_;
46 }
47 });
48 }
49 void ProcessImage(DataRef data, aos::monotonic_clock::time_point) override {
50 prev_data_ = std::string(data);
51 interface_->NewJpeg(data);
52 }
53
54 private:
55 int i_ = 0;
56 std::string prev_data_;
57 DebugFrameworkInterface *interface_;
58 };
59
60 std::unique_ptr<ImageStream> image_stream_;
61};
62
63REGISTER_IMAGE_SOURCE("camera", CameraImageSource);
64
Stephan Pleinesf63bde82024-01-13 15:59:33 -080065} // namespace aos::vision