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.
在本教程中,您将使用来自OpenCV和Python的经过预先训练的Haar Cascade模型来检测和提取图像中的面部。 OpenCV是用于处理图像的开源编程库。
先决条件 (Prerequisites)
A local Python 3 development environment, including
pip
, a tool for installing Python packages, andvenv
, for creating virtual environments.本地Python 3开发环境 ,包括
pip
(用于安装Python软件包的工具)和venv
(用于创建虚拟环境)。
第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:
现在,您已经激活了虚拟环境,您将使用