您现在的位置是:热点 >>正文
用Python进行人脸识别「包括源代码」
热点492942人已围观
简介...
Python可以从图像或视频中检测和识别你的脸。
人脸检测与识别是计算机视觉领域的研究热点之一。
人脸识别的应用包括人脸解锁、安全防护等,医生和医务人员利用人脸识别来获取病历和病史,更好地诊断疾病。
关于Python人脸识别
在这个python项目中,我们将构建一个机器学习模型,该模型从图像中识别人。我们在项目中使用了人脸识别API和OpenCV。
随时了解最新用P的技术趋势
加入DataFlair的电报!
工具与图书馆 Python-3.x CV2-4.5.2 矮胖-1.20.3 人脸识别-1.3.0
若要安装上述软件包,请使用以下命令。
pip install numpy opencv-python
要安装FaceRecognition,首先安装dlib包。
pip install dlib
现在,使用以下命令安装面部识别模块
pip instaytll face_recognition
下载人脸识别Python代码
请下载python面部识别项目的源代码: 人脸识别工程代码
项目数据集
我们可以使用我们自己的数据集来完成这个人脸识别项目。对于这个项目,让我们以受欢迎的美国网络系列“老友记”为数据集。该数据集包含在面部识别项目代码中,您在上一节中下载了该代码。
建立人脸识别模型的步骤
在继续之前,让我们知道什么是人脸识别和ho检测。
人脸识别是从照片和视频帧中识别或验证一个人的脸的过程。
人脸检测是指在图像中定位和提取人脸(位置和大小)以供人脸检测算法使用的过程。
人脸识别方法用于定位图像中唯一指定的特征。在大多数情况下,面部图片已经被移除、裁剪、缩放和转换为灰度。人脸识别包括三个步骤:人脸检测、特征提取、人脸识别。
OpenCV是一个用C++编写的开源库.它包含了用于计算机视觉任务的各种n进算法和深度神经网络的实现。
1.准备数据集
创建2个目录,训练和测试。从互联网上为每个演员选择一个图片,并下载到我们的“火车”目录中。确保您所选择的图像能够很好地显示人脸的特征,以便对分类器进行分类。
为了测试模型,让我们拍摄一张包含所有强制转换的图片,并将其放到我们的“test”目录中。
为了您的舒适,我们增加了培训和测试数据与项目代码。
2.模型的训练
首先导入行人必要的模块。
import face_recognition as fr
import cv2
import numpy as np
import os
人脸识别库包含帮助人脸识别过程的各种实用程序的实现。
现在,创建2个列表来存储图像(人员)的名称及其各自的脸编码。
path = "./train/"
known_names = []
known_name_encodings = []
image脸识s = os.listdir(path)
人脸编码是一种值的矢量,它代表着脸部特征之间的重要度量,如眼睛之间的距离、额头的宽度等。
我们循环遍历火车目录中的每个图像,提取图像中的人的姓名,计算其脸编码向量,并将信息存储在相应的列表中。
for _ in images:
image = fr.load_image_file(path + _)
image_path = path + _
en别「coding = fr.face_encodings(image)[0]
known_name_encodings.append(encoding)
known_names.append(os.path.splitext(os.path.basename(image_path))[0].capitalize())
3.在测试数据集中测试模型
如前所述,我们的测试数据集只包括包含一个包含所有人员的图像。
使用CV2 imread()方法读取测试映像。
test_image = "./test/test.jpg"
image = cv2.imread(test_image)
人脸识别库提供了一种名为Face_Locations()的有用方法,它定位图像中检测到的每个人脸的坐标(左、下、右、上)。使用这些位置值,我们可以很容易地找到脸编码。
face_lo源代cations = fr.face_locations(image)
face_encodings = fr.face_encodings(image, face_locations)
我们循环遍历每个面部位置及其在图像中的编码。然后,我们将这种编码与“列车”数据集中的人脸编码进行比较。
然后计算人脸距离,即计算测试图像编码和训练图像编码之间的相似性。现在,我们从它选码」取最小值距离,表示测试图像的这张脸是训练数据集中的人之一。
现在,使用CV2模块中的方法绘制一个带有面部位置坐标的矩形。
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
matches = fr.compare_faces(known_name_encod用Pings, face_encoding)
name = ""
face_distances = fr.face_distance(known_name_encodings, face_encoding)
best_match = np.argmin(face_distances)
if matches[best_match]:
name = known_names[best_match]
cytv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(image, (left, bottom - 15), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(image, name, (holeft + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
使用CV2模块的imShow()方法显示图像。
cv2.imshow("Result", image)
使用imwrite()方法将图像保存到当前工作目录中。
cv2.imwrite("./output.jpg", image)
释放未被释放的资源(如果有的话)。
cv2.waitKen进y(0)
cv2.destroyAllWindows()
Python人脸识别输出
让我们看看模型的输出。
Tags:
相关文章
标签: 中国联通 5G工厂 联通云 智算中心
热点云梦级4月21日云梦级消息,中国联通今日发布 2026 年第一季度主要财务及经营指标:营业收入达到人民币 1,028 亿元,同比下降 0.5%。税前利润达到人民币 61 亿元。公司权益持有者应占盈利达...
【热点】
阅读更多八张图,看懂数据分析如何驱动决策
热点本文转载自微信公众号「接地气学堂」,作者接地气的陈老师。转载本文请联系接地气学堂公众号。 数据驱动决策,是大家天天挂在嘴边的时髦词汇。可到底数据是如何驱动的?很少有同学真正看到过全流程。更有同学总疑惑...
【热点】
阅读更多想象一个人工智能无处不在的世界
热点想象一下,一个以你为模板、训练有素的机器人学会了思考、反应和行动。假如它要在你的生活(家庭或职场中)中扮演一个角色,面对道德困境,你相信它会做出正确的选择吗? 让更多人思考AI会如何影响未来生活 这是...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.takachi-enclosure.com/search?eqYB=gnI31Ou&text=谷歌霸屏软件【tg:seogoogle987】,8WS
- https://www2.ual.es/ProyectoLates/?s=谷歌霸屏软件【tg:seogoogle987】,011
- https://www.londontheatredirect.com/search/谷歌霸屏软件【tg:seogoogle987】,Ab3
- https://www.wou.edu.my/?s=谷歌霸屏软件【tg:seogoogle987】,lDJ
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=谷歌霸屏软件【tg:seogoogle987】,Xg7
- https://www.whirlpool.com.hk/chinese/catalogsearch/result?cat=30&q=谷歌霸屏软件【tg:seogoogle987】,4R0
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌霸屏软件【tg:seogoogle987】11L
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌霸屏软件【tg:seogoogle987】LBT
- https://www.tarleton.edu/?s=谷歌霸屏软件【tg:seogoogle987】,iBE
- https://www.simcoemuskokahealth.org/SearchResults.aspx?SearchQuery=谷歌霸屏软件【tg:seogoogle987】,Pea
- https://www.mexperience.com/?T=1686132112324&s=谷歌霸屏软件【tg:seogoogle987】,dBk
- https://blvdseafood.com/?s=谷歌霸屏软件【tg:seogoogle987】kzk
- https://www.mingwai.edu.hk/?langzh-hant&s=谷歌霸屏软件【tg:seogoogle987】,1eX
- https://www.lauben.com/en/?action=SEARCH&s=谷歌霸屏软件【tg:seogoogle987】,wUk
- https://www.pickett.co.uk/collections/vendors?q=谷歌霸屏软件【tg:seogoogle987】,asW
- https://www.shacknews.com/editorial-search?q=谷歌霸屏软件【tg:seogoogle987】,lie
- https://www.wheels24.gr/?action=CHECK&s=谷歌霸屏软件【tg:seogoogle987】,k6j
- https://www.stonerealestate.com.au/?cat=100&s=谷歌霸屏软件【tg:seogoogle987】,664
- https://www.lyrics.com/lyrics/kkk3谷歌霸屏软件【tg:seogoogle987】,qig
- https://www.wikiart.org/pt/Search/谷歌霸屏软件【tg:seogoogle987】,edF
- https://www.yli.ro/catalogsearch/result/?q=谷歌霸屏软件【tg:seogoogle987】,VAJ
- https://www.lneg.pt/?cat=140&s=谷歌霸屏软件【tg:seogoogle987】,STR
- https://www.terramania.nl/catalogsearch/result/index/?q=谷歌霸屏软件【tg:seogoogle987】,bzJ
- https://hsmoa.com/search?query=谷歌霸屏软件【tg:seogoogle987】0eZ
- https://www.umc.edu/search.aspx?ID=I4gnDKRS991MFcIDDzoYze4aWSAszAm7&q=谷歌霸屏软件【tg:seogoogle987】,K9C
- https://www.theaerogroup.net/?cat=28&s=谷歌霸屏软件【tg:seogoogle987】,d6O
- https://www.merchbar.com/search?q=谷歌霸屏软件【tg:seogoogle987】,GLR
- https://www.signingsavvy.com/sentence/谷歌霸屏软件【tg:seogoogle987】,tFB
- https://www.soccerbible.com/search?q=谷歌霸屏软件【tg:seogoogle987】,HAi
- https://www.vfw.org/search-results?q=谷歌霸屏软件【tg:seogoogle987】,OCC
- https://www.whiterock2008.com.tw/search?q=谷歌霸屏软件【tg:seogoogle987】,a9f
- https://www.kawainter.com/catalogsearch/result/?q=谷歌霸屏软件【tg:seogoogle987】,EE3
- https://www.olx.ba/pretraga?trazilica=谷歌霸屏软件【tg:seogoogle987】,GHJ
- https://www.remit.co.jp/ne/?s=谷歌霸屏软件【tg:seogoogle987】,jgt
- https://www.libros.unam.mx/catalogsearch/result/?cat=37&dir=asc&format=20&lit=20&manufacturer=3230&mode=list&order=year_edition&q=谷歌霸屏软件【tg:seogoogle987】,vMA
- https://www.remit.co.jp/vi/?s=谷歌霸屏软件【tg:seogoogle987】b21
- https://www.quarterjacksurgery.co.uk/?AT=3rBy&s=谷歌霸屏软件【tg:seogoogle987】,f3Z
- https://www.ushmm.org/search/results.php?q=谷歌霸屏软件【tg:seogoogle987】,1a3
- https://www.mynextmove.org/find/search?s=谷歌霸屏软件【tg:seogoogle987】,uvJ
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏软件【tg:seogoogle987】22F
- https://www.thedp.com/page/search?q=谷歌霸屏软件【tg:seogoogle987】,cFZ
- https://www.pearson.com/en-us/search.html?aq=谷歌霸屏软件【tg:seogoogle987】,C8A
- https://www.sogou.com/web?query=谷歌霸屏软件【tg:seogoogle987】,Jqz
- https://www.rotherham.gov.uk/site-search/results/?q=谷歌霸屏软件【tg:seogoogle987】,95e
- https://www.remit.co.jp/pt/?action=CHECK&s=谷歌霸屏软件【tg:seogoogle987】,b1z
- https://www.usslexington.com/?action=QUERY&s=谷歌霸屏软件【tg:seogoogle987】,rWl
- https://www.nextavenue.org/?action=QUERY&s=谷歌霸屏软件【tg:seogoogle987】,2KV
- https://www.prattlibrary.org/search?q=谷歌霸屏软件【tg:seogoogle987】,UYM
- https://www.searcychamber.com/list/search?AsfnA=DtkTQzs&q=谷歌霸屏软件【tg:seogoogle987】,YNc
- https://www.monroecounty.gov/search/?q=谷歌霸屏软件【tg:seogoogle987】,4Fb