栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

基于dlib的人脸采集

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于dlib的人脸采集

基于dlib的人脸采集
  • 1:原理
    • 安装dlib
  • 2:代码
    • 2-1:标点
    • 2-2:戴墨镜
    • 2-3:代码
  • 3:效果
    • 3-1:特征点
    • 3-2:戴墨镜
  • 4:总结

基于dlib的人脸采集
  • 1:原理
    • 安装dlib
  • 2:代码
    • 2-1:标点
    • 2-2:戴墨镜
    • 2-3:代码
  • 3:效果
    • 3-1:特征点
    • 3-2:戴墨镜
  • 4:总结

1:原理 安装dlib

1、首先查看自己的python是什么版本的使用命令python -V。

2、找到该版本对应的dlib版本,进行安装,我的python是3.8版本的.

2:代码 2-1:标点
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

ok = True
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0)
#camera = cv2.VideoCapture('video.mp4')
while ok:
    # 读取摄像头中的图像,ok为是否读取成功的判断参数
    ok, img = camera.read()

    # 转换成灰度图像
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    rects = detector(img_gray, 0)
    for i in range(len(rects)):
        landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()])
 # 画特征点
        for idx, point in enumerate(landmarks):
            # 68点的坐标
            pos = (point[0, 0], point[0, 1])

            # 利用cv2.circle给每个特征点画一个圈,共68个
            cv2.circle(img, pos, 1, color=(0, 255, 0))

            # 利用cv2.putText输出1-68
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(idx + 1), pos, font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)
    cv2.imshow('video', img)
    k = cv2.waitKey(1)
    if k == 27:  # 按下ESC退出
        break
camera.release()
cv2.destroyAllWindows()

2-2:戴墨镜
while ok:
    # 读取摄像头中的图像,ok为是否读取成功的判断参数
    ok, img = camera.read()

    # 转换成灰度图像
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    rects = detector(img_gray, 0)
    for i in range(len(rects)):
        landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()])
        # 矩阵转为列表
        point_list=landmarks.getA()

        # 点坐标
        point_37 = (point_list[37][0],point_list[37][1])
        point_38 = (point_list[38][0], point_list[38][1])

        # 比例系数,37,38两点距离
        size = (pow(pow(point_38[1] - point_37[1], 2) + pow(point_38[0] - point_37[0], 2), 0.5))

        # 点坐标
        point_39 = (point_list[39][0], point_list[39][1])
        point_42 = (point_list[42][0], point_list[42][1])

        # 画眼镜
        cv2.circle(img, (point_list[41][0], point_list[41][1]), int(3 * size), (0, 0, 0), -1)
        cv2.circle(img, (point_list[46][0], point_list[46][1]), int(3 * size), (0, 0, 0), -1)
        # 画眼镜框
        cv2.line(img, point_39, point_42, (0, 0, 0), 4)
            cv2.imshow('video', img)
    k = cv2.waitKey(1)
    if k == 27:  # 按下ESC退出
        break
camera.release()
cv2.destroyAllWindows()


2-3:代码
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 27 03:15:10 2021

@author: GT72VR
"""
import numpy as np
import cv2
import dlib
import os
import sys
import random

# dlib预测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

ok = True
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0)
#camera = cv2.VideoCapture('video.mp4')
while ok:
    # 读取摄像头中的图像,ok为是否读取成功的判断参数
    ok, img = camera.read()

    # 转换成灰度图像
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    rects = detector(img_gray, 0)
    for i in range(len(rects)):
        landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()])
        # 矩阵转为列表
        point_list=landmarks.getA()

        # 点坐标
        point_37 = (point_list[37][0],point_list[37][1])
        point_38 = (point_list[38][0], point_list[38][1])

        # 比例系数,37,38两点距离
        size = (pow(pow(point_38[1] - point_37[1], 2) + pow(point_38[0] - point_37[0], 2), 0.5))

        # 点坐标
        point_39 = (point_list[39][0], point_list[39][1])
        point_42 = (point_list[42][0], point_list[42][1])

        # 画眼镜
        cv2.circle(img, (point_list[41][0], point_list[41][1]), int(3 * size), (0, 0, 0), -1)
        cv2.circle(img, (point_list[46][0], point_list[46][1]), int(3 * size), (0, 0, 0), -1)
        # 画眼镜框
        cv2.line(img, point_39, point_42, (0, 0, 0), 4)


        # 画特征点
        for idx, point in enumerate(landmarks):
            # 68点的坐标
            pos = (point[0, 0], point[0, 1])

            # 利用cv2.circle给每个特征点画一个圈,共68个
            cv2.circle(img, pos, 1, color=(0, 255, 0))

            # 利用cv2.putText输出1-68
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(idx + 1), pos, font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)
    cv2.imshow('video', img)
    k = cv2.waitKey(1)
    if k == 27:  # 按下ESC退出
        break
camera.release()
cv2.destroyAllWindows()


3:效果 3-1:特征点

3-2:戴墨镜

4:总结

通过dlib采集人脸特征点实现了人脸采集

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487743.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号