blob: ef48a114e54028b2e4d8c47e6deede33b177438c [file] [log] [blame]
Parker Schuh90641112017-02-25 12:18:36 -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/image_stream.h"
8
9namespace aos {
10namespace vision {
11
12class CameraImageSource : public ImageSource {
13 public:
14 void Init(const std::string &jpeg_list_filename,
15 DebugFrameworkInterface *interface) override {
16 // TODO: Get these params from a config file passed in through the
17 // constructor.
18 camera::CameraParams params = {.width = 640 * 2,
19 .height = 480 * 2,
20 .exposure = 10,
21 .brightness = 128,
22 .gain = 0,
23 .fps = 30};
24 image_stream_.reset(new ImageStream(jpeg_list_filename, params, interface));
25 }
26
27 const char *GetHelpMessage() override {
28 return &R"(
29 format_spec is filename of the camera device.
30 example: camera:/dev/video0
31 This viewer source will stream video from a usb camera of your choice.
32)"[1];
33 }
34
35 class ImageStream : public ImageStreamEvent {
36 public:
37 ImageStream(const std::string &fname, camera::CameraParams params,
38 DebugFrameworkInterface *interface)
39 : ImageStreamEvent(fname, params), interface_(interface) {
40 interface_->Loop()->Add(this);
41
42 interface_->InstallKeyPress([this](uint32_t keyval) {
43 // Takes a picture when you press 'a'.
44 // TODO(parker): Allow setting directory.
45 if (keyval == GDK_KEY_a) {
46 std::ofstream ofs(
47 std::string("/tmp/out_jpegs/test") + std::to_string(i_) + ".jpg",
48 std::ofstream::out);
49 ofs << prev_data_;
50 ofs.close();
51 ++i_;
52 }
53 });
54 }
55 void ProcessImage(DataRef data, aos::monotonic_clock::time_point) override {
56 prev_data_ = std::string(data);
57 interface_->NewJpeg(data);
58 }
59
60 private:
61 int i_ = 0;
62 std::string prev_data_;
63 DebugFrameworkInterface *interface_;
64 };
65
66 std::unique_ptr<ImageStream> image_stream_;
67};
68
69REGISTER_IMAGE_SOURCE("camera", CameraImageSource);
70
71} // namespace vision
72} // namespace aos