在人脸识别到以后,需要在实时视频上将所有人脸框绘制出来,一把来说识别人脸会有多种选择,一个是识别最大人脸,这种场景主要用于刷脸门禁,还有一种是识别所有人脸,这种场景主要用于人脸识别摄像机,就是将画面中的所有人脸识别出来发给服务器,人脸框的数据主要是四个参数,左上角和右下角的位置,也可以说是x、y、width、height,可能有些做的比较好的还有倾斜角度,这个意义不是很大,人脸识别的速度一般都是飞快的,就算你用学习上用的opencv做识别也是非常快的,基本上都是毫秒级的响应,主要的耗时操作在特征值的提取,所以一般要求能够响应每个通道每秒钟25帧-30帧的画面绘制+人脸框的绘制,当然人脸框的数据可能会有多个。
用Qt来绘制人脸框,核心就是一个函数,调用QPainter的drawRect方法,传入区域即可,如果花哨点的话还可以设置边框的粗细和颜色、圆角角度等,注意圆角角度使用的是drawRoundedRect而不是drawRoundRect,很多人这里会搞错哦。近期接触的项目对人脸框的要求越来越多,之前是让用户自己拿到图片来绘制,近期索性直接将这个功能内置到视频控件中(视频控件封装了多种内核版本,有ffmpeg、vlc、mpv、海康sdk等),提供了可设置边框粗细、颜色,传入人脸框区域集合的接口,用户只要自己的算法分析拿到人脸的区域集合(用户是上帝,用户的需求就是我的需求),通过setFaceRects函数设置即可,如果要清空人脸,只要设置人脸框区域集合为空即可。总体测试下来速度非常快,可以忽略,采用的QOPenGLWidget绘制的实时图像,也支持人脸框的绘制。
bool FFmpegWidget::eventFilter(QObject *watched, QEvent *event)
{
if (watched == osdWidget && event->type() == QEvent::Paint) {
if (drawImage) {
QPainter painter;
painter.begin(osdWidget);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
//绘制边框
drawBorder(&painter);
if (thread->getIsInit()) {
//绘制背景图片
drawImg(&painter, image);
//绘制人脸框
drawFace(&painter);
//绘制标签
drawOSD(&painter, osd1Visible, osd1FontSize, osd1Text, osd1Color, osd1Image, osd1Format, osd1Position);
drawOSD(&painter, osd2Visible, osd2FontSize, osd2Text, osd2Color, osd2Image, osd2Format, osd2Position);
} else {
//绘制背景
if (!isDrag) {
drawBg(&painter);
}
}
painter.end();
}
}
return QWidget::eventFilter(watched, event);
}
void FFmpegWidget::drawBorder(QPainter *painter)
{
if (borderWidth == 0) {
return;
}
painter->save();
QPen pen;
pen.setWidth(borderWidth);
pen.setColor(hasFocus() ? focusColor : borderColor);
painter->setPen(pen);
painter->drawRect(rect());
painter->restore();
}
void FFmpegWidget::drawBg(QPainter *painter)
{
painter->save();
//背景图片为空则绘制文字,否则绘制背景图片
if (bgImage.isNull()) {
painter->setFont(this->font());
painter->setPen(palette().foreground().color());
painter->drawText(rect(), Qt::AlignCenter, bgText);
} else {
//居中绘制
int x = rect().center().x() - bgImage.width() / 2;
int y = rect().center().y() - bgImage.height() / 2;
QPoint point(x, y);
painter->drawImage(point, bgImage);
}
painter->restore();
}
void FFmpegWidget::drawImg(QPainter *painter, QImage img)
{
if (img.isNull()) {
return;
}
painter->save();
int offset = borderWidth * 1 + 0;
img = img.scaled(width() - offset, height() - offset, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (fillImage) {
QRect rect(offset / 2, offset / 2, width() - offset, height() - offset);
painter->drawImage(rect, img);
} else {
//按照比例自动居中绘制
int x = rect().center().x() - img.width() / 2;
int y = rect().center().y() - img.height() / 2;
QPoint point(x, y);
painter->drawImage(point, img);
}
painter->restore();
}
void FFmpegWidget::drawFace(QPainter *painter)
{
if (faceRects.count() == 0) {
return;
}
painter->save();
//人脸边框的颜色
QPen pen;
pen.setWidth(faceBorder);
pen.setColor(faceColor);
painter->setPen(pen);
//逐个取出人脸框区域进行绘制
foreach (QRect rect, faceRects) {
painter->drawRect(rect);
}
painter->restore();
}
void FFmpegWidget::drawOSD(QPainter *painter,
bool osdVisible,
int osdFontSize,
const QString &osdText,
const QColor &osdColor,
const QImage &osdImage,
const FFmpegWidget::OSDFormat &osdFormat,
const FFmpegWidget::OSDPosition &osdPosition)
{
if (!osdVisible) {
return;
}
painter->save();
//标签位置尽量偏移多一点避免遮挡
QRect osdRect(rect().x() + (borderWidth * 2), rect().y() + (borderWidth * 2), width() - (borderWidth * 5), height() - (borderWidth * 5));
int flag = Qt::AlignLeft | Qt::AlignTop;
QPoint point = QPoint(osdRect.x(), osdRect.y());
if (osdPosition == OSDPosition_Left_Top) {
flag = Qt::AlignLeft | Qt::AlignTop;
point = QPoint(osdRect.x(), osdRect.y());
} else if (osdPosition == OSDPosition_Left_Bottom) {
flag = Qt::AlignLeft | Qt::AlignBottom;
point = QPoint(osdRect.x(), osdRect.height() - osdImage.height());
} else if (osdPosition == OSDPosition_Right_Top) {
flag = Qt::AlignRight | Qt::AlignTop;
point = QPoint(osdRect.width() - osdImage.width(), osdRect.y());
} else if (osdPosition == OSDPosition_Right_Bottom) {
flag = Qt::AlignRight | Qt::AlignBottom;
point = QPoint(osdRect.width() - osdImage.width(), osdRect.height() - osdImage.height());
}
if (osdFormat == OSDFormat_Image) {
painter->drawImage(point, osdImage);
} else {
QDateTime now = QDateTime::currentDateTime();
QString text = osdText;
if (osdFormat == OSDFormat_Date) {
text = now.toString("yyyy-MM-dd");
} else if (osdFormat == OSDFormat_Time) {
text = now.toString("HH:mm:ss");
} else if (osdFormat == OSDFormat_DateTime) {
text = now.toString("yyyy-MM-dd HH:mm:ss");
}
//设置颜色及字号
QFont font;
font.setPixelSize(osdFontSize);
painter->setPen(osdColor);
painter->setFont(font);
painter->drawText(osdRect, flag, text);
}
painter->restore();
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。