媒体大数据实例分析-python人脸识别初探


一、基于OpenCV人脸检测

1. 准备工作

安装opencv:

pip install opencv-python(速度慢)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python

获取分类器位置:
Python路径下找到Lib\sitepackages\cv2\data\haarcascade_frontalface_default.xml文件

准备一张包含人脸的图片

2. 代码

import cv2


def detect(filename):
    ##创建⼀个级联分类器,加载⼀个 .xml⽂件
    # 这⾥路径替换成⾃⼰的'Python路径下/Lib/sitepackages/cv2/data/haarcascade_frontalface_default.xml'
    face_cascade = cv2.CascadeClassifier('G:\Anaconder3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
    img = cv2.imread(filename)
    # 转换为灰度图像
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 使⽤上⾯创建的分类器来检测⼈脸,faces为识别出脸部的矩形框向量组,⼏张脸就有⼏个矩 形框,向量组⾥就有⼏组向量,保存⼈脸的坐标和⼤⼩。
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    # scaleFactor表示⼈脸检测过程中每次迭代时图⽚的压缩率(1.3)
    # minNeighbors:每个⼈脸矩形保留近邻数⽬的最⼩值(5)
    # 在原图上绘制矩形
    # 遍历faces矩形框向量组⾥每⼀组矩形(即每⼀张脸)
    for (x, y, w, h) in faces:
        # 通过坐标绘制矩形,x,y是左上⻆坐标;w,h分别是宽度和⾼度
        # 参数:图⽚,⻓⽅形框左上⻆坐标, ⻓⽅形框右下⻆坐标, 字体颜⾊,字体粗细)
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        # 其中参数的含义:(255, 0, 0)表示颜⾊, 2表示线条粗细
        cv2.imshow('Person Detected!', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
if __name__ == '__main__':
    detect(r'G:\test.jpg')

3. 测试结果

在这里插入图片描述


二、基于Dlib+fr人脸检测

基于OpenCV有时不够精确,⼈脸识别还可以使⽤⼀种准确性更⾼的⽅法——Face-Recognition

1. 安装运行环境

python+opencv+dlib+face_recognition(前两个上⾯已经安装好)
1.pip install cmake
2.pip install dlib (若报错,参考该链接进⾏安装)
3.pip install face_recognition

若安装dlib报错:
https://blog.csdn.net/qq_58691861/article/details/119116148
https://zhuanlan.zhihu.com/p/464846060

2. 代码

import face_recognition
import cv2
#加载图像⽂件(.jpg,.png等),返回的数据是Numpy数组,记录了图⽚的所有像素的特征向量
image = face_recognition.load_image_file("test.jpg")
#定位图中所有的⼈脸的像素位置,返回值为列表形式,列表中每⼀⾏是⼀张⼈脸的位置信息,包括 【top, right, bottom, left】 这是⼀组元组。
face_locations_noCNN=face_recognition.face_locations(image)
# face_locations_useCNN =
face_recognition.face_locations(image,model='cnn')
# A list of tuples of found face locations in css (top, right, bottom,left) order
# 因为返回值的顺序是这样⼦的,因此在后⾯的for循环⾥⾯赋值要注意按这个顺序来
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)
# The number of faces
# 到这⾥为⽌,可以观察两种情况的坐标和⼈脸数,⼀般来说,坐标会不⼀样,但是检测出来的⼈ 脸数应该是⼀样的
# 也就是说face_num1 = face_num2; face_locations_useCNN 和face_locations_noCNN 不⼀样
org = cv2.imread("test.jpg")
img = cv2.imread("test.jpg")
cv2.imshow("test.jpg",img) # 原始图⽚
for i in range(0,face_num2):
    top = face_locations_noCNN[i][0]
    right = face_locations_noCNN[i][1]
    bottom = face_locations_noCNN[i][2]
    left = face_locations_noCNN[i][3]
    start = (left, top)
    end = (right, bottom)
    color = (0,255,255)
    thickness = 2
    #参数:图⽚,⻓⽅形框左上⻆坐标, ⻓⽅形框右下⻆坐标, 字体颜⾊,字体粗细) 123456789
    # Dlib有专⻔的函数和模型,能够实现⼈脸68个特征点的定位。 ⽀持更加精确的标定点。标定点分为5点和68点,可以圈出⾯部眼⿐嘴的位置。
    # 需要先下载预训练模型 ⼈脸特征点检测
    cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. 测试结果

在这里插入图片描述


三、基于Dlib人脸对齐

Dlib有专⻔的函数和模型,能够实现⼈脸68个特征点的定位。 ⽀持更加精确的标定点。
标定点分为5点和68点,可以圈出⾯部眼⿐嘴的位置。

1. 需要先下载预训练模型

shape_predictor_68_face_landmarks.dat
shape_predictor_5_face_landmarks.dat
这两个包放在python路径下的site-packages⾥

2. 代码

import cv2
import dlib
path = "test.jpg"
#读取图⽚
img = cv2.imread(path)
#灰度化处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#获得脸部位置检测器
detector = dlib.get_frontal_face_detector()
# 初步⼈脸检测,框出矩形。返回值是<class 'dlib.dlib.rectangle'>,即⼀个矩形,表示 为能够唯⼀表示这个⼈脸矩形框两个点坐标:左上⻆(x1,y1)、右下⻆(x2,y2
dets = detector(gray, 1)
# 使⽤模型构建特征提取器,返回5/68个特征点的位置 #此处路径是⾃⼰的python路径下site-packages位置
predictor = dlib.shape_predictor(r"G:\Anaconder3\Lib\site-packages\shape_predictor_68_face_landmarks.dat")
for face in dets:
    shape = predictor(img, face) # 寻找⼈脸的68个标定点
    # 遍历所有点,打印出其坐标,并圈出来
    #shape.parts() 类型为_dlib_pybind11.points,可返回检测到的关键点的位置坐标
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        #参数:图⽚,圆⼼, 半径, 字体颜⾊,字体粗细)
        cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
    cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. 测试结果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值