我们的测试程序是基于以下代码修改的:https://github.com/LingDong-/fast-many-face-detection-with-cpp-or-openframeworks-on-mac-using-neural-networks。我们没有使用OpenFramworks框架,只使用OpenCV的代码,比较简单。主程序如下(其他所需要的文件请从github上下载):
#include
#include<time.h>
#include
#include
#include
#include"caffe_face_det.h"
using namespacestd;
#defineMY_TIMEV struct timeval
MY_TIMEVclockGetTime(clockid_t _tclock_id)
{
struct timespec stTime;
//该函数的时间精确到纳秒级别
clock_gettime(_tclock_id, &stTime);
MY_TIMEV stRet;
stRet.tv_sec = stTime.tv_sec;
stRet.tv_usec = stTime.tv_nsec/1000;
return stRet;
}
int main(intargc, char*argv[]) {
if(argc <= 1)
{
cout << "no file."<< endl;
}
MY_TIMEV tmp = {0};
int timeOffset = 0;
int timeOffset1 = 0;
caffe_face_det detector;
detector.setup();
cv::Mat frame;
frame = cv::imread(argv[1]);
cout << "hello there."<< endl;
tmp = clockGetTime(CLOCK_MONOTONIC);
//单位ms
timeOffset = tmp.tv_sec*1000 +tmp.tv_usec/1000;
vector detections =detector.detect(frame);
memset(&tmp,0,sizeof(tmp));
tmp = clockGetTime(CLOCK_MONOTONIC);
timeOffset1 = tmp.tv_sec*1000 +tmp.tv_usec/1000;
for (int i = 0; i < detections.size();i++){
a_det det = detections;
cv::rectangle(frame,cv::Point(det.left,det.top), cv::Point(det.right,det.bottom),cv::Scalar(0,255,255),3);
}
cv:imwrite("result.jpg", frame);
cout << "Time: " <<(timeOffset1 - timeOffset) << "ms" << endl;
return 0;
}
使用如下命令进行编译:
aarch64-buildroot-linux-gnu-g++ -o face-detectmain.cpp--sysroot=/home/forlinx/3568/OK3568-linux-source/buildroot/output/OK3568/host/aarch64-buildroot-linux-gnu/sysroot/-lopencv_imgproc -lopencv_dnn -lopencv_core -lopencv_imgcodecs -lopencv_highgui-std=gnu++11
将生成的face-detect文件和需要的模型文件拷贝到机器上就可以了,没有其他需要的依赖文件。
第二幅图片是一个调皮的女孩,OpenCV除了正确地识别了女孩的脸,还把其肩膀误认为是脸。

第三幅图片是阿根廷国家队在2021年美洲杯夺冠的照片,由于图片尺寸不大,人物多且杂乱,OpenCV的识别效果很差。

第四幅图片是2022年世界杯阿根廷夺冠后的庆祝照片,相当于上一幅图片,由于人脸基本都是正面照,除了被遮挡的人脸都正确识别出来了,劳塔罗的手被误识别为人脸了,但整体效果不错。

时间测试方面,我们只计算人脸检测的时间,处理一副图片是620ms左右,基本可以满足像门禁这样的应用的需要。
我要赚赏金
