python人脸识别库_用python库face_recognition进行人脸识别

本文介绍了如何利用python的face_recognition库进行人脸识别。首先安装opencv和face_recognition库,然后通过摄像头捕获视频,实时检测并识别人脸。通过加载预先准备好的人脸图像,将捕获的帧中的人脸与已知人脸进行比对,匹配成功后在屏幕上显示匹配到的名字。代码中还包含了处理中文字符的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.pip install opencv

2.pip install face_recognition

3.通过摄像头实时在获取的帧上进行人脸识别(较卡顿)

facerecognition.py

# -*- coding: UTF-8 -*-

import face_recognition

import cv2

import os

import ft2

#中文支持,加载微软雅黑字体

ft = ft2.put_chinese_text('msyh.ttf')

# 获取摄像头# 0(默认)

video_capture = cv2.VideoCapture(0)

# 加载待识别人脸图像并识别。

basefacefilespath ="images"#faces文件夹中放待识别任务正面图,文件名为人名,将显示于结果中

baseface_titles=[] #图片名字列表

baseface_face_encodings=[] #识别所需人脸编码结构集

#读取人脸资源

for fn in os.listdir(basefacefilespath): #fn 人脸文件名

baseface_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file(basefacefilespath+"/"+fn))[0])

fn=fn[:(len(fn)-4)]

baseface_titles.append(fn)#

while True:

# 获取一帧视频

ret, frame = video_capture.read()

# 人脸检测,并获取帧中所有人脸编码

face_locations = face_recognition.face_locations(frame)

face_encodings = face_recognition.face_encodings(frame, face_locations)

# 遍历帧中所有人脸编码

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):

# 与baseface_face_encodings匹配否?

for i,v in enumerate(basef

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值