27# include <opencv2/opencv.hpp>
29# include <opencv2/videoio.hpp>
52 virtual const std::string
toFile(
const std::string&,
unsigned int) = 0;
71 const std::string
toFile(
const std::string &dirName,
unsigned int cnt)
override
105 const std::string
toFile(
const std::string &dirName,
unsigned int cnt)
override
161 std::ostringstream
fName;
162 fName << std::setw(8) << std::setfill(
'0') << cnt << ext;
169 const cv::Mat &getImage()
211 void setRxStamp(
const double stamp) { rxStamp=stamp; rxOk=
true; }
212 void setTxStamp(
const double stamp) { txStamp=stamp; txOk=
true; }
225 std::ostringstream ret;
233 if (!ret.str().empty()) {
282 firstIncomingData=
true;
287 unsigned int dwnsample;
289 bool firstIncomingData;
295 void onRead(T &obj)
override
297 if (++cnt==dwnsample)
299 if (firstIncomingData)
301 yInfo() <<
"Incoming data detected";
302 firstIncomingData=
false;
311 if (txTime || (
info.isValid() && !rxTime)) {
312 item.timeStamp.setTxStamp(
info.getTime());
315 if (rxTime || !
info.isValid()) {
320 item.obj->attachFormat(itemformat);
341 std::string infoFile;
342 std::string dataFile;
343 unsigned int blockSize;
344 unsigned int cumulSize;
345 unsigned int counter;
350 std::string videoType;
385 infoFile+=
"/info.log";
388 dataFile+=
"/data.log";
392 transform(videoType.begin(),videoType.end(),videoType.begin(),
::tolower);
393 if ((videoType!=
"mkv") && (videoType!=
"avi"))
395 yWarning() <<
"unknown video type '" << videoType <<
"' specified; "
396 <<
"'mkv' type will be used.";
412 void writeSource(
const std::string &sourceName,
const bool connected)
414 finfo <<
"[" << std::fixed <<
Time::now() <<
"] ";
415 finfo << sourceName <<
" ";
416 finfo << (connected?
"[connected]":
"[disconnected]") <<
'\n';
423 finfo.open(infoFile.c_str());
424 if (!finfo.is_open())
426 yError() <<
"unable to open file: " << infoFile;
437 finfo <<
" Video:" << videoType <<
"(huffyuv);";
442 finfo <<
"DepthCompressed;";
444 finfo <<
"Image:jpg;";
446 finfo <<
"Image:png;";
448 yError() <<
"I should not reach this line! Unknown data type" << (
int)type;
453 if (txTime && rxTime) {
462 fdata.
open(dataFile.c_str());
463 if (!fdata.is_open())
465 yError() <<
"unable to open file: " << dataFile;
489 unsigned int sz=(
unsigned int)buf.size();
495 if ((
curTime-oldTime>10.0) || closing)
516 int frameW=img.size().width;
517 int frameH=img.size().height;
520 double dt=
itemEnd.timeStamp.getStamp()-t0;
532 for (
unsigned int i=0; i<
sz; i++)
539 fdata <<
item.seqNumber <<
' ' <<
item.timeStamp.getString() <<
' ';
541 fdata <<
item.obj->toFile(dirName,counter++) <<
'\n';
543 std::ostringstream frame;
544 frame <<
"frame_" << std::setw(8) << std::setfill(
'0') << counter++;
545 fdata << frame.str() <<
'\n';
554 int dt=(
int)(1000.0*(
item.timeStamp.getStamp()-t0));
563 yInfo() <<
sz <<
" items stored [cumul #: " << cumulSize <<
"]";
596 if ((thread !=
nullptr) &&
info.incoming) {
616 unsigned int dwnsample{0};
617 std::string portName;
619 void polish_filename(std::string &
fname)
621 std::array<char,6>
notallowed={
':',
'*',
'?',
'|',
'>',
'<'};
624#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
642 portName=rf.
check(
"name",
Value(
"/dump")).asString();
643 if (portName[0] !=
'/') {
644 portName =
"/" + portName;
649 std::string videoType=rf.
check(
"videoType",
Value(
"mkv")).asString();
651 if (rf.
check(
"type"))
670 if (rf.
check(
"addVideo")) {
679 if (rf.
check(
"addVideo")) {
688 if (rf.
check(
"addVideo")) {
703 yError() <<
"Error: invalid type";
712 dwnsample=rf.
check(
"downsample",
Value(1)).asInt32();
713 rxTime=rf.
check(
"rxTime");
714 txTime=rf.
check(
"txTime");
722 if (rf.
check(
"overwrite")) {
744 t=
new DumpThread(dumptype,*q,dirName,100,saveData,videoOn,videoType,rxTime,txTime);
760 p_bottle->
open(portName);
768 p_image->
open(portName);
773 if (rf.
check(
"connect"))
776 bool ok=Network::connect(
srcPort.c_str(),
780 std::ostringstream
msg;
781 msg <<
"Connection to " <<
srcPort <<
" " << (ok?
"successful":
"failed");
791 rpcPort.
open(portName+
"/rpc");
794 yInfo() <<
"Service yarp port: " << portName;
795 yInfo() <<
"Data stored in : " << dirName;
832int main(
int argc,
char *argv[])
839 if (rf.
check(
"help"))
841 yInfo() <<
"Options:";
842 yInfo() <<
"\t--name port: service port name (default: /dump)";
843 yInfo() <<
"\t--connect port: name of the port to connect the dumper to at launch time";
844 yInfo() <<
"\t--dir name: provide explicit name of storage directory";
845 yInfo() <<
"\t--overwrite : overwrite pre-existing storage directory";
847 yInfo() <<
"\t--type type: type of the data to be dumped [bottle(default), image, image_jpg, image_png, video, depth, depth_compressed]";
848 yInfo() <<
"\t--addVideo : produce video as well (if image* is selected)";
849 yInfo() <<
"\t--videoType ext: produce video of specified container type [mkv(default), avi]";
851 yInfo() <<
"\t--type type: type of the data to be dumped [bottle(default), image, image_jpg, image_png, depth, depth_compressed]";
853 yInfo() <<
"\t--downsample n: downsample rate (default: 1 => downsample disabled)";
854 yInfo() <<
"\t--rxTime : dump the receiver time instead of the sender time";
855 yInfo() <<
"\t--txTime : dump the sender time straightaway";
861 if (!
yarp.checkNetwork())
863 yError()<<
"YARP server not available!";
DumpBottle(const Bottle &b)
DumpBottle(const DumpBottle &obj)
const DumpBottle & operator=(const DumpBottle &obj)
const std::string toFile(const std::string &dirName, unsigned int cnt) override
DumpImage(const DumpImage &obj)
DumpImage(const Image &img)
const std::string toFile(const std::string &dirName, unsigned int cnt) override
const DumpImage & operator=(const DumpImage &obj)
bool close() override
Close function.
bool updateModule() override
Override this to do whatever your module needs to do.
bool configure(ResourceFinder &rf) override
Configure the module, pass a ResourceFinder object to the module.
double getPeriod() override
You can override this to control the approximate periodicity at which updateModule() is called by run...
virtual ~DumpObj()=default
virtual void attachFormat(const DumpFormat &format)
virtual const std::string toFile(const std::string &, unsigned int)=0
DumpPort(DumpQueue &Q, unsigned int _dwnsample=1, bool _rxTime=true, bool _txTime=false, DumpFormat _dataformat=DumpFormat::bottle)
void setThread(DumpThread *thread)
void report(const PortInfo &info) override
Callback for port event/state information.
DumpThread(DumpFormat _type, DumpQueue &Q, const std::string &_dirName, const int szToWrite, const bool _saveData, const bool _videoOn, const std::string &_videoType, const bool _rxTime, const bool _txTime)
void writeSource(const std::string &sourceName, const bool connected)
bool threadInit() override
Initialization method.
void run() override
Loop function.
void threadRelease() override
Release method.
std::string getString() const
void setTxStamp(const double stamp)
void setRxStamp(const double stamp)
A simple collection of objects that can be described and transmitted in a portable way.
std::string toString() const override
Gives a human-readable textual representation of the bottle.
A mini-server for performing network communication in the background.
std::string getName() const override
Get name of port.
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
void close() override
Stop port activity.
BufferedPort()
Constructor.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void setReporter(PortReport &reporter) override
Set a callback to be called upon any future connections and disconnections to/from the port.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
Utilities for manipulating the YARP network, including initialization and shutdown.
An abstraction for a periodic thread.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Information about a port connection or event.
A base class for objects that want information about port status changes.
A mini-server for network communication.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A base-class for standard YARP modules that supports ResourceFinder.
virtual int runModule()
Calls updateModule() until that returns false.
virtual bool attach(yarp::os::Port &source)
Make any input from a Port object go to the respond() method.
Helper class for finding config files and other external resources.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
An abstraction for a time stamp and/or sequence number.
A single value (typically within a Bottle).
virtual std::string asString() const
Get string value.
Base class for storing images.
virtual int getPixelCode() const
Gets pixel type identifier.
int main(int argc, char *argv[])
DumpObj * factory(Bottle &obj)
::cv::Mat toCvMat(const yarp::sig::Image &yarpImage)
Convert a yarp::sig::FlexImage to a cv::Mat object.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
An interface to the operating system, including Port based communication.
int stat(const char *path)
Portable wrapper for the stat() function.
int mkdir_p(const char *p, int ignoreLevels=0)
Create a directory and all parent directories needed.
@ FORMAT_NUMERIC_COMPRESSED
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
The main, catch-all namespace for YARP.