26#include <linux/videodev2.h>
29#include <opencv2/opencv.hpp>
34#define PATHPREFIX "/jevois/data/saliencysurf/"
113 itsSaliency = addSubComponent<Saliency>(
"saliency");
114 itsMatcher = addSubComponent<ObjectMatcher>(
"surf");
129 itsRunFut =
jevois::async(std::bind(&SaliencySURF::run,
this));
131 LINFO(
"Using " << itsMatcher->numtrain() <<
" Training Images.");
140 itsBuf.
push(cv::Mat());
143 LINFO(
"Waiting for writer thread to complete, " << itsBuf.
filled_size() <<
" frames to go...");
145 LINFO(
"Writer thread completed. Syncing disk...");
146 if (std::system(
"/bin/sync"))
LERROR(
"Error syncing disk -- IGNORED");
158 inimg.
require(
"input", w,
h, V4L2_PIX_FMT_YUYV);
171 outimg = outframe.get();
179 itsSaliency->process(inimg,
false);
182 int const smlev = itsSaliency->smscale::get();
183 int const smfac = (1 << smlev);
184 int const rwh = rsiz::get();
191 for (
size_t i = 0; i < regions::get(); ++i)
194 int mx, my;
intg32 msal; itsSaliency->getSaliencyMax(mx, my, msal);
197 unsigned int const dmx = (mx << smlev) + (smfac >> 2);
198 unsigned int const dmy = (my << smlev) + (smfac >> 2);
199 int rx = (std::min(
int(w) - rwh/2, std::max(rwh/2,
int(dmx + 1 + (smfac >> 2))))) & (~1);
200 int ry = (std::min(
int(
h) - rwh/2, std::max(rwh/2,
int(dmy + 1 + (smfac >> 2))))) & (~1);
204 cv::Mat roi = grayimg(cv::Rect(rx - rwh/2, ry - rwh/2, rwh, rwh));
207 if (save::get()) itsBuf.
push(roi);
211 double dist = itsMatcher->process(roi, trainidx);
224 itsSaliency->inhibitionOfReturn(mx, my, inhsigma::get() / smfac);
228 std::string
const & fpscpu = timer.
stop();
239 size_t frame = 0;
char tmp[2048];
242 std::string
const cmd =
"/bin/mkdir -p " PATHPREFIX;
243 if (std::system(cmd.c_str()))
LERROR(
"Error running [" << cmd <<
"] -- IGNORED");
248 cv::Mat im = itsBuf.
pop();
251 if (im.empty())
break;
254 std::snprintf(tmp, 2047,
"%s/frame%06zu.png",
PATHPREFIX, frame);
255 cv::imwrite(tmp, im);
258 if ((++frame % 100) == 0)
LINFO(
"Saved " << frame <<
" salient regions.");
262 std::shared_ptr<ObjectMatcher> itsMatcher;
263 std::shared_ptr<Saliency> itsSaliency;
264 std::future<void> itsRunFut;
JEVOIS_REGISTER_MODULE(ArUcoBlob)
Simple salient region detection and identification using keypoint matching.
JEVOIS_DECLARE_PARAMETER(regions, size_t, "Number of salient regions", 2, ParamCateg)
Parameter.
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
void postInit() override
Get started.
JEVOIS_DECLARE_PARAMETER(save, bool, "Save regions when true, useful to create a training set. They will be saved to " PATHPREFIX, false, ParamCateg)
Parameter.
virtual ~SaliencySURF()
Virtual destructor for safe inheritance.
void postUninit() override
Get stopped.
JEVOIS_DECLARE_PARAMETER(rsiz, size_t, "Width and height (pixels) of salient regions", 64, ParamCateg)
Parameter.
SaliencySURF(std::string const &instance)
Constructor.
JEVOIS_DECLARE_PARAMETER(inhsigma, float, "Sigma (pixels) used for inhibition of return", 32.0F, ParamCateg)
Parameter.
size_t filled_size() const
friend friend class Module
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
std::string const & stop(double *seconds)
ENV_INTG32_TYPE intg32
32-bit signed integer
std::string warnAndIgnoreException(std::string const &prefix="")
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
cv::Mat convertToCvGray(RawImage const &src)
void drawFilledRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int col)
void drawRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col)
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)
unsigned short constexpr White
unsigned short constexpr LightGreen