python --dlib人脸识别(保姆级安装和识别)

面部特征点检测

import dlib
import dlib
import cv2
import numpy as np

# 加载模型
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(r'D:\code\edg\shape_predictor_68_face_landmarks.dat')

# 读取图片
img = cv2.imread(r'C:\Users\Yi\Desktop\88.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 检测面部
faces = detector(gray)

# 标记面部特征点
for face in faces:
    landmarks = predictor(gray, face)
    for n in range(0, 68):
        x = landmarks.part(n).x
        y = landmarks.part(n).y
        cv2.circle(img, (x, y), 2, (255, 0, 0), -1)

# 显示图片
cv2.imshow('Facial Landmarks', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

人脸检测

import dlib
import cv2


# 加载模型
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(r'D:\code\edg\shape_predictor_68_face_landmarks.dat')


# 读取图片
img = cv2.imread(r'C:\Users\Yi\Desktop\88.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 检测面部
faces = detector(gray)

# 在原图上绘制矩形框
for face in faces:
    x, y, w, h = (face.left(), face.top(), face.width(), face.height())
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

# 显示图片
cv2.imshow('Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ba4ac8747ba64a48a60f6349fe76815e.png

视频帧处理

import dlib
import cv2


# 加载模型
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(r'D:\code\edg\shape_predictor_68_face_landmarks.dat')
facerec = dlib.face_recognition_model_v1(r'D:\code\edg\dlib_face_recognition_resnet_model_v1.dat')


# 打开摄像头
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = detector(gray)

    for face in faces:
        landmarks = predictor(gray, face)
        for n in range(0, 68):
            x = landmarks.part(n).x
            y = landmarks.part(n).y
            cv2.circle(frame, (x, y), 2, (255, 0, 0), -1)

    cv2.imshow('Video Frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

人脸比对

import dlib
import cv2
import numpy as np

# 加载模型
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(r'D:\code\edg\shape_predictor_68_face_landmarks.dat')
facerec = dlib.face_recognition_model_v1(r'D:\code\edg\dlib_face_recognition_resnet_model_v1.dat')


# 创建一个函数用于获取面部特征向量
def get_face_descriptor(img):
    dets = detector(img, 1)
    if len(dets) == 0:
        return None

    shape = predictor(img, dets[0])
    face_descriptor = facerec.compute_face_descriptor(img, shape)
    return np.array(face_descriptor)


# 读取图片
img1 = cv2.imread(r'C:\Users\Yi\Desktop\88.jpg')
img2 = cv2.imread(r'C:\Users\Yi\Desktop\99.jpg')

# 获取面部特征向量
descriptor1 = get_face_descriptor(img1)
descriptor2 = get_face_descriptor(img2)

# 计算两者之间的欧式距离
dist = np.linalg.norm(descriptor1 - descriptor2)

print(f'The distance between the two faces is: {dist}')

# 通常距离越小相似度越高
if dist < 0.6:
    print('同一个人')
else:
    print('不同人')

在这里插入图片描述

模型文件资源联系qq
qq: 1206154726

Linux

linux安装测试了好多方法都不行 最后在conda环境下安装成功

conda install -c conda-forge dlib

命令分解
conda:

这是命令行工具,用于管理 Conda 环境和包。
install:

这是一个子命令,告诉 Conda 要安装一个或多个包。
-c conda-forge:

-c 是 --channel 的缩写,指定要从哪个渠道(channel)安装包。
conda-forge 是一个社区驱动的 Conda 包仓库,提供了许多开源软件包,包括 dlib。使用 -c conda-forge 表示你希望从这个渠道获取 dlib 包。
使用 conda-forge 的好处是,它通常会提供更新更快、依赖关系处理更好的包版本。
dlib:

这是你要安装的具体包的名称。在这里,它是一个用于机器学习和计算机视觉的库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
是的,你可以使用OpenCV-Pythondlib库实现嘴巴和眼睛的分割。以下是基本步骤: 1. 使用dlib中的人脸检测器检测人脸。 2. 使用dlib中的关键点检测器检测人脸的关键点,其中包括眼睛和嘴巴的关键点。 3. 根据关键点的位置,使用OpenCV中的形态学操作和阈值处理来分割眼睛和嘴巴。 你可以使用以下代码片段开始: ```python import cv2 import dlib # 加载人脸检测器和关键点检测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') # 读取图像 image = cv2.imread('test.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = detector(gray, 0) for face in faces: # 检测关键点 landmarks = predictor(gray, face) # 提取嘴巴关键点 mouth_points = [] for i in range(48, 68): x = landmarks.part(i).x y = landmarks.part(i).y mouth_points.append((x, y)) # 提取左眼关键点 left_eye_points = [] for i in range(36, 42): x = landmarks.part(i).x y = landmarks.part(i).y left_eye_points.append((x, y)) # 提取右眼关键点 right_eye_points = [] for i in range(42, 48): x = landmarks.part(i).x y = landmarks.part(i).y right_eye_points.append((x, y)) # 分割嘴巴 mouth_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8) mouth_hull = cv2.convexHull(np.array(mouth_points)) cv2.drawContours(mouth_mask, [mouth_hull], -1, 255, -1) mouth = cv2.bitwise_and(image, image, mask=mouth_mask) # 分割左眼 left_eye_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8) left_eye_hull = cv2.convexHull(np.array(left_eye_points)) cv2.drawContours(left_eye_mask, [left_eye_hull], -1, 255, -1) left_eye = cv2.bitwise_and(image, image, mask=left_eye_mask) # 分割右眼 right_eye_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8) right_eye_hull = cv2.convexHull(np.array(right_eye_points)) cv2.drawContours(right_eye_mask, [right_eye_hull], -1, 255, -1) right_eye = cv2.bitwise_and(image, image, mask=right_eye_mask) # 显示结果 cv2.imshow('Mouth', mouth) cv2.imshow('Left Eye', left_eye) cv2.imshow('Right Eye', right_eye) cv2.waitKey(0) cv2.destroyAllWindows() ``` 请注意,此代码片段仅用于演示目的,并且可能需要进行更改以适应您的特定情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像风一样的男人@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值