24#include <linux/videodev2.h>
26#include <opencv2/imgcodecs.hpp>
39 "Width and height (in percent of image size, with valid percentages between "
40 "10.0 and 100.0) of the window used to interactively save objects",
170 public jevois::Parameter<win, showwin>
177 { itsMatcher = addSubComponent<ObjectMatcher>(
"surf"); }
190 if (newval.first < 10.0F || newval.first > 100.0F || newval.second < 10.0F || newval.second > 100.0F)
191 throw std::range_error(
"Invalid window percentage values, must be between 10.0 and 100.0");
202 itsGrayImg = inframe.getCvGRAY();
207 itsDist = itsMatcher->process(itsGrayImg, itsTrainIdx, itsCorners);
210 if (itsDist < 100.0 && itsCorners.size() == 4)
211 sendSerialContour2D(itsGrayImg.cols, itsGrayImg.rows, itsCorners, itsMatcher->traindata(itsTrainIdx).name);
226 inimg.
require(
"input", w,
h, V4L2_PIX_FMT_YUYV);
233 outimg = outframe.get();
245 if (itsKPfut.valid())
248 if (itsKPfut.wait_for(std::chrono::milliseconds(2)) == std::future_status::ready)
254 itsDist = itsMatcher->match(itsKeypoints, itsDescriptors, itsTrainIdx, itsCorners);
266 itsMatcher->detect(itsGrayImg, itsKeypoints);
267 itsMatcher->compute(itsGrayImg, itsKeypoints, itsDescriptors);
279 if (itsDist < 100.0 && itsCorners.size() == 4)
282 int(itsCorners[1].x + 0.499F),
int(itsCorners[1].y + 0.499F),
285 int(itsCorners[2].x + 0.499F),
int(itsCorners[2].y + 0.499F), 2,
288 int(itsCorners[3].x + 0.499F),
int(itsCorners[3].y + 0.499F), 2,
291 int(itsCorners[0].x + 0.499F),
int(itsCorners[0].y + 0.499F), 2,
303 int const ww = (wi.first * 0.01F) * w, wh = (wi.second * 0.01F) *
h;
308 std::string
const & fpscpu = timer.
stop();
318 void parseSerial(std::string
const & str, std::shared_ptr<jevois::UserInterface> s)
override
321 if (tok.empty())
throw std::runtime_error(
"Unsupported empty module command");
322 std::string
const dirname =
absolutePath(itsMatcher->traindir::get());
324 if (tok[0] ==
"save")
326 if (tok.size() == 1)
throw std::runtime_error(
"save command requires one <name> argument");
330 int ww = (wi.first * 0.01F) * itsGrayImg.cols;
331 int wh = (wi.second * 0.01F) * itsGrayImg.rows;
332 cv::Rect cr( (itsGrayImg.cols - ww) / 2, (itsGrayImg.rows - wh) / 2, ww, wh);
335 cv::imwrite(dirname +
'/' + tok[1] +
".png", itsGrayImg(cr));
336 s->writeString(tok[1] +
".png saved and trained.");
338 else if (tok[0] ==
"del")
340 if (tok.size() == 1)
throw std::runtime_error(
"del command requires one <name> argument");
341 if (std::remove((dirname +
'/' + tok[1] +
".png").c_str()))
342 throw std::runtime_error(
"Failed to delete " + tok[1] +
".png");
343 s->writeString(tok[1] +
".png deleted and forgotten.");
345 else if (tok[0] ==
"list")
349 for (std::string
const & f : files) s->writeString(f);
352 else throw std::runtime_error(
"Unsupported module command [" + str +
']');
356 try {
if (itsKPfut.valid()) itsKPfut.get(); }
catch (...) { }
365 itsKeypoints.clear(); itsDescriptors = cv::Mat(); itsDist = 1.0e30; itsCorners.clear();
368 itsMatcher = addSubComponent<ObjectMatcher>(
"surf");
376 os <<
"list - show current list of training images" << std::endl;
377 os <<
"save <somename> - grab current frame and save as new training image <somename>.png" << std::endl;
378 os <<
"del <somename> - delete training image <somename>.png" << std::endl;
382 std::shared_ptr<ObjectMatcher> itsMatcher;
383 std::future<void> itsKPfut;
385 std::vector<cv::KeyPoint> itsKeypoints;
386 cv::Mat itsDescriptors;
389 std::vector<cv::Point2f> itsCorners;
JEVOIS_REGISTER_MODULE(ArUcoBlob)
#define JEVOIS_UNUSED_PARAM(x)
std::pair< float, float > floatpair
Define a pair of floats, to avoid macro parsing problems when used as a parameter:
Simple object detection using keypoint matching.
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function with USB output.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(win, floatpair, "Width and height (in percent of image size, with valid percentages between " "10.0 and 100.0) of the window used to interactively save objects", floatpair(50.0F, 50.0F), ParamCateg)
Parameter.
ObjectDetect(std::string const &instance)
Constructor.
virtual ~ObjectDetect()
Virtual destructor for safe inheritance.
void supportedCommands(std::ostream &os) override
Human-readable description of this Module's supported custom commands.
virtual void process(jevois::InputFrame &&inframe) override
Processing function with no USB output.
void onParamChange(win const &JEVOIS_UNUSED_PARAM(param), floatpair const &newval) override
Parameter callback.
JEVOIS_DECLARE_PARAMETER(showwin, bool, "Show the interactive image capture window when true", false, ParamCateg)
Parameter.
void parseSerial(std::string const &str, std::shared_ptr< jevois::UserInterface > s) override
Receive a string from a serial port which contains a user command.
std::filesystem::path absolutePath(std::filesystem::path const &path="")
void removeSubComponent(std::shared_ptr< Comp > &component)
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
StdModule(std::string const &instance)
void sendSerialContour2D(unsigned int camw, unsigned int camh, std::vector< cv::Point_< T > > points, std::string const &id="", std::string const &extra="")
std::string const & stop(double *seconds)
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 drawLine(RawImage &img, int x1, int y1, int x2, int y2, unsigned int thick, 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)
std::string system(std::string const &cmd, bool errtoo=true)
std::vector< std::string > split(std::string const &input, std::string const ®ex="\\s+")
unsigned short constexpr White
unsigned short constexpr MedGrey
unsigned short constexpr LightGreen