YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FrameGrabber_nwc_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
8
9#include <yarp/os/Network.h>
11#include <yarp/os/LogStream.h>
12
14#include <yarp/sig/ImageUtils.h>
15
16namespace {
17YARP_LOG_COMPONENT(FRAMEGRABBER_NWC_YARP, "yarp.devices.frameGrabber_nwc_yarp")
18} // namespace
19
20
21
22// BEGIN StreamReceiver
24{
25 return m_lastHeight;
26}
27
29{
30 return m_lastWidth;
31}
32
34{
35 return m_lastStamp;
36}
37
38template <typename ImageType>
40{
41 std::lock_guard<std::mutex> lock(m_mutex);
42
43 bool ret {false};
44 if (!port.isOpen()) {
45 image.zero();
46 ret = false;
47 } else if (reader.check()) {
48 ret = image.copy(*(reader.read(true)));
49 reader.getEnvelope(m_lastStamp);
50 m_lastHeight = image.height();
51 m_lastWidth = image.width();
52 } else {
53 ret = image.copy(*(reader.lastRead()));
54 }
55
56 return ret;
57}
58
59bool StreamReceiver::open(const std::string& local,
60 const std::string& remote,
61 const std::string& carrier)
63 std::lock_guard<std::mutex> lock(m_mutex);
65 if (!port.open(local)) {
66 yCError(FRAMEGRABBER_NWC_YARP) << "Failed to open " << local << "port.";
67 }
68
69 if (!remote.empty()) {
70 yCInfo(FRAMEGRABBER_NWC_YARP) << "Connecting" << port.getName() << "to" << remote;
71 if (!yarp::os::NetworkBase::connect(remote, port.getName(), carrier)) {
72 yCError(FRAMEGRABBER_NWC_YARP) << "Failed to connect" << local << "to" << remote;
73 return false;
74 }
75 } else {
76 yCInfo(FRAMEGRABBER_NWC_YARP) << "No remote specified. Waiting for connection";
77 }
78
79 reader.attach(port);
80
81 return true;
82}
83
85{
86 std::lock_guard<std::mutex> lock(m_mutex);
87
88 m_lastStamp = {0, 0.0};
89 m_lastHeight = 0;
90 m_lastWidth = 0;
91
92 if (!port.isOpen()) {
93 return true;
94 }
95 port.interrupt();
96 port.close();
97
98 return true;
99}
100// END StreamReceiver
101
102
103// BEGIN FrameGrabberOf_ForwarderWithStream
104template <typename ImageType,
105 yarp::conf::vocab32_t IfVocab,
106 yarp::conf::vocab32_t ImgVocab>
108 yarp::proto::framegrabber::FrameGrabberOf_Forwarder<ImageType, IfVocab, ImgVocab>(rpcPort)
109{
110}
111
112template <typename ImageType,
113 yarp::conf::vocab32_t IfVocab,
114 yarp::conf::vocab32_t ImgVocab>
116{
117 std::lock_guard<std::mutex> lock(m_mutex);
118 if (!m_streamReceiver) {
120 }
121
122 return m_streamReceiver->lastHeight();
123}
124
125template <typename ImageType,
126 yarp::conf::vocab32_t IfVocab,
127 yarp::conf::vocab32_t ImgVocab>
129{
130 std::lock_guard<std::mutex> lock(m_mutex);
131 if (!m_streamReceiver) {
133 }
134
135 return m_streamReceiver->lastWidth();
136}
137
138template <typename ImageType,
139 yarp::conf::vocab32_t IfVocab,
140 yarp::conf::vocab32_t ImgVocab>
142{
143 std::lock_guard<std::mutex> lock(m_mutex);
144 if (!m_streamReceiver) {
146 }
147
148 return m_streamReceiver->lastImage(image);
149}
150
151template <typename ImageType,
152 yarp::conf::vocab32_t IfVocab,
153 yarp::conf::vocab32_t ImgVocab>
155 yarp::sig::VectorOf<std::pair<int, int>> vertices,
157{
158 std::lock_guard<std::mutex> lock(m_mutex);
159 if(!m_streamReceiver) {
161 }
162
163 if (cropType == YARP_CROP_RECT) {
164 if (vertices.size() != 2) {
165 yCError(FRAMEGRABBER_NWC_YARP, "GetImageCrop failed: RECT mode requires 2 vertices");
166 return false;
167 }
168 ImageType full;
169 bool b = m_streamReceiver->lastImage(full);
170 if (!b || full.width() == 0 || full.height() == 0)
171 {
172 yCError(FRAMEGRABBER_NWC_YARP, "GetImageCrop failed: No image received");
173 return false;
174 }
175
176 if (!yarp::sig::utils::cropRect(full, vertices[0], vertices[1], image)) {
177 yCError(FRAMEGRABBER_NWC_YARP, "GetImageCrop failed: utils::cropRect error: (%d, %d) (%d, %d)",
178 vertices[0].first,
179 vertices[0].second,
180 vertices[1].first,
181 vertices[1].second);
182 return false;
183 }
184 }
185 else if (cropType == YARP_CROP_LIST) {
186 yCError(FRAMEGRABBER_NWC_YARP, "List type not yet implemented");
187 return false;
188 }
189
190 return true;
191}
192
193
194template <typename ImageType,
195 yarp::conf::vocab32_t IfVocab,
196 yarp::conf::vocab32_t ImgVocab>
198{
199 std::lock_guard<std::mutex> lock(m_mutex);
200 m_streamReceiver = streamReceiver;
201}
202// END FrameGrabberOf_ForwarderWithStream
203
204
205
206// BEGIN FrameGrabber_nwc_yarp
208 FrameGrabberOf_ForwarderWithStream<yarp::sig::ImageOf<yarp::sig::PixelRgb>>(rpcPort),
209 FrameGrabberOf_ForwarderWithStream<yarp::sig::ImageOf<yarp::sig::PixelMono>, VOCAB_FRAMEGRABBER_IMAGERAW>(rpcPort),
210 FrameGrabberOf_ForwarderWithStream<yarp::sig::ImageOf<yarp::sig::PixelFloat>>(rpcPort),
211 FrameGrabberOf_ForwarderWithStream<yarp::sig::FlexImage>(rpcPort),
212 yarp::proto::framegrabber::FrameGrabberControls_Forwarder(rpcPort),
213 yarp::proto::framegrabber::FrameGrabberControlsDC1394_Forwarder(rpcPort),
214 yarp::proto::framegrabber::RgbVisualParams_Forwarder(rpcPort)
215{
216}
217
218
220{
221 if (!this->parseParams(config)) { return false; }
222
223 if (!m_no_stream) {
224 if (!streamReceiver.open(m_local, m_remote, m_carrier)) {
225 return false;
226 }
231 }
232
233 std::string rpc_local = m_local + "/rpc_client";
234 std::string rpc_remote = m_remote + "/rpc";
235 if (!rpcPort.open(rpc_local)) {
236 yCError(FRAMEGRABBER_NWC_YARP) << "Failed to open " << rpc_local << "port.";
237 }
238
239 if (!m_remote.empty()) {
240 yCInfo(FRAMEGRABBER_NWC_YARP) << "Connecting" << rpcPort.getName() << "to" << rpc_remote;
241 if (!yarp::os::NetworkBase::connect(rpcPort.getName(), rpc_remote)) {
242 yCError(FRAMEGRABBER_NWC_YARP) << "Failed to connect" << rpcPort.getName() << "to" << rpc_remote;
243 return false;
244 }
245 } else {
246 yCInfo(FRAMEGRABBER_NWC_YARP) << "No remote specified. Waiting for connection";
247 }
248
249 return true;
250}
251
253{
254 rpcPort.interrupt();
255 rpcPort.close();
256
257 streamReceiver.close();
258
259 return true;
260}
261
262
267
268// END FrameGrabber_nwc_yarp
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_IMAGERAW
cropType_id_t
@ YARP_CROP_LIST
@ YARP_CROP_RECT
yarp::sig::ImageOf< yarp::sig::PixelRgb > ImageType
Definition ImageType.h:24
bool ret
int width() const override
Return the width of each frame.
bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf< std::pair< int, int > > vertices, ImageType &image) override
Get a crop of the image from the frame grabber.
FrameGrabberOf_ForwarderWithStream(yarp::os::Port &rpcPort)
int height() const override
Return the height of each frame.
void setStreamReceiver(StreamReceiver *m_streamReceiver)
bool getImage(ImageType &image) override
Get an image from the frame grabber.
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
bool lastImage(ImageType &image)
yarp::os::Stamp lastStamp() const
bool open(const std::string &local, const std::string &remote, const std::string &carrier)
virtual std::string getName() const
Get name of port.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:682
T * read(bool shouldWait=true) override
Read an available object from the port.
virtual bool getEnvelope(PortReader &envelope)
bool check()
Check if data is available.
void attach(Port &port)
Attach this buffer to a particular port.
T * lastRead() override
Get the last data returned by read()
A mini-server for network communication.
Definition Port.h:46
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:383
void close() override
Stop port activity.
Definition Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
bool isOpen() const
Check if the port has been opened.
Definition Port.cpp:677
A base class for nested structures that can be searched.
Definition Searchable.h:31
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
int height() const override
Return the height of each frame.
int width() const override
Return the width of each frame.
bool getImage(ImageType &image) override
Get an image from the frame grabber.
bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf< std::pair< int, int > > vertices, ImageType &image) override
Get a crop of the image from the frame grabber.
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
size_t size() const
Definition Vector.h:341
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::int32_t vocab32_t
Definition numeric.h:78
bool cropRect(const yarp::sig::Image &inImg, const std::pair< unsigned int, unsigned int > &vertex1, const std::pair< unsigned int, unsigned int > &vertex2, yarp::sig::Image &outImg)
Crop a rectangle area out of an image given two opposite vertices.
The main, catch-all namespace for YARP.
Definition dirs.h:16