Linux下python与C++使用dlib实现人脸检测


Posted in Python onJune 29, 2018

python 与 C++ dlib人脸检测结果对比,供大家参考,具体内容如下

说明:

由于项目需求发现Linux下c++使用dlib进行人脸检测和python使用dlib检测,得到的结果出入比较大,于是写了测试用例,发现影响结果的原因有但不限于:

1.dlib版本不同(影响不大,几个像素的差别)
2.dlib 人脸检测中detector()第二个参数的设置测试结果如下:

Linux下python与C++使用dlib实现人脸检测

python

PDlib.py:

# -*- coding: utf-8 -*-

import sys
import cv2 
import dlib

from skimage import io
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()

for f in sys.argv[1:]: 
  img = io.imread(f)

  dets = detector(img,1) #使用detector进行人脸检测

  for i, d in enumerate(dets):
    x = d.left()
    y = d.top()
    w = d.right()
    h = d.bottom()   
    cv2.rectangle(img, (x, y), (w, h), (0, 255, 0))
    print("({},{},{},{})".format( x, y, (w-x), (h-y)))

  win.set_image(img)
  io.imsave('./P_Dlib_test.jpg',img)

  #等待点击
  dlib.hit_enter_to_continue()

C++

CDlib.cpp:

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/opencv.h>
#include "opencv2/opencv.hpp"
#include <iostream>

using namespace dlib;
using namespace std;

cv::Rect Detect(cv::Mat im)
{
  cv::Rect R;
  frontal_face_detector detector = get_frontal_face_detector();
  array2d<bgr_pixel> img; 
  assign_image(img, cv_image<uchar>(im));
  std::vector<rectangle> dets = detector(img);//检测人脸

  //查找最大脸
  if (dets.size() != 0)
  {
    int Max = 0;
    int area = 0;
    for (unsigned long t = 0; t < dets.size(); ++t)
    {      
      if (area < dets[t].width()*dets[t].height())
      {
        area = dets[t].width()*dets[t].height();
        Max = t;
      }
    }

    R.x = dets[Max].left();
    R.y = dets[Max].top();
    R.width = dets[Max].width();
    R.height = dets[Max].height();
    cout<<"("<<R.x<<","<<R.y<<","<<R.width<<","<<R.height<<")"<<endl;
  }
  return R;
}

int main(int argc, char** argv)
{
  if (argc != 2) {
    fprintf(stderr, "请输入正确参数\n");
    return 1;
  }  
  string path = argv[1];
  try
  {    
    cv::Mat src, dec;
    src = cv::imread(path);
    src.copyTo(dec);
    cv::cvtColor(dec, dec, CV_BGR2GRAY);
    cv::Rect box;
    box = Detect(dec);
    cv::rectangle(src, box, cv::Scalar(0, 0, 255), 1, 1, 0);    
    cv::imshow("frame", src);
    cv::imwrite("./C_Dlib_test.jpg", src);
    cv::waitKey(0);//等待建入 
  }
  catch (exception& e)
  {
    cout << e.what() << endl;
  }
}

项目编译及运行

python

运行脚本 python PDlib.py G:\DlibTest\data\bush.jpg

C++

  • 创建编译文件夹 mkdir cbuild
  • 切换到编译目录 cd cbuild
  • 生成makefile文件 cmake ..
  • 编译项目 make
  • 运行可执行文件 ./DlibTest G:\DlibTest\data\bush.jpg

Demo:点击下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Django中STATIC_ROOT和STATIC_URL及STATICFILES_DIRS浅析
May 08 Python
浅谈Django中的数据库模型类-models.py(一对一的关系)
May 30 Python
pandas表连接 索引上的合并方法
Jun 08 Python
python traceback捕获并打印异常的方法
Aug 31 Python
python微信好友数据分析详解
Nov 19 Python
对Python正则匹配IP、Url、Mail的方法详解
Dec 25 Python
Python容器使用的5个技巧和2个误区总结
Sep 26 Python
python pptx复制指定页的ppt教程
Feb 14 Python
简述python Scrapy框架
Aug 17 Python
python爬虫智能翻页批量下载文件的实例详解
Feb 02 Python
关于python中readlines函数的参数hint的相关知识总结
Jun 24 Python
python数字图像处理实现图像的形变与缩放
Jun 28 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 #Python
Python数据持久化shelve模块用法分析
Jun 29 #Python
python 统计列表中不同元素的数量方法
Jun 29 #Python
python计算两个数的百分比方法
Jun 29 #Python
python统计字母、空格、数字等字符个数的实例
Jun 29 #Python
python中计算一个列表中连续相同的元素个数方法
Jun 29 #Python
Python使用ConfigParser模块操作配置文件的方法
Jun 29 #Python
You might like
全国FM电台频率大全 - 11 浙江省
2020/03/11 无线电
apache2.2.4+mysql5.0.77+php5.2.8安装精简
2009/04/29 PHP
PHP加Nginx实现动态裁剪图片方案
2014/03/10 PHP
php实现读取超大文件的方法
2014/07/28 PHP
php生成百度sitemap站点地图类函数实例
2014/10/17 PHP
php中HTTP_REFERER函数用法实例
2014/11/21 PHP
PHP模板引擎Smarty内建函数详解
2016/04/11 PHP
php中bind_param()函数用法分析
2017/03/28 PHP
PHP convert_cyr_string()函数讲解
2019/02/13 PHP
php常用的工具开发整理
2019/09/26 PHP
JS 获取select(多选下拉)中所选值的示例代码
2013/08/02 Javascript
firefox浏览器用jquery.uploadify插件上传时报HTTP 302错误
2015/03/01 Javascript
Bootstrap图片轮播组件Carousel使用方法详解
2016/10/20 Javascript
Bootstrap fileinput文件上传预览插件使用详解
2017/05/16 Javascript
VSCode使用之Vue工程配置eslint
2019/04/30 Javascript
原生JavaScript实现日历功能代码实例(无引用Jq)
2019/09/23 Javascript
微信小程序页面间传递数组对象方法解析
2019/11/06 Javascript
[01:11:10]2014 DOTA2华西杯精英邀请赛 5 24 iG VS VG加赛
2014/05/26 DOTA
多线程爬虫批量下载pcgame图片url 保存为xml的实现代码
2013/01/17 Python
Python3连接MySQL(pymysql)模拟转账实现代码
2016/05/24 Python
Python中py文件引用另一个py文件变量的方法
2018/04/29 Python
Python3.5 Json与pickle实现数据序列化与反序列化操作示例
2019/04/29 Python
Django网络框架之HelloDjango项目创建教程
2019/06/06 Python
Python提取转移文件夹内所有.jpg文件并查看每一帧的方法
2019/06/27 Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
2019/07/18 Python
opencv-python的RGB与BGR互转方式
2020/06/02 Python
python 爬取英雄联盟皮肤并下载的示例
2020/12/04 Python
乐高西班牙官方商店:LEGO Shop ES
2019/12/01 全球购物
简历自荐信
2013/12/02 职场文书
财务方面个人工作的自我评价
2013/12/28 职场文书
《草原的早晨》教学反思
2014/04/08 职场文书
食品安全承诺书
2014/05/22 职场文书
会计专业毕业生求职信
2014/07/04 职场文书
2015年党风廉政建设责任书
2015/01/29 职场文书
2015年精神文明建设工作总结
2015/04/21 职场文书
新教师教学工作总结
2015/08/14 职场文书