如何使用OpenCV和Python从图像中检测和提取人脸

The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。

介绍 (Introduction)

Images make up a large amount of the data that gets generated each day, which makes the ability to process these images important. One method of processing images is via face detection. Face detection is a branch of image processing that uses machine learning to detect faces in images.

图像构成了每天生成的大量数据,因此处理这些图像的能力很重要。 一种处理图像的方法是通过面部检测 。 人脸检测是图像处理的一个分支,它使用机器学习来检测图像中的人脸。

A Haar Cascade is an object detection method used to locate an object of interest in images. The algorithm is trained on a large number of positive and negative samples, where positive samples are images that contain the object of interest. Negative samples are images that may contain anything but the desired object. Once trained, the classifier can then locate the object of interest in any new images.

Haar级联是一种用于在图像中定位目标物体的物体检测方法。 在大量正样本和负样本上对算法进行了训练,其中正样本是包含感兴趣对象的图像。 负样本是可能包含除所需对象之外的任何图像的图像。 一旦训练完成,分类器便可以在任何新图像中定位感兴趣的对象。

In this tutorial, you will use a pre-trained Haar Cascade model from OpenCV and Python to detect and extract faces from an image. OpenCV is an open-source programming library that is used to process images.

在本教程中,您将使用来自OpenCVPython的经过预先训练的Haar Cascade模型来检测和提取图像中的面部。 OpenCV是用于处理图像的开源编程库。

先决条件 (Prerequisites)

第1步-配置本地环境 (Step 1 — Configuring the Local Environment)

Before you begin writing your code, you will first create a workspace to hold the code and install a few dependencies.

在开始编写代码之前,您将首先创建一个工作区来保存代码并安装一些依赖项。

Create a directory for the project with the mkdir command:

使用mkdir命令为项目创建目录:

  • mkdir face_scrapper

    mkdir face_scrapper

Change into the newly created directory:

转到新创建的目录:

  • cd face_scrapper

    cd face_scrapper

Next, you will create a virtual environment for this project. Virtual environments isolate different projects so that differing dependencies won’t cause any disruptions. Create a virtual environment named face_scrapper to use with this project:

接下来,您将为此项目创建一个虚拟环境。 虚拟环境隔离了不同的项目,因此不同的依赖项不会造成任何中断。 创建一个名为face_scrapper的虚拟环境以用于该项目:

  • python3 -m venv face_scrapper

    python3 -m venv face_scrapper

Activate the isolated environment:

激活隔离的环境:

  • source face_scrapper/bin/activate

    源face_scrapper / bin / activate

You will now see that your prompt is prefixed with the name of your virtual environment:

现在,您将看到提示中带有虚拟环境名称的前缀:

Now that you’ve activated your virtual environment, you will use nano or your favorite text editor to create a requirements.txt file. This file indicates the necessary Python dependencies:

现在,您已经激活了虚拟环境,您将使用

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值