搜索
首页后端开发Python教程利用Python实现人脸识别的方法及步骤

对于开发环境,我们将使用 Visual Studio Community Edition。

如果你的计算机上还没有安装它,你可以从这里下载。并使用 C++安装桌面开发。

如何利用Python实现面部识别

现在我们有了使用 C++ 进行桌面开发的 Visual Studio,我们可以开始我们的项目了。

使用 Visual Studio 打开一个新目录并创建一个新的 python 环境。我们将使用venv. 打开你的集成终端并编写python -m venv venv。然后通过键入venv/bin/Activate.ps1激活环境。这是针对 PowerShell 的。

如果你使用任何其他终端,你可以在此处找到完整列表

如何利用Python实现面部识别

现在我们已经完成了虚拟环境的创建,让我们开始提取我们的依赖项。为此,我们将需要opencv和face_recognition。在你的终端内使用pip.

pip install opencv-python face_recognition

Face Recognition是一个使用最先进的dlib库的库。我们准备好编写一些代码并识别一些面孔。

创建一个新的 python 文件,我们将调用文件missingPerson.py,假设我们将使用我们的应用程序匹配失踪人员。导入我们的依赖项并编写我们的前几行。

import cv2
import numpy as np
import face_recognition
import os
 
from face_recognition.api import face_distance

假设我们所有的照片都存储在我们的服务器存储中,我们需要首先将所有人物的图像拉入我们的应用程序并读取这些图像。

path = 'MissingPersons'
images = []
missingPersons = []
missingPersonsList = os.listdir(path)
 
for missingPerson in missingPersonsList :
    curImg = cv2.imread(f'{path}/{missingPerson}')
    images.append(curImg)
    missingPersons.append(os.path.splitext(missingPerson)[0])
print(missingPersons)

在本节中,我们将使用 opencv 读取失踪人员的所有图像并将它们附加到我们的missingPerson列表中。

在我们从存储中读取所有丢失的人脸图像后,我们需要找到人脸编码,以便我们可以使用 CNN 人脸检测器在图像中创建人脸边界框的二维数组。

def findEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encodeList.append(encode)
    print(encodeList)
    return encodeList
 
encodeListKnown = findEncodings(images)
print('Encoding Complete')

我们将二维数组存储到已知人脸编码列表中。这将需要几分钟。

现在我们有了所有失踪人员的面部编码,我们现在要做的就是将它们与我们的报告人图像进行匹配。face_recognition使用起来非常方便。

def findMissingPerson(encodeListKnown, reportedPerson='found1.jpg'):
    person = face_recognition.load_image_file(f'ReportedPersons/{reportedPerson}]')
    person = cv2.cvtColor(person,cv2.COLOR_BGR2RGB)
 
    try:
        encodePerson = face_recognition.face_encodings(person)[0]
 
        comparedFace = face_recognition.compare_faces(encodeListKnown,encodePerson)
        faceDis = face_recognition.face_distance(encodeListKnown,encodePerson)
        matchIndex = np.argmin(faceDis)
        if comparedFace[matchIndex]:
            name = missingPersons[matchIndex].upper()
            print(name)
            return name
        else:
          print('Not Found')
          return False
        
    except IndexError as e:
        print(e)
        return e

首先我们需要加载被报告人的图像文件,对他们的脸进行编码。剩下的就是将被报告人脸编码与我们已知的人脸编码进行比较。然后一个简单的逻辑匹配他们的索引并返回是否在我们的 missingPersons 列表中找到该人。

这种人脸识别不仅用于寻找失踪人员。它可以检测和识别人脸,并且可以根据需要进行操作。

以上是利用Python实现人脸识别的方法及步骤的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

对于循环和python中的循环时:每个循环的优点是什么?对于循环和python中的循环时:每个循环的优点是什么?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器