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 #include <opencv2/imgproc/imgproc.hpp>
28 #include <Calibration/Calibration.h>
32 namespace visionx
33 {
35  {
36 
37  }
38 
39 
40 
41 
43  {
44  return "WebCamImageProvider";
45  }
46 
47 
48  std::vector<ImageDimension> visionx::WebCamImageProvider::getSupportedResolutions(cv::VideoCapture& camera)
49  {
50  std::vector<ImageDimension> supportedVideoResolutions;
51  const ImageDimension CommonResolutions[] =
52  {
53  ImageDimension(320, 240),
54  ImageDimension(640, 480),
55  ImageDimension(800, 600),
56  ImageDimension(768, 576),
57  ImageDimension(1024, 768),
58  ImageDimension(1280, 720),
59  ImageDimension(1280, 960),
60  ImageDimension(1280, 1024),
61  ImageDimension(1600, 1200),
62  ImageDimension(1920, 1080),
63  ImageDimension(1920, 1200),
64  ImageDimension(2560, 1440),
65  ImageDimension(3849, 2160)
66  };
67  int nbTests = sizeof(CommonResolutions) / sizeof(CommonResolutions[0]);
68 
69  for (int i = 0; i < nbTests; i++)
70  {
71  ImageDimension test = CommonResolutions[i];
72 
73  // try to set resolution
74  camera.set(cv::CAP_PROP_FRAME_WIDTH, test.width);
75  camera.set(cv::CAP_PROP_FRAME_HEIGHT, test.height);
76  camera.set(cv::CAP_PROP_AUTOFOCUS, 0);
77 
78  double width = camera.get(cv::CAP_PROP_FRAME_WIDTH);
79  double height = camera.get(cv::CAP_PROP_FRAME_HEIGHT);
80  if (test.width == width && test.height == height)
81  {
82  supportedVideoResolutions.push_back(test);
83  }
84  }
85 
86  return supportedVideoResolutions;
87  }
88 
89 
91  {
92  ARMARX_INFO << "Opening camera";
93  capturer.open(getProperty<int>("DeviceNumber").getValue());
94  ARMARX_CHECK_EXPRESSION(capturer.isOpened());
95  // CCalibration::LoadCameraParameters();
96  setNumberImages(1);
97  auto dim = getProperty<ImageDimension>("ImageResolution").getValue();
98  if (dim.width == 0)
99  {
100  auto supportedResolutions = getSupportedResolutions(capturer);
101  ARMARX_INFO << "Supported resolutions: " << supportedResolutions;
102  ARMARX_CHECK_POSITIVE(supportedResolutions.size());
103  capturer.set(cv::CAP_PROP_FRAME_WIDTH, supportedResolutions.rbegin()->width);
104  capturer.set(cv::CAP_PROP_FRAME_HEIGHT, supportedResolutions.rbegin()->height);
105  ARMARX_INFO << "Using max resolution found for camera: " << capturer.get(cv::CAP_PROP_FRAME_WIDTH) << "x" << 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" << capturer.get(cv::CAP_PROP_FRAME_HEIGHT);
112  }
113  // setImageFormat(ImageDimension(dim.width, dim.height), visionx::eRgb, visionx::eBayerPatternGr);
114  setImageFormat(ImageDimension(capturer.get(cv::CAP_PROP_FRAME_WIDTH), capturer.get(cv::CAP_PROP_FRAME_HEIGHT)), visionx::eRgb, visionx::eBayerPatternGr);
115  if (getProperty<float>("FPS").isSet())
116  {
117  setImageSyncMode(visionx::eFpsSynchronization);
118  frameRate = getProperty<float>("FPS").getValue();
119  ARMARX_INFO << "Using " << frameRate << " FPS";
120  }
121  else
122  {
123  setImageSyncMode(visionx::eCaptureSynchronization);
124  }
125  }
126 
128  {
129  capturer.release();
130 
131  }
132 
133  bool visionx::WebCamImageProvider::capture(void** ppImageBuffers)
134  {
135  // TIMING_START(capture);
136  auto result = capturer.read(image);
137  // TIMING_END(capture);
138  auto tmpSharedMemoryProvider = sharedMemoryProvider;
139  if (result && tmpSharedMemoryProvider)
140  {
141  Ice::Byte* pixels = static_cast<Ice::Byte*>(image.data);
142  // TIMING_START(conversion);
143  updateTimestamp(armarx::TimeUtil::GetTime().toMicroSeconds());
144  cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
145  // TIMING_END(conversion);
146  // TIMING_START(mutex);
147  armarx::SharedMemoryScopedWriteLockPtr lock(tmpSharedMemoryProvider->getScopedWriteLock());
148  // TIMING_END(mutex);
149  // TIMING_START(copy);
150  memcpy(ppImageBuffers[0], pixels, image.cols * image.rows * image.channels());
151  // TIMING_END(copy);
152 
153  }
154  // TIMING_END(capture);
155  return result;
156  }
157 
158 
159 
160 
162  {
163  ARMARX_INFO << __FUNCTION__;
164  // capturer.set(CV_CAP_PROP_FPS, framesPerSecond);
165 
166  }
167 
169  {
170  }
171 
172 
174  {
176  }
177 
178 
179  visionx::MonocularCalibration visionx::WebCamImageProvider::getCalibration(const Ice::Current&)
180  {
182  }
183 
184 
185  std::vector<imrec::ChannelPreferences>
187  {
188  ARMARX_TRACE;
189 
190  imrec::ChannelPreferences cp;
191  cp.requiresLossless = false;
192  cp.name = "rgb";
193  return {cp};
194  }
195 }
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:127
visionx::WebCamImageProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: WebCamImageProvider.cpp:173
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:46
WebCamImageProvider.h
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
visionx::WebCamImageProvider::onStartCapture
void onStartCapture(float framesPerSecond) override
This is called when the image provider capturing has been started.
Definition: WebCamImageProvider.cpp:161
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:106
visionx::WebCamImageProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: WebCamImageProvider.cpp:42
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:186
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:90
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
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:48
visionx::WebCamImageProvider::getCalibration
MonocularCalibration getCalibration(const Ice::Current &)
Definition: WebCamImageProvider.cpp:179
visionx::WebCamImageProvider::onStopCapture
void onStopCapture() override
This is called when the image provider capturing has been stopped.
Definition: WebCamImageProvider.cpp:168
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
visionx::WebCamImageProvider::WebCamImageProvider
WebCamImageProvider()
Definition: WebCamImageProvider.cpp:34