• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

go使用dlib人脸检测和人脸识别

武飞扬头像
xinlinhack
帮助1

一、安装go-face库

      准备好环境,需要下载go-face库:

go get -u -f  github.com/Kagami/go-face

官方源码路径

https://github.com/Kagami/go-face

二、安装dlib

      安装这个库挺费时间的,比编写go代码还要长,目前 作者的环境是windows 10, 64位环境。

1、下载MSYS2,是一款高效专业的类似于linux的开发环境软件,该软件基于Cygwin和MinGW-w64的MSYS进行重写,集合了丰富实用的工具组件。下载地址为MSYS2

2、安装此软件,安装成功后,在windows的“开始”菜单,选择MSYS2 MSYS。

3、在MSYS2超级终端里,输入pacman -Syu,如果提示关闭,就关闭。

4、再执行第二步,重新输入pacman -Syu

5、然后输入,pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-dlib,至此你已成功安装了dlib了。

6、如果您已经安装了Go和Git,而且想在MSYS2超级终端使用GO和GIT命令,需要修改msys2_shell.cmd文件,搜索“rem set MSYS2_PATH_TYPE=inherit”,然后修改为set MSYS2_PATH_TYPE=inherit,保存即可。

7、从“开始”菜单,选择并运行MSYS2 MinGW x64,然后进入你的例子代码路径,输入go build main.go编译即可。

三、模型下载

下载地址 http://dlib.net/files/

学新通

 下载三个模型

shape_predictor_5_face_landmarks.dat

dlib_face_recognition_resnet_model_v1.dat

mmod_human_face_detector.dat

四、准备好gocv环境

安装的教程如下:

使用gocv,更好的处理图片。

五、示例代码

单一人脸:

学新通

多人脸图片:

学新通

  1.  
    package main
  2.  
     
  3.  
    import (
  4.  
    "fmt"
  5.  
    "github.com/Kagami/go-face"
  6.  
    "gocv.io/x/gocv"
  7.  
    "image"
  8.  
    "image/color"
  9.  
    "log"
  10.  
    "path/filepath"
  11.  
    )
  12.  
     
  13.  
    //
  14.  
    const (
  15.  
    dataDir = "models"
  16.  
    imageDir = "images"
  17.  
    )
  18.  
     
  19.  
     
  20.  
     
  21.  
    func main() {
  22.  
     
  23.  
    // 特征值
  24.  
    var descriptor face.Descriptor
  25.  
     
  26.  
    // 目标距离
  27.  
    matchDistance := 0.1
  28.  
     
  29.  
    // 创建一个窗口
  30.  
    window := gocv.NewWindow("dlib Recognize")
  31.  
    defer window.Close()
  32.  
     
  33.  
    // 颜色
  34.  
    greenColor := color.RGBA{0, 255, 0, 255}
  35.  
    redColor := color.RGBA{255, 0, 0, 255}
  36.  
     
  37.  
     
  38.  
    // 加载模型
  39.  
    rec, err := face.NewRecognizer(dataDir)
  40.  
     
  41.  
    if err != nil {
  42.  
    log.Fatal(err)
  43.  
    }
  44.  
     
  45.  
    defer rec.Close()
  46.  
     
  47.  
    // 单一脸图片
  48.  
    faceImagePath := filepath.Join(imageDir, "face.jpg")
  49.  
     
  50.  
    img1 := gocv.IMRead(faceImagePath, gocv.IMReadColor)
  51.  
    defer img1.Close()
  52.  
     
  53.  
    fmt.Println("正在读的单一脸图像 = ", faceImagePath)
  54.  
     
  55.  
    //
  56.  
    faces, err := rec.RecognizeFile(faceImagePath)
  57.  
     
  58.  
    if err != nil {
  59.  
    log.Fatalf("无法识别: %v", err)
  60.  
    }
  61.  
     
  62.  
    if 0 == len(faces){
  63.  
    log.Fatal("图片没有人脸")
  64.  
    }
  65.  
     
  66.  
    for _, f := range faces{
  67.  
    descriptor = f.Descriptor
  68.  
    }
  69.  
     
  70.  
     
  71.  
    // 多人脸图片
  72.  
    facesImagePath := filepath.Join(imageDir, "faces.jpg")
  73.  
     
  74.  
    img2 := gocv.IMRead(facesImagePath, gocv.IMReadColor)
  75.  
    defer img2.Close()
  76.  
     
  77.  
    // copy bg to draw
  78.  
    background := img2.Clone()
  79.  
    defer background.Close()
  80.  
     
  81.  
     
  82.  
    //
  83.  
    fmt.Println("正在读的多人脸图像 = ", facesImagePath)
  84.  
     
  85.  
    //
  86.  
    faces, err = rec.RecognizeFile(facesImagePath)
  87.  
     
  88.  
    if err != nil {
  89.  
    log.Fatalf("无法识别: %v", err)
  90.  
    }
  91.  
     
  92.  
    if 0 == len(faces){
  93.  
    log.Fatal("图片没有人脸")
  94.  
    }
  95.  
     
  96.  
    for _, f := range faces{
  97.  
     
  98.  
    gocv.Rectangle(&background, f.Rectangle, redColor, 3)
  99.  
     
  100.  
    // 计算特征值之间的欧拉距离
  101.  
    dist := face.SquaredEuclideanDistance(f.Descriptor, descriptor)
  102.  
     
  103.  
    fmt.Println("欧拉距离 = ", dist)
  104.  
     
  105.  
    c := redColor
  106.  
     
  107.  
    if dist < matchDistance {
  108.  
    c = greenColor
  109.  
    }
  110.  
     
  111.  
    // 在图片上画人脸框
  112.  
    pt := image.Pt(f.Rectangle.Min.X, f.Rectangle.Min.Y-20)
  113.  
    gocv.PutText(&background, "jay", pt, gocv.FontHersheyPlain, 2, c, 2)
  114.  
     
  115.  
    }
  116.  
     
  117.  
     
  118.  
    // 显示图片
  119.  
    window.IMShow(background)
  120.  
     
  121.  
    for{
  122.  
    if window.WaitKey(1) >= 0 {
  123.  
    break
  124.  
    }
  125.  
    }
  126.  
     
  127.  
    }
学新通

最终输出如下,绿色表示找到的人脸图片。

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfaffe
系列文章
更多 icon
同类精品
更多 icon
继续加载