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

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

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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于Python的OpenCV和face_recognition的简单人脸识别代码示例: ```python import cv2 import face_recognition # 加载已知的人脸图像和对应的名字 obama_image = face_recognition.load_image_file("obama.jpg") obama_face_encoding = face_recognition.face_encodings(obama_image)[0] biden_image = face_recognition.load_image_file("biden.jpg") biden_face_encoding = face_recognition.face_encodings(biden_image)[0] known_face_encodings = [ obama_face_encoding, biden_face_encoding ] known_face_names = [ "Barack Obama", "Joe Biden" ] # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取一帧图像 ret, frame = cap.read() # 转换为RGB图像 rgb_frame = frame[:, :, ::-1] # 检测人脸 face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) # 遍历每个检测到的人脸 for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # 判断是否和已知人脸匹配 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # 如果匹配到了已知人脸,则获取对应的名字 if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] # 在图像上绘制人脸矩形和名字 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) # 显示图像 cv2.imshow('Video', frame) # 按下q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头和窗口资源 cap.release() cv2.destroyAllWindows() ``` 注意,这个代码示例需要在已经安装了face_recognition和OpenCVPython环境中运行,还需要把`obama.jpg`和`biden.jpg`两个已知人脸图像放在代码所在目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值