【亲测可用】图像目标识别入门-利用笔记本电脑摄像头识别人脸标记出来采用深度学习模型实现

更高的精度和准确性,可以考虑使用基于深度学习的人脸检测和识别方法,例如基于人脸特征的人脸检测器和具有高识别率的人脸识别模型。下面是使用基于深度学习的人脸检测和识别方法的代码示例:

  1. 首先,安装必要的库和模型:
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-contrib-python -i https://pypi.tuna.tsinghua.edu.cn/simple

  1. 接下来,使用dlib库进行人脸检测和形状预测,确保您已经安装了dlib库:
pip install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple

  1. 然后,使用face_recognition库进行人脸编码和识别,确保您已经安装了face_recognition库:
pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple

  1. 编写下面的代码,它将使用上述库来实现更高精度的人脸检测和识别:
import cv2
import face_recognition

# 读取图像并进行人脸检测
def detect_faces(image):
    # 将图像转换为RGB格式
    rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # 使用HOG人脸检测器检测人脸
    face_locations = face_recognition.face_locations(rgb_image, model='hog')

    return face_locations

# 在图像中标记人脸
def mark_faces(image, face_locations):
    for top, right, bottom, left in face_locations:
        # 画一个矩形框标记人脸
        cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)

    return image

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

while True:
    # 读取摄像头的图像帧
    ret, frame = cap.read()

    # 进行人脸检测
    face_locations = detect_faces(frame)

    # 标记检测到的人脸
    marked_image = mark_faces(frame, face_locations)

    # 显示带有人脸标记的图像
    cv2.imshow('Face Detection', marked_image)

    # 检测按下键盘上的q键退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放摄像头,关闭窗口
cap.release()
cv2.destroyAllWindows()

此代码使用dlib库进行基于HOG的人脸检测,并使用face_recognition库对检测到的人脸进行编码和识别。它能够提供更高精度和准确性的人脸检测和识别结果,但它可能会导致更高的计算资源需求。

请注意,虽然使用基于深度学习的人脸检测和识别方法可以提供更高的精度,但其识别准确性仍然可能受到多种因素的影响,例如光照条件、人脸角度和遮挡。因此,在特定应用场景中,可能需要进一步优化和调整参数,或使用更复杂的人脸识别模型来提高准确性。

注意事项:安装不指定版本的dlib并不会安装所需要的动态库,一运行会报错:

import face_recognition File "D:\anaconda3-2023.07-2\envs\test_env-py3.8\lib\site-packages\face_recognition\__init__.py", line 7, in <module> from .api import load_image_file, face_locations, batch_face_locations, face_landmarks, face_encodings, compare_faces, face_distance File "D:\anaconda3-2023.07-2\envs\test_env-py3.8\lib\site-packages\face_recognition\api.py", line 4, in <module> import dlib File "D:\anaconda3-2023.07-2\envs\test_env-py3.8\lib\site-packages\dlib\__init__.py", line 19, in <module> from _dlib_pybind11 import * ImportError: DLL load failed while importing _dlib_pybind11: 找不到指定的模块。

制定版本安装时提示cmake没有:

pip install dlib==19.22.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting dlib==19.22.0 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/11/93/ec41d6ef7e769977aa08e49441c52276da27859f12dcbf1c6deb96ce5e9f/dlib-19.22.0.tar.gz (7.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.4/7.4 MB 14.4 MB/s eta 0:00:00 Preparing metadata (setup.py) ... done Building wheels for collected packages: dlib Building wheel for dlib (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [7 lines of output] running bdist_wheel running build running build_py running build_ext ERROR: CMake must be installed to build dlib [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for dlib Running setup.py clean for dlib Failed to build dlib ERROR: Could not build wheels for dlib, which is required to install pyproject.toml-based projects

以下步骤安装cmake:

以下步骤进行操作:

  1. 访问CMake的官方网站:https://cmake.org/download/ ,在页面上找到“Download”按钮,点击进入下载页面。

  2. 在Windows下,您可以选择下载安装程序(Windows win64-x64 Installer),根据您的操作系统位数(64位或32位)选择相应的安装程序。

  3. 下载完成后,双击运行下载的安装程序。如果出现用户控制提示,请点击“是”继续。

  4. 在安装程序中,您可以选择是否将CMake添加到系统的环境变量中。建议勾选此选项,这样CMake就可以在任何位置都能够访问。

  5. 点击“Next”按钮,选择合适的安装选项。

  6. 点击“Install”按钮开始安装。等待安装程序完成安装过程。

  7. 安装完成后,您可以在开始菜单中找到CMake,或者在命令提示符窗口中运行以下命令来验证安装:

    cmake --version
    

    如果命令能够正确执行并显示CMake的版本信息,则说明CMake已成功安装。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Zda天天爱打卡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值