python人脸识别库_用python库face_recognition进行人脸识别
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(baseface_face_encodings):
match = face_recognition.compare_faces([v], face_encoding,tolerance=0.5)
name = "?"
if match[0]:
name = baseface_titles[i]
break
name=unicode(name,'gb2312')#gbk is also ok.
print(name)
# 围绕脸的框
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# 框下的名字(即,匹配的图片文件名)
cv2.rectangle(frame, (left, bottom), (right, bottom+35), (0, 0, 255), cv2.FILLED)
frame = ft.draw_text(frame, (left + 2, bottom + 12), name, 16, (255, 255, 255))
# show结果图像
cv2.imshow('Video', frame)
# 按q退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头中的流
video_capture.release()
cv2.destroyAllWindows()
ft2.py
# -*- coding: utf-8 -*-
import numpy as np
import freetype
import copy
import pdb
class put_chinese_text(object):
def __init__(self, ttf):
self._face = freetype.Face(ttf)
def draw_text(self, image, pos, text, text_size, text_color):
'''
draw chinese(or not) text with ttf
:param image: image(numpy.ndarray) to draw text
:param pos: where to draw text
:param text: the context, for chinese should be unicode type
:param text_size: text size
:param text_color:text color
:return: image
'''
self._face.set_char_size(text_size * 64)
metrics = self._face.size
ascender = metrics.ascender/64.0
#descender = metrics.descender/64.0
#height = metrics.height/64.0
#linegap = height - ascender + descender
ypos = int(ascender)
if not isinstance(text, unicode):
text = text.decode('utf-8')
img = self.draw_string(image, pos[0], pos[1]+ypos, text, text_color)
return img
def draw_string(self, img, x_pos, y_pos, text, color):
'''
draw string
:param x_pos: text x-postion on img
:param y_pos: text y-postion on img
:param text: text (unicode)
:param color: text color
:return: image
'''
prev_char = 0
pen = freetype.Vector()
pen.x = x_pos << 6 # div 64
pen.y = y_pos << 6
hscale = 1.0
matrix = freetype.Matrix(int(hscale)*0x10000L, int(0.2*0x10000L),\
int(0.0*0x10000L), int(1.1*0x10000L))
cur_pen = freetype.Vector()
pen_translate = freetype.Vector()
image = copy.deepcopy(img)
for cur_char in text:
self._face.set_transform(matrix, pen_translate)
self._face.load_char(cur_char)
kerning = self._face.get_kerning(prev_char, cur_char)
pen.x += kerning.x
slot = self._face.glyph
bitmap = slot.bitmap
cur_pen.x = pen.x
cur_pen.y = pen.y - slot.bitmap_top * 64
self.draw_ft_bitmap(image, bitmap, cur_pen, color)
pen.x += slot.advance.x
prev_char = cur_char
return image
def draw_ft_bitmap(self, img, bitmap, pen, color):
'''
draw each char
:param bitmap: bitmap
:param pen: pen
:param color: pen color e.g.(0,0,255) - red
:return: image
'''
x_pos = pen.x >> 6
y_pos = pen.y >> 6
cols = bitmap.width
rows = bitmap.rows
glyph_pixels = bitmap.buffer
for row in range(rows):
for col in range(cols):
if glyph_pixels[row*cols + col] != 0:
img[y_pos + row][x_pos + col][0] = color[0]
img[y_pos + row][x_pos + col][1] = color[1]
img[y_pos + row][x_pos + col][2] = color[2]
if __name__ == '__main__':
# just for test
import cv2
line = '你好'
img = np.zeros([300,300,3])
color_ = (0,255,0) # Green
pos = (3, 3)
text_size = 24
#ft = put_chinese_text('wqy-zenhei.ttc')
ft = put_chinese_text('msyh.ttf')
image = ft.draw_text(img, pos, line, text_size, color_)
cv2.imshow('ss', image)
cv2.waitKey(0)
将msyh.ttf 微软雅黑字体文件copy到同级目录,在同级images文件夹下,以人名命名正面脸图.
运行, python facerecognition.py
卡顿,跳帧,
结果如图:
base faces
识别:
仅图像检测(不识别)
python人脸识别库_用python库face_recognition进行人脸识别相关推荐
- python人脸识别库_基于Python的face_recognition库实现人脸识别
Python Python开发 Python语言 基于Python的face_recognition库实现人脸识别 一.face_recognition库简介 face_recognition是Pyt ...
- python判断题题库_《Python程序设计》判断题1-240题
1.Python是一种跨平台.开源.免费的高级动态编程语言.(对) 2.Python 3.x完全兼容Python 2.x.(错) 3.Python 3.x和Python 2.x唯一的区别就是:prin ...
- python urllib3离线安装_全球Python库下载前10名
Python的简洁性,不仅仅在于其语法简单,还有各种python库函数的支持,为大家节省了大量的时间和精力,所以网上有人戏称python的编程者为调包侠.但是你知道全球最受欢迎的python库嘛?今天 ...
- c调用python第三方库_用 Python ctypes 来调用 C/C++ 编写的第三方库
看到一篇简洁的文章--如何用Python ctypes调用C++(ctypes调用C没这么多麻烦事),不敢独享... 如果需要用 Python 调用 C/C++ 编写的第三方库(这些第三方库很可能就是 ...
- python package安装包_安装Python包(第三方库)
检查python版本 如果"提示 'python' 不是内部或外部命令,也不是可运行的程序或批处理文件.",可能有2种原因 1.进入的目录不对 (切换目录后再查看) C:\User ...
- css 解析 开源库_干货 | python库大全,全面高效
黑客技术点击右侧关注,了解黑客的世界! Java开发进阶点击右侧关注,掌握进阶之路! Python开发点击右侧关注,探讨技术话题!来源丨速学Pythonhttps://mp.weixin.qq.com ...
- python cgi库_《Python 数据库 GUI CGI编程》
1. 上次写在前面,我们介绍了一篇关于开始使用Python.今天我们将介绍Python数据库,GUI, CGI编程和Python和Python的区别.2.连接到数据库标准Python数据库接口是Pyt ...
- python批量安装第三方库_使用Python批量安装第三方库
Python的很多功能通过第三方库实现,99%的第三方库可以通过Python自带的pip方法进行自动下载和安装.然而Python有几十万个第三方库,最常用的也有几十个.想要一次性地安装几十个常用的第三 ...
- python处理时间的标准函数库_使用Python的datetime库处理时间(RPA流程)
RPA流程自动化过程中,遇到时间的相关操作时,可以调用datetime库的一些方法进行处理. datetime 是 Python 处理日期和时间的标准库. 1.获取当前日期和时间 我们先看如何获取当前 ...
- python常用的几个库_盘点Python常用的20个库
1.Requests.最着名的http库由kenneth reitz编写.这是每个python开发人员必备的. 2.Scrapy.如果您参与webscraping,那么这是一个必备的库.使用此库后,您 ...
最新文章
- 刚进园子,广州的冬天像夏天
- 浏览器默认标签样式总结及css初始化程序(转)
- 百度咋做长文本去重(一分钟系列)--转
- 深入探索并发编程之内存屏障:资源控制操作
- Java是值传递还是引用传递?
- 页面复杂对象传递参数 开发中遇到的问题
- C++编程--函数与委托(2)
- android 得到目录,android 获取路径的获取
- 产品工作中/阅读中的涓滴意念
- 美国AI出口限令倒计时,硅谷直喊“下手轻点”,担心推动中国技术发展
- voices——最后的决赛
- 突击Mercurial SCM(HG)3---撤销操作
- DirectShow介绍-1
- Excel·VBA按列拆分工作表、工作簿
- ios手机页面滑动卡顿问题
- 枸杞功效多,这样吃效果翻倍
- Kafka之消费全流程
- 计算机网络自顶向下第一章知识点总结(上)
- liunx常驻运行项目命令
- coding随笔 | 点绕向量旋转
热门文章
- python代码运行助手下载_Python自学:使用代码运行助手
- Java API 中文版 谷歌翻译 拿走不谢 jdk api 1.8_google
- 固高板卡mct2008调试轴回零_MCT2008-for-ges 固高所有GE运动控制卡的DEMO软件,用来测试板卡是否正常工作 matlab 238万源代码下载- www.pudn.com...
- 全球经典《深入解析Windows 操作系统,第4 版》4月19日全国发货!
- 服务器主板能插几块硬盘,如何知道自己的主板最大支持多少的硬盘啊
- xp大容量u盘补丁_大容量硬盘补丁
- verifycode.php,verifycode.php
- linux下QQ配置文件路径,Linux下QQ的使用并手动设置QQ文件保存路径
- 修改Linux窗口大小
- java1.4.2中文下载_j2sdk1.4.2-j2sdk下载1.4.2_19官方安装版-西西软件下载