libwallaby  v23
The wallaby standard library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
camera.hpp
Go to the documentation of this file.
1 /*
2  * camera.hpp
3  *
4  * Created on: Jan 29, 2016
5  * Author: Nafis Zaman
6  */
7 
8 #ifndef _CAMERA_HPP_
9 #define _CAMERA_HPP_
10 
11 #include "geom.hpp"
12 #include "color.hpp"
13 #include "config.hpp"
14 #include <cstring>
15 #include <string>
16 #include <vector>
17 #include <map>
18 
19 #include "wallaby/camera.h"
20 
21 #include <opencv2/core/core.hpp>
22 
23 // These keys are used in the config files loaded by
24 // Camera::Device
25 #define CAMERA_GROUP ("camera")
26 #define CAMERA_NUM_CHANNELS_KEY ("num_channels")
27 #define CAMERA_CHANNEL_GROUP_PREFIX ("channel_")
28 #define CAMERA_CHANNEL_TYPE_KEY ("type")
29 
30 #define CAMERA_CHANNEL_TYPE_HSV_KEY ("hsv")
31 #define CAMERA_CHANNEL_TYPE_QR_KEY ("qr")
32 
33 namespace cv
34 {
35  class VideoCapture;
36 }
37 
38 namespace Camera
39 {
40  class Device;
41 
42  class Object
43  {
44  public:
47  const double confidence,
48  const char *data = 0,
49  const size_t &dataLength = 0);
50  Object(const Object &rhs);
51  ~Object();
52 
53  const Point2<unsigned> &centroid() const;
54  const Rect<unsigned> &boundingBox() const;
55 
56  const double confidence() const;
57  const char *data() const;
58  const size_t dataLength() const;
59 
60  private:
61  Point2<unsigned> m_centroid;
62  Rect<unsigned> m_boundingBox;
63  double m_confidence;
64  char *m_data;
65  size_t m_dataLength;
66  };
67 
68  typedef std::vector<Object> ObjectVector;
69 
71  {
72  public:
73  ChannelImpl();
74  virtual ~ChannelImpl();
75 
76  void setImage(const cv::Mat &image);
77  ObjectVector objects(const Config &config);
78 
79  protected:
80  virtual void update(const cv::Mat &image) = 0;
81  virtual ObjectVector findObjects(const Config &config) = 0;
82 
83  private:
84  bool m_dirty;
85  cv::Mat m_image;
86  };
87 
89  {
90  public:
91  static void setImage(const cv::Mat &image);
92  static ChannelImpl *channelImpl(const std::string &name);
93 
94  private:
95  // TODO: private constructor?
96  static std::map<std::string, ChannelImpl *> m_channelImpls;
97  };
98 
99  class Channel
100  {
101  public:
102  Channel(Device *device, const Config &config);
103  ~Channel();
104 
105  void invalidate();
106  const ObjectVector *objects() const;
107  Device *device() const;
108 
112  void setConfig(const Config &config);
113 
114  private:
115  Device *m_device;
116  Config m_config;
117  mutable ObjectVector m_objects;
118  ChannelImpl *m_impl;
119  mutable bool m_valid;
120  };
121 
122  typedef std::vector<Channel *> ChannelPtrVector;
123 
125  {
126  public:
127  static std::string extension();
128 
129  static void setBasePath(const std::string &path);
130  static std::string path(const std::string &name = std::string());
131  static std::string defaultPath();
132  static std::string defaultConfigPath();
133  static void setDefaultConfigPath(const std::string &name);
134 
135  private:
136  static std::string s_path;
137  };
138 
139  class Device
140  {
141  public:
142  Device();
143  ~Device();
144 
145  bool open(const int number = 0, Resolution resolution = LOW_RES, Model model = WHITE_2016);
146  bool isOpen() const;
147  bool close();
148  bool update();
149 
150  void setWidth(const unsigned width);
151  void setHeight(const unsigned height);
152 
153  unsigned width() const;
154  unsigned height() const;
155 
156  static unsigned int resolutionToHeight(Resolution res);
157  static unsigned int resolutionToWidth(Resolution res);
158 
159 
160  const ChannelPtrVector &channels() const;
161 
162  const cv::Mat &rawImage() const;
163 
164  void setConfig(const Config &config);
165  const Config &config() const;
166 
167  const unsigned char *bgr() const;
168 
169  private:
170  void updateConfig();
171  bool initCapDevice(const unsigned width, const unsigned height);
172  int readFrame();
173  cv::Mat decodeJpeg(void *p, int size);
174  int xioctl(int fh, int request, void *arg);
175 
176  struct buffer {
177  void *start;
178  size_t length;
179  };
180  struct buffer *buffers;
181  unsigned int nBuffers;
182 
183  //cv::VideoCapture *m_capture;
184  Config m_config;
185  ChannelPtrVector m_channels;
186  cv::Mat m_image;
187  unsigned char *m_bmpBuffer;
188 
189  mutable unsigned char *m_bgr;
190  mutable unsigned m_bgrSize;
191 
192  int m_fd;
193  static const char *device_name;
194  cv::VideoCapture *m_cap;
195  bool m_connected;
196 
197  Resolution m_resolution;
198  Model m_model;
199  };
200 
205 }
206 
207 
208 
209 #endif
Camera::Device * cDevice()
void setHeight(const unsigned height)
void setConfig(const Config &config)
virtual ObjectVector findObjects(const Config &config)=0
Definition: camera.hpp:139
ObjectVector objects(const Config &config)
const char * data() const
static void setDefaultConfigPath(const std::string &name)
static std::string extension()
unsigned width() const
const size_t dataLength() const
unsigned height() const
Definition: camera.hpp:33
const ChannelPtrVector & channels() const
std::vector< Object > ObjectVector
Definition: camera.hpp:68
Definition: camera.h:41
static ChannelImpl * channelImpl(const std::string &name)
void setWidth(const unsigned width)
void setImage(const cv::Mat &image)
Definition: config.hpp:16
const cv::Mat & rawImage() const
Definition: camera.hpp:38
static void setBasePath(const std::string &path)
Definition: camera.hpp:124
virtual void update(const cv::Mat &image)=0
static std::string path(const std::string &name=std::string())
Definition: camera.h:33
std::vector< Channel * > ChannelPtrVector
Definition: camera.hpp:122
void setConfig(const Config &config)
Model
Definition: camera.h:39
Resolution
Definition: camera.h:31
Definition: camera.hpp:99
static std::string defaultPath()
const Config & config() const
const ObjectVector * objects() const
static void setImage(const cv::Mat &image)
static std::string defaultConfigPath()
virtual ~ChannelImpl()
const double confidence() const
static unsigned int resolutionToHeight(Resolution res)
Definition: camera.hpp:70
bool isOpen() const
Device * device() const
Definition: camera.hpp:88
Definition: camera.hpp:42
const unsigned char * bgr() const
const Point2< unsigned > & centroid() const
static unsigned int resolutionToWidth(Resolution res)
const Rect< unsigned > & boundingBox() const
Object(const Point2< unsigned > &centroid, const Rect< unsigned > &boundingBox, const double confidence, const char *data=0, const size_t &dataLength=0)
bool open(const int number=0, Resolution resolution=LOW_RES, Model model=WHITE_2016)
Channel(Device *device, const Config &config)