python人脸识别库_基于Python的face_recognition库实现人脸识别

Python

Python开发

Python语言

基于Python的face_recognition库实现人脸识别

CgpOIF5EAWWAQ0y_AAcB-fDz9P0878.png

一、face_recognition库简介

1880991-20200217165757510-1268660808.png

face_recognition是Python的一个开源人脸识别库,支持Python 3.3+和Python 2.7。引用官网介绍:

Recognize and manipulate faces from Python or from the command line with the world's simplest face recognition library.

之所以选用这个库,是因为

1、用这个库来实现一个人脸识别程序非常简单,环境配置也很容易;

2、可以直接使用已经训练好的模型,不需要在本地重新训练。一般普通的电脑都可以直接运行识别程序,硬件环境要求不高。

PS:很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为此我整理啦从基础的python脚本到web开发、爬虫、django、数据挖掘等【PDF等】需要的可以进Python全栈开发交流.裙 :一久武其而而流一思(数字的谐音)转换下可以找到了,里面有最新Python教程项目可拿,不懂的问题有老司机解决哦,一起相互监督共同进步

二、环境安装

我自己的环境如下:

硬件:08年的笔记本电脑,奔腾双核,算是比较低端的笔记本了

系统:win7x64

python:3.6 (注意:建议用3.6版本配置环境。我自己用3.7配置环境失败了,dlib安装总是失败。)

用3.6安装的过程比较简单,可以参考https://www.jianshu.com/p/8296f2aac1aa

用pip安装之前,注意首先修改pip为阿里的源,这样速度就快多了。

三、代码实现

import face_recognition

import cv2

import os

import numpy as np

from PIL import Image,ImageDraw,ImageFont

#路径参数配置

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

destfacefilepath = "0d" #用于识别的目标图片目录

#写入中文字符支持

def paint_chinese_opencv(im, chinese, pos, color):

img_PIL = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))

font = ImageFont.truetype('simsun.ttc', 14)

fillColor = color # (255,0,0)

position = pos # (100,100)

#chinese = chinese.decode('utf-8')

draw = ImageDraw.Draw(img_PIL)

draw.text(position, chinese, font=font, fill=fillColor)

img = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)

return img

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

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.split("_")[1]

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

baseface_titles.append(fn)

print(fn)

#从识别库中读取一张图片并识别

for fd in os.listdir(destfacefilepath):

# 获取一张图片

faceData = face_recognition.load_image_file(destfacefilepath + "/" + fd)

print(fd)

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

face_locations = face_recognition.face_locations(faceData)

face_encodings = face_recognition.face_encodings(faceData, face_locations)

# 遍历图片中所有人脸编码

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

# 与baseface_face_encodings匹配否?

name = "?"

for i,v in enumerate(baseface_face_encodings):

match = face_recognition.compare_faces([v], face_encoding,tolerance=0.5)

name = "?"

if match[0]:

name = baseface_titles[i]

print("识别出:" + name)

break

#如果遇到没有识别出的人脸,则跳过

if name == "?":

continue

# 围绕脸的框

cv2.rectangle(faceData, (left, top), (right, bottom), (0, 0, 255), 2)

# 框下的名字(即,匹配的图片文件名)

cv2.rectangle(faceData, (left, bottom), (right, bottom+25), (0, 0, 255), cv2.FILLED)

#faceData = cv2.putText(faceData, name,(left + 2, bottom + 12), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255),1)

faceData = paint_chinese_opencv(faceData, name, (left + 2, bottom + 4), (255, 255, 255))

# frame = ft.draw_text(frame, (left + 2, bottom + 12), name, 16, (255, 255, 255))

# show结果图像

cv2.imshow(fd, cv2.cvtColor(faceData, cv2.COLOR_BGR2RGB))

cv2.waitKey()

cv2.destroyAllWindows()

图片分为两个文件夹:0s和0d

0s中存放的是基础人脸数据

0d文件夹中存放的是接下来要进行识别的图片,我这里放了3张:

四、运行效果

运行结果:

五、问题总结

1、cv2.imshow,这个最开始显示的图片颜色不对,后来修改如下:

cv2.imshow(fd, cv2.cvtColor(faceData, cv2.COLOR_BGR2RGB))

将图片的颜色模式调整为RGB,然后色彩就正常了。

2、中文字符支持,最开始用 cv2.putText,但是这个函数只支持英文字符,中文字符会显示问号乱码。

后加入中文字符支持函数,用 PIL 库中的 ImageDraw 来写入中文字符。

总结:很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为此我整理啦从基础的python脚本到web开发、爬虫、django、数据挖掘等【PDF等】需要的可以进Python全栈开发交流.裙 :一久武其而而流一思(数字的谐音)转换下可以找到了,里面有最新Python教程项目可拿,不懂的问题有老司机解决哦,一起相互监督共同进步

本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

内容来源于网络,如有侵权请联系客服删除

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值