22#include <opencv2/videoio.hpp>
23#include <opencv2/imgproc.hpp>
24#include <linux/videodev2.h>
30#define PATHPREFIX "/jevois/data/surpriserecorder/"
34 PATHPREFIX " will be prepended to it. Name should contain a printf-like directive for "
35 "one int argument, which will start at 0 and be incremented on each streamoff command.",
36 "video%06d.avi", ParamCateg);
40 "as to which codecs are supported. Presumably, the ffmpeg library is used inside OpenCV. "
41 "Hence any video encoder supported by ffmpeg should work. Tested codecs include: MJPG, "
42 "MP4V, AVC1. Make sure you also pick the right filename extension (e.g., .avi for MJPG, "
43 ".mp4 for MP4V, etc)",
44 "MJPG", boost::regex(
"^\\w{4}$"), ParamCateg);
48 "playback. Beware that the video writer will drop frames if you are capturing faster than "
49 "the frame rate specified here. For example, if capturing at 120fps, be sure to set this "
50 "parameter to 120, otherwise by default the saved video will be at 30fps even though capture "
51 "was running at 120fps.",
60 "each surprising event.",
125 public jevois::Parameter<filename, fourcc, fps, thresh, ctxframes>
134 {
itsSurprise = addSubComponent<Surprise>(
"surprise"); }
167 LINFO(
"Writer thread completed. Syncing disk...");
168 if (std::system(
"/bin/sync"))
LERROR(
"Error syncing disk -- IGNORED");
181 inimg.
require(
"input", w,
h, V4L2_PIX_FMT_YUYV);
186 std::future<double> itsSurpFut =
214 cv::rectangle(cvimg, cv::Point(3, 3), cv::Point(w-4,
h-4), cv::Scalar(0,0,255), 7);
262 cv::VideoWriter writer;
271 if (im.empty())
break;
274 if (writer.isOpened() ==
false)
277 std::string
const fcc = fourcc::get();
278 int const cvfcc = cv::VideoWriter::fourcc(fcc[0], fcc[1], fcc[2], fcc[3]);
281 std::string fn = filename::get();
282 if (fn.empty())
LFATAL(
"Cannot save to an empty filename");
286 std::string
const cmd =
"/bin/mkdir -p " + fn.substr(0, fn.rfind(
'/'));
287 if (std::system(cmd.c_str()))
LERROR(
"Error running [" << cmd <<
"] -- IGNORED");
293 std::snprintf(tmp, 2047, fn.c_str(),
itsFileNum);
294 std::ifstream ifs(tmp);
295 if (ifs.is_open() ==
false) {
itsFilename = tmp;
break; }
300 if (writer.open(
itsFilename, cvfcc, fps::get(), im.size(),
true) ==
false)
310 if ((++frame % 100) == 0)
sendSerial(
"SAVEDNUM " + std::to_string(frame));
JEVOIS_REGISTER_MODULE(ArUcoBlob)
Surprise-based recording of events.
std::deque< cv::Mat > itsCtxBuf
Buffer for context frames before event start.
virtual ~SurpriseRecorder()
Virtual destructor for safe inheritance.
std::shared_ptr< Surprise > itsSurprise
JEVOIS_DECLARE_PARAMETER(filename, std::string, "Name of the video file to write. If path is not absolute, " PATHPREFIX " will be prepended to it. Name should contain a printf-like directive for " "one int argument, which will start at 0 and be incremented on each streamoff command.", "video%06d.avi", ParamCateg)
Parameter.
std::future< void > itsRunFut
Future for our run() thread.
int itsFileNum
Video file number.
void postInit() override
Get started.
JEVOIS_DECLARE_PARAMETER(fourcc, std::string, "FourCC of the codec to use. The OpenCV VideoWriter doc is unclear " "as to which codecs are supported. Presumably, the ffmpeg library is used inside OpenCV. " "Hence any video encoder supported by ffmpeg should work. Tested codecs include: MJPG, " "MP4V, AVC1. Make sure you also pick the right filename extension (e.g., .avi for MJPG, " ".mp4 for MP4V, etc)", "MJPG", boost::regex("^\\w{4}$"), ParamCateg)
Parameter.
void process(jevois::InputFrame &&inframe) override
Processing function, version with no video output.
void run()
Video writer thread.
SurpriseRecorder(std::string const &instance)
Constructor.
std::atomic< bool > itsRunning
Flag to let run thread when to quit.
std::string itsFilename
Current video file name.
JEVOIS_DECLARE_PARAMETER(ctxframes, unsigned int, "Number of context video frames recorded before and after " "each surprising event.", 150, ParamCateg)
Parameter.
jevois::BoundedBuffer< cv::Mat, jevois::BlockingBehavior::Block, jevois::BlockingBehavior::Block > itsBuf
Buffer for frames to save.
void postUninit() override
Get stopped.
JEVOIS_DECLARE_PARAMETER(thresh, double, "Surprise threshold. Lower values will record more events.", 1.0e7, ParamCateg)
Parameter.
int itsToSave
Number of context frames after end of event that remain to be saved.
JEVOIS_DECLARE_PARAMETER(fps, double, "Video frames/sec as stored in the file and to be used both for recording and " "playback. Beware that the video writer will drop frames if you are capturing faster than " "the frame rate specified here. For example, if capturing at 120fps, be sure to set this " "parameter to 120, otherwise by default the saved video will be at 30fps even though capture " "was running at 120fps.", 15.0, ParamCateg)
Parameter.
size_t filled_size() const
friend friend class Module
virtual void sendSerial(std::string const &str)
void checkpoint(char const *description)
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
std::string warnAndIgnoreException(std::string const &prefix="")
cv::Mat convertToCvBGR(RawImage const &src)
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)