27# include <opencv2/opencv.hpp>
29# include <opencv2/core/core_c.h>
30# include <opencv2/videoio.hpp>
53 virtual const std::string
toFile(
const std::string&,
unsigned int) = 0;
72 const std::string
toFile(
const std::string &dirName,
unsigned int cnt)
override
106 const std::string
toFile(
const std::string &dirName,
unsigned int cnt)
override
162 std::ostringstream
fName;
163 fName << std::setw(8) << std::setfill(
'0') << cnt << ext;
170 const cv::Mat &getImage()
212 void setRxStamp(
const double stamp) { rxStamp=stamp; rxOk=
true; }
213 void setTxStamp(
const double stamp) { txStamp=stamp; txOk=
true; }
226 std::ostringstream
ret;
234 if (!
ret.str().empty()) {
283 firstIncomingData=
true;
288 unsigned int dwnsample;
290 bool firstIncomingData;
296 void onRead(T &obj)
override
298 if (++cnt==dwnsample)
300 if (firstIncomingData)
302 yInfo() <<
"Incoming data detected";
303 firstIncomingData=
false;
312 if (txTime || (
info.isValid() && !rxTime)) {
313 item.timeStamp.setTxStamp(
info.getTime());
316 if (rxTime || !
info.isValid()) {
321 item.obj->attachFormat(itemformat);
342 std::string infoFile;
343 std::string dataFile;
344 unsigned int blockSize;
345 unsigned int cumulSize;
346 unsigned int counter;
351 std::string videoType;
386 infoFile+=
"/info.log";
389 dataFile+=
"/data.log";
393 transform(videoType.begin(),videoType.end(),videoType.begin(),
::tolower);
394 if ((videoType!=
"mkv") && (videoType!=
"avi"))
396 yWarning() <<
"unknown video type '" << videoType <<
"' specified; "
397 <<
"'mkv' type will be used.";
413 void writeSource(
const std::string &sourceName,
const bool connected)
415 finfo <<
"[" << std::fixed <<
Time::now() <<
"] ";
416 finfo << sourceName <<
" ";
417 finfo << (connected?
"[connected]":
"[disconnected]") <<
'\n';
424 finfo.open(infoFile.c_str());
425 if (!finfo.is_open())
427 yError() <<
"unable to open file: " << infoFile;
438 finfo <<
" Video:" << videoType <<
"(huffyuv);";
443 finfo <<
"DepthCompressed;";
445 finfo <<
"Image:jpg;";
447 finfo <<
"Image:png;";
449 yError() <<
"I should not reach this line! Unknown data type" << (
int)type;
454 if (txTime && rxTime) {
463 fdata.
open(dataFile.c_str());
464 if (!fdata.is_open())
466 yError() <<
"unable to open file: " << dataFile;
490 unsigned int sz=(
unsigned int)buf.size();
496 if ((
curTime-oldTime>10.0) || closing)
517 int frameW=img.size().width;
518 int frameH=img.size().height;
521 double dt=
itemEnd.timeStamp.getStamp()-t0;
533 for (
unsigned int i=0; i<
sz; i++)
540 fdata <<
item.seqNumber <<
' ' <<
item.timeStamp.getString() <<
' ';
542 fdata <<
item.obj->toFile(dirName,counter++) <<
'\n';
544 std::ostringstream frame;
545 frame <<
"frame_" << std::setw(8) << std::setfill(
'0') << counter++;
546 fdata << frame.str() <<
'\n';
555 int dt=(
int)(1000.0*(
item.timeStamp.getStamp()-t0));
564 yInfo() <<
sz <<
" items stored [cumul #: " << cumulSize <<
"]";
597 if ((thread !=
nullptr) &&
info.incoming) {
617 unsigned int dwnsample{0};
618 std::string portName;
620 void polish_filename(std::string &
fname)
622 std::array<char,6>
notallowed={
':',
'*',
'?',
'|',
'>',
'<'};
625#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
643 portName=rf.
check(
"name",
Value(
"/dump")).asString();
644 if (portName[0] !=
'/') {
645 portName =
"/" + portName;
650 std::string videoType=rf.
check(
"videoType",
Value(
"mkv")).asString();
652 if (rf.
check(
"type"))
671 if (rf.
check(
"addVideo")) {
680 if (rf.
check(
"addVideo")) {
689 if (rf.
check(
"addVideo")) {
704 yError() <<
"Error: invalid type";
713 dwnsample=rf.
check(
"downsample",
Value(1)).asInt32();
714 rxTime=rf.
check(
"rxTime");
715 txTime=rf.
check(
"txTime");
723 if (rf.
check(
"overwrite")) {
745 t=
new DumpThread(dumptype,*q,dirName,100,saveData,videoOn,videoType,rxTime,txTime);
761 p_bottle->
open(portName);
769 p_image->
open(portName);
774 if (rf.
check(
"connect"))
777 bool ok=Network::connect(
srcPort.c_str(),
781 std::ostringstream msg;
782 msg <<
"Connection to " <<
srcPort <<
" " << (ok?
"successful":
"failed");
785 yInfo() << msg.str();
792 rpcPort.
open(portName+
"/rpc");
795 yInfo() <<
"Service yarp port: " << portName;
796 yInfo() <<
"Data stored in : " << dirName;
833int main(
int argc,
char *argv[])
840 if (rf.
check(
"help"))
842 yInfo() <<
"Options:";
843 yInfo() <<
"\t--name port: service port name (default: /dump)";
844 yInfo() <<
"\t--connect port: name of the port to connect the dumper to at launch time";
845 yInfo() <<
"\t--dir name: provide explicit name of storage directory";
846 yInfo() <<
"\t--overwrite : overwrite pre-existing storage directory";
848 yInfo() <<
"\t--type type: type of the data to be dumped [bottle(default), image, image_jpg, image_png, video, depth, depth_compressed]";
849 yInfo() <<
"\t--addVideo : produce video as well (if image* is selected)";
850 yInfo() <<
"\t--videoType ext: produce video of specified container type [mkv(default), avi]";
852 yInfo() <<
"\t--type type: type of the data to be dumped [bottle(default), image, image_jpg, image_png, depth, depth_compressed]";
854 yInfo() <<
"\t--downsample n: downsample rate (default: 1 => downsample disabled)";
855 yInfo() <<
"\t--rxTime : dump the receiver time instead of the sender time";
856 yInfo() <<
"\t--txTime : dump the sender time straightaway";
862 if (!
yarp.checkNetwork())
864 yError()<<
"YARP server not available!";
869 return mod.runModule(rf);
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 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(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.