libwallaby  v24
The wallaby standard library
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 "color.hpp"
12 #include "config.hpp"
13 #include "geom.hpp"
14 #include <cstring>
15 #include <map>
16 #include <string>
17 #include <vector>
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 #define CAMERA_CHANNEL_TYPE_ARUCO_KEY ("aruco")
33 
34 namespace cv {
35 class VideoCapture;
36 }
37 
38 namespace Camera {
39 class Device;
40 
41 class Object {
42 public:
44  const double confidence, const char *data = 0,
45  const size_t &dataLength = 0);
46  Object(const Object &rhs);
47  ~Object();
48 
49  const Point2<unsigned> &centroid() const;
50  const Rect<unsigned> &boundingBox() const;
51 
52  const double confidence() const;
53  const char *data() const;
54  const size_t dataLength() const;
55 
56 private:
57  Point2<unsigned> m_centroid;
58  Rect<unsigned> m_boundingBox;
59  double m_confidence;
60  char *m_data;
61  size_t m_dataLength;
62 };
63 
64 typedef std::vector<Object> ObjectVector;
65 
66 class ChannelImpl {
67 public:
68  ChannelImpl();
69  virtual ~ChannelImpl();
70 
71  void setImage(const cv::Mat &image);
72  ObjectVector objects(const Config &config);
73 
74 protected:
75  virtual void update(const cv::Mat &image) = 0;
76  virtual ObjectVector findObjects(const Config &config) = 0;
77 
78 private:
79  bool m_dirty;
80  cv::Mat m_image;
81 };
82 
84 public:
85  static void setImage(const cv::Mat &image);
86  static ChannelImpl *channelImpl(const std::string &name);
87 
88 private:
89  // TODO: private constructor?
90  static std::map<std::string, ChannelImpl *> m_channelImpls;
91 };
92 
93 class Channel {
94 public:
95  Channel(Device *device, const Config &config);
96  ~Channel();
97 
98  void invalidate();
99  const ObjectVector *objects() const;
100  Device *device() const;
101 
105  void setConfig(const Config &config);
106 
107 private:
108  Device *m_device;
109  Config m_config;
110  mutable ObjectVector m_objects;
111  ChannelImpl *m_impl;
112  mutable bool m_valid;
113 };
114 
115 typedef std::vector<Channel *> ChannelPtrVector;
116 
117 class ConfigPath {
118 public:
119  static std::string extension();
120 
121  static void setBasePath(const std::string &path);
122  static std::string path(const std::string &name = std::string());
123  static std::string defaultPath();
124  static std::string defaultConfigPath();
125  static void setDefaultConfigPath(const std::string &name);
126 
127 private:
128  static std::string s_path;
129 };
130 
131 class Device {
132 public:
133  Device();
134  ~Device();
135 
136  bool open(const int number = 0, Resolution resolution = LOW_RES,
137  Model model = WHITE_2016);
138  bool isOpen() const;
139  bool close();
140  bool update();
141 
142  void setWidth(const unsigned width);
143  void setHeight(const unsigned height);
144 
145  unsigned width() const;
146  unsigned height() const;
147 
148  static unsigned int resolutionToHeight(Resolution res);
149  static unsigned int resolutionToWidth(Resolution res);
150 
151  const ChannelPtrVector &channels() const;
152 
153  const cv::Mat &rawImage() const;
154 
155  void setConfig(const Config &config);
156  const Config &config() const;
157 
158  const unsigned char *bgr() const;
159 
160  Model getModel();
161 
162 private:
163  void updateConfig();
164  bool initCapDevice(const unsigned width, const unsigned height);
165  int readFrame();
166  cv::Mat decodeJpeg(void *p, int size);
167  int xioctl(int fh, int request, void *arg);
168 
169  struct buffer {
170  void *start;
171  size_t length;
172  };
173  struct buffer *buffers;
174  unsigned int nBuffers;
175 
176  // cv::VideoCapture *m_capture;
177  Config m_config;
178  ChannelPtrVector m_channels;
179  cv::Mat m_image;
180  unsigned char *m_bmpBuffer;
181 
182  mutable unsigned char *m_bgr;
183  mutable unsigned m_bgrSize;
184 
185  int m_fd;
186  static const char *device_name;
187  cv::VideoCapture *m_cap;
188  bool m_connected;
189 
190  Resolution m_resolution;
191  Model m_model;
192 };
193 
198 }
199 
200 #endif
Camera::Device::~Device
~Device()
Camera::Object::boundingBox
const Rect< unsigned > & boundingBox() const
Camera::Device::getModel
Model getModel()
geom.hpp
Camera::Device::close
bool close()
Camera::Channel::device
Device * device() const
config.hpp
Camera::Channel::setConfig
void setConfig(const Config &config)
Camera::ConfigPath::extension
static std::string extension()
Camera::Device::update
bool update()
Camera::ChannelImpl::findObjects
virtual ObjectVector findObjects(const Config &config)=0
Camera::Device::resolutionToHeight
static unsigned int resolutionToHeight(Resolution res)
Camera::ChannelImpl::update
virtual void update(const cv::Mat &image)=0
Camera::Object::Object
Object(const Point2< unsigned > &centroid, const Rect< unsigned > &boundingBox, const double confidence, const char *data=0, const size_t &dataLength=0)
Camera::Device::rawImage
const cv::Mat & rawImage() const
Camera::Object::data
const char * data() const
Camera::Object::confidence
const double confidence() const
Camera::Device::width
unsigned width() const
Camera::ObjectVector
std::vector< Object > ObjectVector
Definition: camera.hpp:64
Camera::Channel::Channel
Channel(Device *device, const Config &config)
Camera::ConfigPath::setDefaultConfigPath
static void setDefaultConfigPath(const std::string &name)
Camera::Object::~Object
~Object()
Model
Model
Definition: camera.h:39
Camera::Device::isOpen
bool isOpen() const
Camera::Channel
Definition: camera.hpp:93
Config
Definition: config.hpp:16
Camera::Device::config
const Config & config() const
Camera::Device::Device
Device()
Camera::Device::channels
const ChannelPtrVector & channels() const
Camera::Device::open
bool open(const int number=0, Resolution resolution=LOW_RES, Model model=WHITE_2016)
Camera::ConfigPath::path
static std::string path(const std::string &name=std::string())
Camera::Channel::~Channel
~Channel()
Camera::Device::height
unsigned height() const
Camera::ConfigPath::defaultPath
static std::string defaultPath()
Camera::ConfigPath::defaultConfigPath
static std::string defaultConfigPath()
Camera::Device::setHeight
void setHeight(const unsigned height)
Camera::ChannelImpl::ChannelImpl
ChannelImpl()
Camera::ConfigPath::setBasePath
static void setBasePath(const std::string &path)
Camera::ChannelImpl::setImage
void setImage(const cv::Mat &image)
LOW_RES
Definition: camera.h:33
Point2< unsigned >
Camera::Channel::invalidate
void invalidate()
Camera::ChannelImplManager::setImage
static void setImage(const cv::Mat &image)
camera.h
Camera::Device::setWidth
void setWidth(const unsigned width)
color.hpp
Camera::Object::centroid
const Point2< unsigned > & centroid() const
Rect< unsigned >
Camera::Channel::objects
const ObjectVector * objects() const
Camera::ChannelPtrVector
std::vector< Channel * > ChannelPtrVector
Definition: camera.hpp:115
Camera::Device::resolutionToWidth
static unsigned int resolutionToWidth(Resolution res)
Camera::ChannelImplManager::channelImpl
static ChannelImpl * channelImpl(const std::string &name)
Camera::Object
Definition: camera.hpp:41
Camera::ConfigPath
Definition: camera.hpp:117
Camera::Device::bgr
const unsigned char * bgr() const
Camera::ChannelImpl::objects
ObjectVector objects(const Config &config)
Camera::Object::dataLength
const size_t dataLength() const
Camera::Device::setConfig
void setConfig(const Config &config)
Camera::ChannelImpl
Definition: camera.hpp:66
cv
Definition: camera.hpp:34
Resolution
Resolution
Definition: camera.h:31
Camera::ChannelImplManager
Definition: camera.hpp:83
Camera::ChannelImpl::~ChannelImpl
virtual ~ChannelImpl()
Camera::Device
Definition: camera.hpp:131
Camera::cDevice
Camera::Device * cDevice()
Camera
Definition: camera.hpp:38
WHITE_2016
Definition: camera.h:41