JeVoisBase
1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
|
Share this page: |
|
|
|
|
|
|
Go to the documentation of this file.
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.499
F),
int(itsCorners[1].y + 0.499
F),
283 2, jevois::yuyv::LightGreen);
285 int(itsCorners[2].x + 0.499
F),
int(itsCorners[2].y + 0.499
F), 2,
286 jevois::yuyv::LightGreen);
288 int(itsCorners[3].x + 0.499
F),
int(itsCorners[3].y + 0.499
F), 2,
289 jevois::yuyv::LightGreen);
291 int(itsCorners[0].x + 0.499
F),
int(itsCorners[0].y + 0.499
F), 2,
292 jevois::yuyv::LightGreen);
294 " avg distance " +
std::to_string(itsDist), 3,
h + 1, jevois::yuyv::White);
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;
void sendSerialContour2D(unsigned int camw, unsigned int camh, std::vector< cv::Point_< T > > points, std::string const &id="", std::string const &extra="")
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function with USB output.
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)
std::vector< std::string > split(std::string const &input, std::string const ®ex="\\s+")
virtual void process(jevois::InputFrame &&inframe) override
Processing function with no USB output.
virtual void writeString(std::string const &str)=0
void onParamChange(win const &JEVOIS_UNUSED_PARAM(param), floatpair const &newval) override
Parameter callback.
void removeSubComponent(std::shared_ptr< Comp > &component)
void supportedCommands(std::ostream &os) override
Human-readable description of this Module's supported custom commands.
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.
cv::Mat convertToCvGray(RawImage const &src)
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
std::pair< float, float > floatpair
Define a pair of floats, to avoid macro parsing problems when used as a parameter:
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
std::string system(std::string const &cmd, bool errtoo=true)
ObjectDetect(std::string const &instance)
Constructor.
const std::string & stop(double *seconds)
JEVOIS_REGISTER_MODULE(ObjectDetect)
void drawFilledRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int col)
StdModule(std::string const &instance)
virtual ~ObjectDetect()
Virtual destructor for safe inheritance.
std::string to_string(T const &val)
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
void drawRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col)
std::filesystem::path absolutePath(std::filesystem::path const &path="")
Simple object detection using keypoint matching.
void drawLine(RawImage &img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col)
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(dataroot, std::string, "Root path for data, config, and weight files. " "If empty, use the module's path.", JEVOIS_SHARE_PATH "/darknet/single", ParamCateg)
Parameter.