飞腾派
直播中

jf_98561620

未满1年用户 13经验值
私信 关注
[经验]

【飞腾派4G版免费试用】飞腾派SeetafaceEngine人脸对齐(PART2)

继续玩飞腾派。。。
接着上一篇【飞腾派4G版免费试用】飞腾派SeetafaceEngine人脸检测 继续补充。

FaceAlignment人脸对齐是标出面部眼、鼻、嘴等,需要用到FaceDetection和FaceAlignment的so库函数。

1、飞腾派Ubuntu系统下编译FaceDetection人脸检测库

$cd XXX/SeetaFaceEngine-master/FaceDetection
$ mkdir build
$ cd build
$ cmake -D CUDA_USE_STATIC_CUDA_RUNTIME=OFF ..
$ make -j4

编码报错 error: unrecognized command-line
option ‘-msse4.1’,打开CMakeLists.txt删除USE_SSE相关定义即可。
编译成功后,得到人脸检测demofacedet_test,人脸检测用户态库libseeta_facedet_lib.so和face_detection.h头文件。

2、飞腾派Ubuntu系统下编译FaceAlignment人脸对齐库

$cd /SeetaFaceEngine-master/FaceAlignment
$ mkdir build
$ cd build
//拷贝face_detection.h到FaceAlignment/include/目录下
//拷贝libseeta_facedet_lib.so到FaceAlignment/build/目录下
$ cmake ..
$ make -j4
编译报错处理:
(1)打开CMakeLists.txt删除USE_SSE定义
(2)修改face_alignment_test.cpp,改成适配opencv v4以上版本库函数,在源文件添加定义“using std::isnan;”。
编译成功后,得到人脸对齐demofa_test,人脸对齐用户态库libseeta_fa_lib.so和face_alignment.h头文件。

3、在IDE环境编译UVC摄像头人脸对齐演示demo

这里创建OpenCV project工程OpenCV_UVC,添加common.h、face_detection.h、libseeta_facedet_lib.so、face_alignment.h、libseeta_fa_lib.so文件到OpenCV project,然后编写源码如下:

#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"  //CV_FILLED
#include "face_detection.h"
#include "face_alignment.h"
using namespace std;
using namespace cv;
int seeta_fa_main(int argc, char** argv)
{
    bool result_cam = 0;
    printf("face alignment\n");
    // Initialize face detection model
    seeta::FaceDetection detector(“../../seetaface/seeta_fd_frontal_v1.0.bin”);//fd模型
    detector.SetMinFaceSize(40);
    detector.SetScoreThresh(2.f);
    detector.SetImagePyramidScaleFactor(0.8f);
    detector.SetWindowStep(4, 4);
    // Initialize face alignment model
    seeta::FaceAlignment point_detector(“../../seetaface/seeta_fa_v1.1.bin”);//fa模型
    cv::VideoCapture capture;
    result_cam = capture.open(“/dev/video0”, cv::CAP_V4L2);  //打开摄像头
    if(result_cam < 1) {
        printf("open camera fail: %d\n", result_cam);
        return -1;
    }
    capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
    capture.set(cv::CAP_PROP_FRAME_WIDTH, 640);
    capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
    capture.set(cv::CAP_PROP_FPS, 30);
    cv::Mat img;
    cv::Mat img_gray;
    while(1)
    {
        capture >> img;
        if (img.empty()) continue;
        cvtColor(img, img_gray, COLOR_BGR2GRAY);
        int pts_num = 5;
        int im_width = img_gray.cols;
        int im_height = img_gray.rows;
        unsigned char* data = new unsigned char[im_width * im_height];
        unsigned char* data_ptr = data;
        unsigned char* image_data_ptr = (unsigned char*)img_gray.data;
        int h = 0;
        for (h = 0; h < im_height; h++) {
            memcpy(data_ptr, image_data_ptr, im_width);
            data_ptr += im_width;
            image_data_ptr += img_gray.step;
        }
        seeta::ImageData image_data;
        image_data.data = data;
        image_data.width = im_width;
        image_data.height = im_height;
        image_data.num_channels = 1;
        std::vector<seeta::FaceInfo> faces = detector.Detect(image_data);
        int32_t face_num = static_cast<int32_t>(faces.size());
        if (face_num > 0)
        {
            face_num = (face_num>16)?16:face_num;
            seeta::FacialLandmark points[16];
            for(int  id=0; id<face_num; id++)   {  //增加支持多人脸画面的对齐
              point_detector.PointDetectLandmarks(image_data, faces[id], points);
              cv::Rect face_rect;
              face_rect.x = faces[id].bbox.x;
              face_rect.y = faces[id].bbox.y;
              face_rect.width = faces[id].bbox.width;
              face_rect.height = faces[id].bbox.height;
              cv::rectangle(img, face_rect, CV_RGB(0, 0, 255), 1, 8, 0);
              cv::Point pt;
              for (int i = 0; i<pts_num; i++)      {
                pt.x = points[i].x;
                pt.y = points[i].y;
                cv::circle(img, pt, 2, CV_RGB(0, 255, 0), CV_FILLED);
              }
            }
        }
        cv::namedWindow("faceAlignment", cv::WINDOW_AUTOSIZE);
        cv::imshow(“faceAlignment”, img);//显示人脸对齐结果
        delete[]data;
        if (cv::waitKey(1) == 27)  //wait key ESC(27)
            break;
    }
    cv::destroyAllWindows();
    return 0;
}

4、demo演示

编译OpenCV project,得到可运行程序OpenCV_UVC,运行人脸对齐测试程序、根据PID号绑定大核运行。
$ ./OpenCV_UVC
$ top //获取PID(例如5523)…
$ taskset -cp 2 5523

image.png

粗略估算多个人脸图像的对齐速度在5~6 幅/秒。。。

回帖(1)

alwinlee

2023-12-20 10:52:38
加油加油,
举报

更多回帖

发帖
×
20
完善资料,
赚取积分