YARP
Yet Another Robot Platform
dev/file_grabber.cpp

This example shows how we would make a new device for a camera.We make a device that reads images from file. The only step missing here is to integrate the device into the YARP library.

Here's the header file, dev/FileFrameGrabber.h

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <stdio.h>
#include <yarp/os/all.h>
#include <yarp/sig/all.h>
#include <yarp/dev/all.h>
class FileFrameGrabber : public yarp::dev::IFrameGrabberImage,
private:
std::string pattern, lastLoad;
int first, last, at;
int h, w;
bool findImage(yarp::sig::ImageOf<yarp::sig::PixelRgb>& image) {
bool triedFirst = false;
char buf[1000];
sprintf(buf,pattern.c_str(),at);
while (!yarp::sig::file::read(image,buf)) {
if (at==first) {
if (triedFirst) {
return false;
}
triedFirst = true;
}
if (last==-1) {
at = first;
} else {
at++;
if (at>last) {
at = first;
}
}
sprintf(buf,pattern.c_str(),at);
}
lastLoad = buf;
h = image.height();
w = image.width();
return true;
}
public:
FileFrameGrabber() {
pattern = "%d.ppm";
first = last = -1;
at = -1;
h = w = 0;
}
bool open(const char *pattern, int first, int last) {
this->pattern = pattern;
this->first = first;
this->last = last;
at = first;
return findImage(dummy);
}
virtual bool open(yarp::os::Searchable& config) {
std::string pattern =
config.check("pattern",yarp::os::Value("%d.ppm")).asString();
int first = config.check("first",yarp::os::Value(0)).asInt32();
int last = config.check("last",yarp::os::Value(-1)).asInt32();
return open(pattern.c_str(),first,last);
}
virtual bool close() {
return true; // easy
}
bool ok = findImage(image);
if (ok) {
printf("showing image %s\n", lastLoad.c_str());
at++;
if (last!=-1 && at>last) {
at = first;
}
}
return ok;
}
virtual int height() const {
return h;
}
virtual int width() const {
return w;
}
};

And here's how we can use it. In fact, we just grab the first image from the device.

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include "FileFrameGrabber.h"
using namespace yarp::os;
using namespace yarp::sig;
using namespace yarp::dev;
int main(int argc, char *argv[]) {
// give YARP a factory for creating instances of FileFrameGrabber
DriverCreator *file_grabber_factory =
"grabber",
"FileFrameGrabber");
Drivers::factory().add(file_grabber_factory); // hand factory over to YARP
// use YARP to create and configure an instance of FileFrameGrabber
Property config;
if (argc==1) {
// no arguments, use a default
config.fromString("(device file_grabber) (pattern \"image/%03d.ppm\")");
} else {
// expect something like '--device file_grabber --pattern "image/%03d.ppm"'
// or '--device dragonfly'
// or '--device test_grabber --period 0.5 --mode [ball]'
config.fromCommand(argc,argv);
}
PolyDriver dd(config);
if (!dd.isValid()) {
printf("Failed to create and configure a device\n");
return 1;
}
IFrameGrabberImage *grabberInterface;
if (!dd.view(grabberInterface)) {
printf("Failed to view device through IFrameGrabberImage interface\n");
return 1;
}
grabberInterface->getImage(img);
printf("Got a %dx%d image\n", img.width(), img.height());
dd.close();
return 0;
}
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:68
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:656
all.h
yarp::sig
Signal processing.
Definition: Image.h:25
Drivers.h
yarp::dev::DeviceDriver::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: DeviceDriver.h:58
yarp::dev::DriverCreatorOf
A factory for creating driver objects of a particular type.
Definition: Drivers.h:19
yarp::os::Property::fromString
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1046
all.h
yarp::dev::PolyDriver::isValid
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:194
yarp::dev::DeviceDriver
Definition: DeviceDriver.h:37
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:259
yarp::dev::DeviceDriver::view
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::sig::ImageOf< yarp::sig::PixelRgb >
all.h
yarp::os::Property::fromCommand
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1057
yarp::dev::IFrameGrabberImage
Definition: FrameGrabberInterfaces.h:240
PolyDriver.h
yarp::dev::IFrameGrabberImage::getImage
virtual bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image)=0
Get an rgb image from the frame grabber, if required demosaicking/color reconstruction is applied.
yarp::dev::PolyDriver
Definition: PolyDriver.h:25
yarp::dev::PolyDriver::close
bool close() override
Close the DeviceDriver.
Definition: PolyDriver.cpp:171
yarp::dev::DriverCreator
A base class for factories that create driver objects.
Definition: Drivers.h:31
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
FrameGrabberInterfaces.h
yarp::dev::DeviceDriver::close
bool close() override
Close the DeviceDriver.
Definition: DeviceDriver.h:64
yarp::os
Definition: AbstractCarrier.h:17
yarp::os::Network
Definition: Network.h:781
yarp
Definition: numeric.h:47
yarp::dev::IFrameGrabberImage::height
virtual int height() const =0
Return the height of each frame.
yarp::dev::IFrameGrabberImage::width
virtual int width() const =0
Return the width of each frame.
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:46
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:34