162 static cv::Mat itsLastFace(60, 60, CV_8UC2, 0x80aa) ;
163 static cv::Mat itsLastObject(60, 60, CV_8UC2, 0x80aa) ;
164 static std::string itsLastObjectCateg;
165 static bool doobject =
false;
166 static bool intromode =
false;
167 static bool intromoviedone =
false;
168 static ScriptItem const * scriptitem = &TheScript[0];
169 static int scriptframe = 0;
176 inimg.
require(
"input", 320, 240, V4L2_PIX_FMT_YUYV);
178 itsProcessingTimer.
start();
179 int const roihw = 32;
186 outimg = outframe.get();
187 outimg.
require(
"output", 640, outimg.
height, V4L2_PIX_FMT_YUYV);
192 case 480: intromode =
true;
break;
193 default:
LFATAL(
"Incorrect output height: should be 312, 360 or 480");
197 if (intromode && intromoviedone ==
false)
201 if (m.empty()) intromoviedone =
true;
209 else if (outimg.
height > 360)
228 if (intromode && intromoviedone ==
false) { outframe.send();
return; }
236 int const smadj = smlev > 0 ? (1 << (smlev-1)) : 0;
237 int const dmx = (mx << smlev) + smadj;
238 int const dmy = (my << smlev) + smadj;
241 int const rx = std::min(
int(inimg.
width) - roihw, std::max(roihw, dmx));
242 int const ry = std::min(
int(inimg.
height) - roihw, std::max(roihw, dmy));
287 else if (outimg.
height > 312)
292 float kfxraw, kfyraw, kfximg, kfyimg;
293 itsKF->get(kfxraw, kfyraw, kfximg, kfyimg, inimg.
width, inimg.
height, 1.0F, 1.0F);
302 if (intromode && intromoviedone)
306 if (scriptframe < 32) lum = scriptframe * 8;
307 else if (scriptframe > 4*30 - 32) lum = std::max(0, (4*30 - scriptframe) * 8);
310 int x = (640 - 10 * strlen(scriptitem->
msg)) / 2;
316 int phase = scriptframe / 10;
322 if (++scriptframe >= 140)
324 scriptframe = 0; ++scriptitem;
325 if (scriptitem->
msg ==
nullptr) scriptitem = &TheScript[0];
332 cv::Mat rawroi = rawimgcv(cv::Rect(rx - roihw, ry - roihw, roihw * 2, roihw * 2));
341 switch (objsz.depth_)
348 cv::cvtColor(rawroi, objroi, cv::COLOR_YUV2GRAY_YUYV);
351 size_t const elem = (objroi.cols * objroi.rows * 10) / 100;
352 std::vector<unsigned char> v; v.assign(objroi.datastart, objroi.dataend);
353 std::nth_element(v.begin(), v.begin() + elem, v.end());
354 unsigned char const thresh = std::min((
unsigned char)(100), std::max((
unsigned char)(30), v[elem]));
357 cv::threshold(objroi, objroi, thresh, 255, cv::THRESH_BINARY_INV);
360 cv::Mat pts; cv::findNonZero(objroi, pts);
361 cv::Rect r = cv::boundingRect(pts);
362 int const cx = r.x + r.width / 2;
363 int const cy = r.y + r.height / 2;
364 int const siz = std::min(roihw * 2, std::max(16, 8 + std::max(r.width, r.height)));
365 int const tlx = std::max(0, std::min(roihw*2 - siz, cx - siz/2));
366 int const tly = std::max(0, std::min(roihw*2 - siz, cy - siz/2));
367 cv::Rect ar(tlx, tly, siz, siz);
368 cv::resize(objroi(ar), objroi, cv::Size(objsz.width_, objsz.height_), 0, 0, cv::INTER_AREA);
374 cv::cvtColor(rawroi, objroi, cv::COLOR_YUV2RGB_YUYV);
375 cv::resize(objroi, objroi, cv::Size(objsz.width_, objsz.height_), 0, 0, cv::INTER_AREA);
379 LFATAL(
"Unsupported object detection input depth " << objsz.depth_);
386 std::ostringstream oss;
387 for (
size_t i = 0; i < scores.size(); ++i)
388 oss <<
itsObjectRecognition->category(i) <<
':' << std::fixed << std::setprecision(2) << scores[i] <<
' ';
392 float best1 = scores[0], best2 = scores[0];
size_t idx1 = 0, idx2 = 0;
393 for (
size_t i = 1; i < scores.size(); ++i)
395 if (scores[i] > best1) { best2 = best1; idx2 = idx1; best1 = scores[i]; idx1 = i; }
396 else if (scores[i] > best2) { best2 = scores[i]; idx2 = i; }
400 if (best1 > 90.0F && best2 < 20.0F)
404 itsLastObject = rawimgcv(cv::Rect(rx - 30, ry - 30, 60, 60)).clone();
406 LINFO(
"Object recognition: best: " << itsLastObjectCateg <<
" (" << best1 <<
415 cv::Mat grayroi; cv::cvtColor(rawroi, grayroi, cv::COLOR_YUV2GRAY_YUYV);
416 cv::equalizeHist(grayroi, grayroi);
419 std::vector<cv::Rect> faces; std::vector<std::vector<cv::Rect> > eyes;
425 LINFO(
"detected " << faces.size() <<
" faces");
427 itsLastFace = rawimgcv(cv::Rect(rx - 30, ry - 30, 60, 60)).clone();
430 for (
size_t i = 0; i < faces.size(); ++i)
433 cv::Rect
const & f = faces[i];
437 for (
auto const & e : eyes[i])
446 cv::Mat outimgcv(outimg.
height, outimg.
width, CV_8UC2, outimg.
buf->data());
447 itsLastObject.copyTo(outimgcv(cv::Rect(520, 240, 60, 60)));
448 itsLastFace.copyTo(outimgcv(cv::Rect(580, 240, 60, 60)));
472 doobject = ! doobject;