【最快人臉檢測模型開源】libfacedetection開源


來自於仕琪老師的人臉檢測庫,可以實現高達1500fps的人臉檢測
算法源碼已經開源

在這里插入圖片描述

1.簡介

libfacedetection是一個優秀的人臉檢測算法庫,在pc和樹莓派上都能達到十分優秀的檢測結果:

windows上的表現
Method Time FPS Time FPS
X64 X64 X64 X64
Single-thread Single-thread Multi-thread Multi-thread
OpenCV Haar+AdaBoost (640x480) 12.33ms 81.1
cnn (CPU, 640x480) 64.21ms 15.57 15.59ms 64.16
cnn (CPU, 320x240) 15.23ms 65.68 3.99ms 250.40
cnn (CPU, 160x120) 3.47ms 288.08 0.95ms 1052.20
cnn (CPU, 128x96) 2.35ms 425.95 0.64ms 1562.10
  • OpenCV Haar+AdaBoost runs with minimal face size 48x48
  • Face detection only, and no landmark detection included.
  • Minimal face size ~12x12
  • Intel® Core™ i7-7700 CPU @ 3.6GHz.
嵌入式設備Raspberry Pi 3 B+上的表現
Method Time FPS Time FPS
Single-thread Single-thread Multi-thread Multi-thread
cnn (CPU, 640x480) 512.04ms 1.95 174.89ms 5.72
cnn (CPU, 320x240) 123.47ms 8.10 42.13ms 23.74
cnn (CPU, 160x120) 27.42ms 36.47 9.75ms 102.58
cnn (CPU, 128x96) 17.78ms 56.24 6.12ms 163.50
  • Face detection only, and no landmark detection included.
  • Minimal face size ~12x12
  • Raspberry Pi 3 B+, Broadcom BCM2837B0, Cortex-A53 (ARMv8) 64-bit SoC @ 1.4GHz

近日算法的源碼已經開源:
在這里插入圖片描述

2.源碼

源碼中主要包含了四個文件,包括了模型數據、人臉檢測cnn模型的定義和卷積操作的實現等:

facedetectcnn-floatdata.cpp   //模型數據
facedetectcnn-int8data.cpp   //模型數據
facedetectcnn-model.cpp	     //cnn定義
facedetectcnn.cpp            //基本操作定義
facedetectcnn.h

1.使用樣例

API是這樣的:int * facedetect_cnn(unsigned char * result_buffer, //buffer memory for storing face detection results, !!its size must be 0x20000 Bytes!! unsigned char * rgb_image_data, int width, int height, int step); //input image, it must be RGB (three-channel) image!

//copy from https://github.com/ShiqiYu/libfacedetection/blob/master/example/libfacedetectcnn-example.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "facedetectcnn.h"

//定義緩沖區大學. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000
using namespace cv;

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
    	//圖像的路徑輸入
        printf("Usage: %s <image_file_name>\n", argv[0]);
        return -1;
    }

	//load an image and convert it to gray (single-channel)
	Mat image = imread(argv[1]); 
	if(image.empty())
	{
		fprintf(stderr, "Can not load the image file %s.\n", argv[1]);
		return -1;
	}

	int * pResults = NULL; 
    //pBuffer is used in the detection functions.
    //If you call functions in multiple threads, please create one buffer for each thread!
    unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
    if(!pBuffer)
    {
        fprintf(stderr, "Can not alloc buffer.\n");
        return -1;
    }
	

	///////////////////////////////////////////
	// CNN face detection 
	// Best detection rate
	//////////////////////////////////////////
	//!!! The input image must be a RGB one (three-channel)
	//!!! DO NOT RELEASE pResults !!!
	pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);
	//這個api用於檢測

    printf("%d faces detected.\n", (pResults ? *pResults : 0));
	Mat result_cnn = image.clone();
	//print the detection results
	for(int i = 0; i < (pResults ? *pResults : 0); i++)
	{
        
        //返回值position坐標點
        short * p = ((short*)(pResults+1))+142*i;
		int x = p[0];
		int y = p[1];
		int w = p[2];
		int h = p[3];
		//int neighbors = p[4];
		int confidence = p[4]; 
		int angle = p[5];

		//printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle); 
		//update neifhbors to confidence,05302019
		printf("face_rect=[%d, %d, %d, %d], confidence=%d, angle=%d\n", x,y,w,h,confidence, angle);
		rectangle(result_cnn, Rect(x, y, w, h), Scalar(0, 255, 0), 2);   //畫框框
	}
	imshow("result_cnn", result_cnn);

	waitKey();

    //release the buffer
    free(pBuffer);

	return 0;
}

注:
1.可以使用avx2指令集編譯得到更大的加速。
2.於老師曾經寫了著名的《OpenCV入門教程》

ref:
https://blog.csdn.net/m0_37733057/article/details/68059552
https://blog.csdn.net/CV_Jason/article/details/78819088 https://blog.csdn.net/CV_Jason/article/details/78819088
https://blog.csdn.net/sinat_31425585/article/details/77891844

在這里插入圖片描述
pic from pexels.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM