24 #include <opencv2/imgproc/imgproc.hpp>
41 {
LFATAL(
"Not implemented in this module"); }
45 {
LFATAL(
"Not implemented in this module"); }
49 void jevois::Module::process(InputFrame && JEVOIS_UNUSED_PARAM(inframe), GUIhelper & JEVOIS_UNUSED_PARAM(helper))
50 {
LFATAL(
"Not implemented in this module, and only available on JeVois-Pro"); }
57 if (e ==
nullptr)
LFATAL(
"My parent is not Engine -- CANNOT SEND SERIAL");
64 std::shared_ptr<jevois::UserInterface> JEVOIS_UNUSED_PARAM(s))
65 {
throw std::runtime_error(
"Unsupported command [" + str +
']'); }
69 { os <<
"None" << std::endl; }
88 case jevois::module::SerStamp::None:
91 case jevois::module::SerStamp::Frame:
95 case jevois::module::SerStamp::Time:
97 std::time_t t =
std::time(
nullptr);
char str[100];
98 std::strftime(str,
sizeof(str),
"%T", std::localtime(&t));
99 ret = std::string(str);
103 case jevois::module::SerStamp::FrameTime:
105 std::time_t t =
std::time(
nullptr);
char str[100];
106 std::strftime(str,
sizeof(str),
"%T", std::localtime(&t));
111 case jevois::module::SerStamp::FrameDateTime:
113 std::time_t t =
std::time(
nullptr);
char str[100];
114 std::strftime(str,
sizeof(str),
"%F/%T", std::localtime(&t));
120 if (ret.empty() ==
false) ret +=
' ';
126 std::string
const & extra)
135 sendSerialStd1Dx(x, size,
id, extra);
142 std::ostringstream oss; oss << std::fixed << std::setprecision(
serprec::get());
150 case jevois::module::SerStyle::Terse:
154 case jevois::module::SerStyle::Normal:
157 oss << x <<
' ' << size;
160 case jevois::module::SerStyle::Detail:
161 case jevois::module::SerStyle::Fine:
164 oss << x - 0.5F * size <<
' ' << x + 0.5F * size;
165 if (extra.empty() ==
false) oss <<
' ' << extra;
170 sendSerial(oss.str());
175 std::string
const & extra)
183 sendSerialStd1Dy(y, size,
id, extra);
190 std::ostringstream oss; oss << std::fixed << std::setprecision(
serprec::get());
198 case jevois::module::SerStyle::Terse:
202 case jevois::module::SerStyle::Normal:
205 oss << y <<
' ' << size;
208 case jevois::module::SerStyle::Detail:
209 case jevois::module::SerStyle::Fine:
212 oss << y - 0.5F * size <<
' ' << y + 0.5F * size;
213 if (extra.empty() ==
false) oss <<
' ' << extra;
218 sendSerial(oss.str());
223 std::string
const &
id, std::string
const & extra)
232 sendSerialStd2D(x, y, w,
h,
id, extra);
236 std::string
const & extra)
239 std::ostringstream oss; oss << std::fixed << std::setprecision(
serprec::get());
247 case jevois::module::SerStyle::Terse:
248 oss <<
"T2 " << x <<
' ' << y;
251 case jevois::module::SerStyle::Normal:
254 oss << x <<
' ' << y <<
' ' << w <<
' ' <<
h;
257 case jevois::module::SerStyle::Detail:
260 oss << x - 0.5F * w <<
' ' << y - 0.5F *
h <<
' ';
261 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h <<
' ';
262 oss << x + 0.5F * w <<
' ' << y + 0.5F *
h <<
' ';
263 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h;
264 if (extra.empty() ==
false) oss <<
' ' << extra;
267 case jevois::module::SerStyle::Fine:
271 oss << x - 0.5F * w <<
' ' << y - 0.5F *
h <<
' ';
272 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h <<
' ';
273 oss << x + 0.5F * w <<
' ' << y + 0.5F *
h <<
' ';
274 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h;
275 if (extra.empty() ==
false) oss <<
' ' << extra;
280 sendSerial(oss.str());
284 template <
typename T>
286 std::string
const &
id, std::string
const & extra)
291 case jevois::module::SerStyle::Terse:
294 float cx = 0.0F, cy = 0.0F;
295 for (cv::Point
const & p : points) { cx += p.x; cy += p.y; }
296 if (points.size()) { cx /= points.size(); cy /= points.size(); }
297 sendSerialImg2D(camw, camh, cx, cy, 0.0
F, 0.0
F,
id, extra);
301 case jevois::module::SerStyle::Normal:
304 cv::Rect r = cv::boundingRect(points);
305 sendSerialImg2D(camw, camh, r.x + 0.5F * r.width, r.y + 0.5F * r.height, r.width, r.height,
id, extra);
309 case jevois::module::SerStyle::Detail:
312 cv::RotatedRect r = cv::minAreaRect(points);
315 unsigned int const prec =
serprec::get();
float const eps = std::pow(10.0
F, -
float(prec));
316 std::ostringstream oss; oss << std::fixed << std::setprecision(prec);
324 cv::Point2f corners[4];
329 for (
int i = 0; i < 4; ++i)
331 float x = corners[i].x, y = corners[i].y;
333 oss <<
' ' << x <<
' ' << y;
335 if (extra.empty() ==
false) oss <<
' ' << extra;
338 sendSerial(oss.str());
342 case jevois::module::SerStyle::Fine:
345 unsigned int const prec =
serprec::get();
float const eps = std::pow(10.0
F, -
float(prec));
346 std::ostringstream oss; oss << std::fixed << std::setprecision(prec);
354 oss << points.size();
356 for (cv::Point
const & p : points)
358 float x = p.x, y = p.y;
360 oss <<
' ' << x <<
' ' << y;
362 if (extra.empty() ==
false) oss <<
' ' << extra;
365 sendSerial(oss.str());
376 std::string
const &
id, std::string
const & extra);
379 std::string
const &
id, std::string
const & extra);
382 std::string
const &
id, std::string
const & extra);
387 float q1,
float q2,
float q3,
float q4,
388 std::string
const &
id, std::string
const & extra)
391 std::ostringstream oss; oss << std::fixed << std::setprecision(
serprec::get());
399 case jevois::module::SerStyle::Terse:
400 oss <<
"T3 " << x <<
' ' << y <<
' ' << z;
403 case jevois::module::SerStyle::Normal:
406 oss << x <<
' ' << y <<
' ' << z <<
' ' << w <<
' ' <<
h <<
' ' << d;
409 case jevois::module::SerStyle::Detail:
412 oss << x <<
' ' << y <<
' ' << z <<
' ' << w <<
' ' <<
h <<
' ' << d <<
' '
413 << q1 <<
' ' << q2 <<
' ' << q3 <<
' ' << q4;
414 if (extra.empty() ==
false) oss <<
' ' << extra;
417 case jevois::module::SerStyle::Fine:
421 oss << x - 0.5F * w <<
' ' << y - 0.5F *
h <<
' ' << z - 0.5F * d <<
' ';
422 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h <<
' ' << z - 0.5F * d <<
' ';
423 oss << x + 0.5F * w <<
' ' << y + 0.5F *
h <<
' ' << z - 0.5F * d <<
' ';
424 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h <<
' ' << z - 0.5F * d <<
' ';
425 oss << x - 0.5F * w <<
' ' << y - 0.5F *
h <<
' ' << z + 0.5F * d <<
' ';
426 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h <<
' ' << z + 0.5F * d <<
' ';
427 oss << x + 0.5F * w <<
' ' << y + 0.5F *
h <<
' ' << z + 0.5F * d <<
' ';
428 oss << x + 0.5F * w <<
' ' << y - 0.5F *
h <<
' ' << z + 0.5F * d <<
' ';
429 if (extra.empty() ==
false) oss <<
' ' << extra;
434 sendSerial(oss.str());
439 std::string
const & extra)
444 case jevois::module::SerStyle::Terse:
447 cv::Point3f cg(0.0
F, 0.0
F, 0.0
F);
448 for (cv::Point3f
const & p : points) cg += p;
449 if (points.size()) { cg.x /= points.size(); cg.y /= points.size(); cg.z /= points.size(); }
450 sendSerialStd3D(cg.x, cg.y, cg.z, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F,
id, extra);
454 case jevois::module::SerStyle::Normal:
457 cv::Point3f cg(0.0
F, 0.0
F, 0.0
F), pmin(1e30F, 1e30F, 1e30F), pmax(-1e30F, -1e30F, -1e30F);
458 for (cv::Point3f
const & p : points)
461 if (p.x < pmin.x) pmin.x = p.x;
462 if (p.y < pmin.y) pmin.y = p.y;
463 if (p.z < pmin.z) pmin.z = p.z;
464 if (p.x > pmax.x) pmax.x = p.x;
465 if (p.y > pmax.y) pmax.y = p.y;
466 if (p.z > pmax.z) pmax.z = p.z;
468 if (points.size()) { cg.x /= points.size(); cg.y /= points.size(); cg.z /= points.size(); }
469 sendSerialStd3D(cg.x, cg.y, cg.z, pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z, 0.0F, 0.0F, 0.0F, 0.0F,
474 case jevois::module::SerStyle::Detail:
477 cv::Point3f cg(0.0
F, 0.0
F, 0.0
F), pmin(1e30F, 1e30F, 1e30F), pmax(-1e30F, -1e30F, -1e30F);
478 for (cv::Point3f
const & p : points)
481 if (p.x < pmin.x) pmin.x = p.x;
482 if (p.y < pmin.y) pmin.y = p.y;
483 if (p.z < pmin.z) pmin.z = p.z;
484 if (p.x > pmax.x) pmax.x = p.x;
485 if (p.y > pmax.y) pmax.y = p.y;
486 if (p.z > pmax.z) pmax.z = p.z;
488 if (points.size()) { cg.x /= points.size(); cg.y /= points.size(); cg.z /= points.size(); }
490 sendSerialStd3D(cg.x, cg.y, cg.z, pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z, 0.0F, 0.0F, 0.0F, 1.0F,
495 case jevois::module::SerStyle::Fine:
499 std::ostringstream oss; oss << std::fixed << std::setprecision(prec);
507 oss << points.size();
509 for (cv::Point3f
const & p : points) oss <<
' ' << p.x <<
' ' << p.y <<
' ' << p.z;
510 if (extra.empty() ==
false) oss <<
' ' << extra;
513 sendSerial(oss.str());
523 if (m == jevois::module::SerMark::None || m == jevois::module::SerMark::Stop)
return;
524 sendSerial(getStamp() +
"MARK START");
531 if (m == jevois::module::SerMark::None || m == jevois::module::SerMark::Start)
return;
532 sendSerial(getStamp() +
"MARK STOP");
538 if (res.empty())
return;
541 std::ostringstream oss; oss << std::fixed << std::setprecision(
serprec::get());
549 case jevois::module::SerStyle::Terse:
553 case jevois::module::SerStyle::Normal:
557 case jevois::module::SerStyle::Detail:
562 case jevois::module::SerStyle::Fine:
569 sendSerial(oss.str());
574 std::vector<ObjReco>
const & res)
576 if (res.empty())
return;
578 std::string best, extra; std::string * ptr = &best;
581 for (
auto const & r : res)
585 case jevois::module::SerStyle::Terse:
592 if (ptr == &extra) (*ptr) +=
' ';
597 if (extra.empty() ==
false) extra = extra.substr(0, extra.length() - 1);
599 sendSerialImg2D(camw, camh, x, y, w,
h, best, extra);