python3+opencv3.4+dlib库编程实现人脸特征点标定

文章目录

      • 一.安装dlib以及OpenCV
      • 二.dlib的68点模型
      • 三.Python实现摄像头人脸检测
      • 参考链接:

一.安装dlib以及OpenCV

到此处http://dlib.net找到自己合适的安装包下载,下载后解压缩包。
在Anaconda Prompt中使用命令pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl命令中的cp37是指自己的python版本为3.7,如果是其他版本要下载对应的安装包,不要下错了!!!

命令pip install opencv-python

二.dlib的68点模型

链接:https://pan.baidu.com/s/1JjCjv66S0HPkDEHEsIiMcQ
提取码:1234

三.Python实现摄像头人脸检测

# -*- 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
# 存储位置
output_dir = 'D:/faces'
size = 64
 
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
# 改变图片的亮度与对比度
 
def relight(img, light=1, bias=0):
    w = img.shape[1]
    h = img.shape[0]
    #image = []
    for i in range(0,w):
        for j in range(0,h):
            for c in range(3):
                tmp = int(img[j,i,c]*light + bias)
                if tmp > 255:
                    tmp = 255
                elif tmp < 0:
                    tmp = 0
                img[j,i,c] = tmp
    return img
 
#使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0)
#camera = cv2.VideoCapture('C:/Users/CUNGU/Videos/Captures/wang.mp4')
ok = True

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('d:/shape_predictor_68_face_landmarks.dat')


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])
            print(idx,pos)
    
            # 利用cv2.circle给每个特征点画一个圈,共68个
            cv2.circle(img, pos, 2, color=(0, 255, 0))
            # 利用cv2.putText输出1-68
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(idx+1), pos, font, 0.2, (0, 0, 255), 1,cv2.LINE_AA)
    cv2.imshow('video', img)
    k = cv2.waitKey(1)
    if k == 27:    # press 'ESC' to quit
        break
    
camera.release()
cv2.destroyAllWindows()

运行结果:

python3+opencv3.4+dlib库编程实现人脸特征点标定_第1张图片

找到双眼特征点坐标值,以坐标中心点为圆心,用opencv函数绘制两个圆,并填充黑色。(即相当于给人脸配上一付墨镜)
在绘制点的循环中添加代码(纯黑色太可怕了,加点边框修饰一下):

left_pointx=left_pointy=right_pointx=right_pointy=0
        for i in range(36,42):
            left_pointx+=landmarks[i][0,0]
            left_pointy+=landmarks[i][0,1]
        pos_left=(int(left_pointx/6),int(left_pointy/6))
        cv2.circle(img,pos_left,20,color=(255,0,0))
        cv2.circle(img,pos_left,19,color=(255,153,0))
        cv2.circle(img,pos_left,18,color=(255,255,0))
        cv2.circle(img,pos_left,17,color=(0,102,0))
        cv2.circle(img,pos_left,16,color=(51,255,51))
        cv2.circle(img,pos_left,18,color=(0,204,255))
        cv2.circle(img,pos_left,18,color=(102,0,204))
        cv2.circle(img,pos_left,17,color=(0,0,0),thickness=-1)
        for i in range(42,48):
            right_pointx+=landmarks[i][0,0]
            right_pointy+=landmarks[i][0,1]
        pos_right=(int(right_pointx/6),int(right_pointy/6))
        cv2.circle(img,pos_right,20,color=(255,0,0))
        cv2.circle(img,pos_right,19,color=(255,153,0))
        cv2.circle(img,pos_right,18,color=(255,255,0))
        cv2.circle(img,pos_right,17,color=(0,102,0))
        cv2.circle(img,pos_right,16,color=(51,255,51))
        cv2.circle(img,pos_right,18,color=(0,204,255))
        cv2.circle(img,pos_right,18,color=(102,0,204))
        cv2.circle(img,pos_right,17,color=(0,0,0),thickness=-1)

运行效果如图:

python3+opencv3.4+dlib库编程实现人脸特征点标定_第2张图片

最后加一句:杨洋真帅!!!

参考链接:

https://blog.csdn.net/junseven164/article/details/121054134

你可能感兴趣的:(笔记,opencv,python,计算机视觉)