WebCamImageProvider.cpp
Go to the documentation of this file.
1 #/*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "WebCamImageProvider.h"
25 
26 #include <opencv2/imgproc/imgproc.hpp>
27 
31 
33 
34 #include <Calibration/Calibration.h>
35 
36 namespace visionx
37 {
39  {
40  }
41 
42  std::string
44  {
45  return "WebCamImageProvider";
46  }
47 
48  std::vector<ImageDimension>
50  {
51  std::vector<ImageDimension> supportedVideoResolutions;
52  const ImageDimension CommonResolutions[] = {ImageDimension(320, 240),
53  ImageDimension(640, 480),
54  ImageDimension(800, 600),
55  ImageDimension(768, 576),
56  ImageDimension(1024, 768),
57  ImageDimension(1280, 720),
58  ImageDimension(1280, 960),
59  ImageDimension(1280, 1024),
60  ImageDimension(1600, 1200),
61  ImageDimension(1920, 1080),
62  ImageDimension(1920, 1200),
63  ImageDimension(2560, 1440),
64  ImageDimension(3849, 2160)};
65  int nbTests = sizeof(CommonResolutions) / sizeof(CommonResolutions[0]);
66 
67  for (int i = 0; i < nbTests; i++)
68  {
69  ImageDimension test = CommonResolutions[i];
70 
71  // try to set resolution
72  camera.set(cv::CAP_PROP_FRAME_WIDTH, test.width);
73  camera.set(cv::CAP_PROP_FRAME_HEIGHT, test.height);
74  camera.set(cv::CAP_PROP_AUTOFOCUS, 0);
75 
76  double width = camera.get(cv::CAP_PROP_FRAME_WIDTH);
77  double height = camera.get(cv::CAP_PROP_FRAME_HEIGHT);
78  if (test.width == width && test.height == height)
79  {
80  supportedVideoResolutions.push_back(test);
81  }
82  }
83 
84  return supportedVideoResolutions;
85  }
86 
87  void
89  {
90  ARMARX_INFO << "Opening camera";
91  capturer.open(getProperty<int>("DeviceNumber").getValue());
92  ARMARX_CHECK_EXPRESSION(capturer.isOpened());
93  // CCalibration::LoadCameraParameters();
94  setNumberImages(1);
95  auto dim = getProperty<ImageDimension>("ImageResolution").getValue();
96  if (dim.width == 0)
97  {
98  auto supportedResolutions = getSupportedResolutions(capturer);
99  ARMARX_INFO << "Supported resolutions: " << supportedResolutions;
100  ARMARX_CHECK_POSITIVE(supportedResolutions.size());
101  capturer.set(cv::CAP_PROP_FRAME_WIDTH, supportedResolutions.rbegin()->width);
102  capturer.set(cv::CAP_PROP_FRAME_HEIGHT, supportedResolutions.rbegin()->height);
103  ARMARX_INFO << "Using max resolution found for camera: "
104  << capturer.get(cv::CAP_PROP_FRAME_WIDTH) << "x"
105  << capturer.get(cv::CAP_PROP_FRAME_HEIGHT);
106  }
107  else
108  {
109  capturer.set(cv::CAP_PROP_FRAME_WIDTH, dim.width);
110  capturer.set(cv::CAP_PROP_FRAME_HEIGHT, dim.height);
111  ARMARX_INFO << "Using resolution " << capturer.get(cv::CAP_PROP_FRAME_WIDTH) << "x"
112  << capturer.get(cv::CAP_PROP_FRAME_HEIGHT);
113  }
114  // setImageFormat(ImageDimension(dim.width, dim.height), visionx::eRgb, visionx::eBayerPatternGr);
115  setImageFormat(ImageDimension(capturer.get(cv::CAP_PROP_FRAME_WIDTH),
116  capturer.get(cv::CAP_PROP_FRAME_HEIGHT)),
117  visionx::eRgb,
118  visionx::eBayerPatternGr);
119  if (getProperty<float>("FPS").isSet())
120  {
121  setImageSyncMode(visionx::eFpsSynchronization);
122  frameRate = getProperty<float>("FPS").getValue();
123  ARMARX_INFO << "Using " << frameRate << " FPS";
124  }
125  else
126  {
127  setImageSyncMode(visionx::eCaptureSynchronization);
128  }
129  }
130 
131  void
133  {
134  capturer.release();
135  }
136 
137  bool
139  {
140  // TIMING_START(capture);
141  auto result = capturer.read(image);
142  // TIMING_END(capture);
143  auto tmpSharedMemoryProvider = sharedMemoryProvider;
144  if (result && tmpSharedMemoryProvider)
145  {
146  Ice::Byte* pixels = static_cast<Ice::Byte*>(image.data);
147  // TIMING_START(conversion);
148  updateTimestamp(armarx::TimeUtil::GetTime().toMicroSeconds());
149  cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
150  // TIMING_END(conversion);
151  // TIMING_START(mutex);
153  tmpSharedMemoryProvider->getScopedWriteLock());
154  // TIMING_END(mutex);
155  // TIMING_START(copy);
156  memcpy(ppImageBuffers[0], pixels, image.cols * image.rows * image.channels());
157  // TIMING_END(copy);
158  }
159  // TIMING_END(capture);
160  return result;
161  }
162 
163  void
165  {
166  ARMARX_INFO << __FUNCTION__;
167  // capturer.set(CV_CAP_PROP_FPS, framesPerSecond);
168  }
169 
170  void
172  {
173  }
174 
177  {
179  new WebCamImageProviderPropertyDefinitions(getConfigIdentifier()));
180  }
181 
182  visionx::MonocularCalibration
184  {
186  }
187 
188  std::vector<imrec::ChannelPreferences>
190  {
191  ARMARX_TRACE;
192 
193  imrec::ChannelPreferences cp;
194  cp.requiresLossless = false;
195  cp.name = "rgb";
196  return {cp};
197  }
198 } // namespace visionx
visionx::WebCamImageProviderPropertyDefinitions
Definition: WebCamImageProvider.h:33
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::WebCamImageProvider::onExitCapturingImageProvider
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
Definition: WebCamImageProvider.cpp:132
visionx::WebCamImageProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: WebCamImageProvider.cpp:176
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:47
WebCamImageProvider.h
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
visionx::WebCamImageProvider::onStartCapture
void onStartCapture(float framesPerSecond) override
This is called when the image provider capturing has been started.
Definition: WebCamImageProvider.cpp:164
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:109
visionx::WebCamImageProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: WebCamImageProvider.cpp:43
NotImplementedYetException.h
ARMARX_CHECK_POSITIVE
#define ARMARX_CHECK_POSITIVE(number)
This macro evaluates whether number is positive (> 0) and if it turns out to be false it will throw a...
Definition: ExpressionException.h:145
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
visionx::WebCamImageProvider::getImageRecordingChannelPreferences
std::vector< imrec::ChannelPreferences > getImageRecordingChannelPreferences(const Ice::Current &) override
Definition: WebCamImageProvider.cpp:189
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
visionx::WebCamImageProvider::onInitCapturingImageProvider
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
Definition: WebCamImageProvider.cpp:88
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::exceptions::user::NotImplementedYetException
Throw this exception to indicate missing functionality.
Definition: NotImplementedYetException.h:39
ImageUtil.h
visionx::WebCamImageProvider::getSupportedResolutions
std::vector< ImageDimension > getSupportedResolutions(cv::VideoCapture &camera)
Definition: WebCamImageProvider.cpp:49
visionx::WebCamImageProvider::getCalibration
MonocularCalibration getCalibration(const Ice::Current &)
Definition: WebCamImageProvider.cpp:183
visionx::WebCamImageProvider::onStopCapture
void onStopCapture() override
This is called when the image provider capturing has been stopped.
Definition: WebCamImageProvider.cpp:171
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
visionx::WebCamImageProvider::WebCamImageProvider
WebCamImageProvider()
Definition: WebCamImageProvider.cpp:38