YARP  2.3.68+272-20170522.1+git50f0ae7
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) 2010 RobotCub Consortium
* Author: Paul Fitzpatrick
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#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:
yarp::os::ConstString pattern, lastLoad;
int first, last, at;
int h, w;
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) {
config.check("pattern",yarp::os::Value("%d.ppm")).asString();
int first = config.check("first",yarp::os::Value(0)).asInt();
int last = config.check("last",yarp::os::Value(-1)).asInt();
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) 2010 RobotCub Consortium
* Author: Paul Fitzpatrick
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/
#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;
}