当前位置:网站首页>人脸识别 face_recognition
人脸识别 face_recognition
2022-07-06 09:14:00 【imxlw00】
Github开源人脸识别项目face_recognition,face_recognition是一个强大、简单、易上手的人脸识别开源项目
安装
pip install face_recognition
导入face_recognition模块
import face_recognition
load_image_file 加载图像
调用face_recognition.load_image_file()读入图像。输出图像是rgb格式(opencv中是bgr格式)
import face_recognition
image = face_recognition.load_image_file("../datas/obama.jpg")
face_locations 人脸检测
能定位所有图像中识别出的人脸位置信息,返回值是列表形式,列表中每一行是一张人脸的位置信息,包括[top, right, bottom, left]
import face_recognition
import cv2
#加载图像文件
image = face_recognition.load_image_file("../datas/hz.png")
image2 = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
cv2.imshow("src",image2)
face_locations = face_recognition.face_locations(image)
# 循环找到的所有人脸
for face_location in face_locations:
# 打印每张脸的位置信息
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人脸的位置信息,然后显示人脸图片
# face_image = image2[top:bottom, left:right]
# cv2.imshow("src2", face_image)
# 矩形框
cv2.rectangle(image2, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow("dest",image2)
cv2.waitKey(0)

face_landmarks() 识别人脸关键点
调用face_recognition.face_landmarks(image)可识别出人脸关键点信息,包括眼睛、鼻子、嘴巴和下巴等
返回值是包含面部特征字典的列表,列表中每一项对应一张人脸,包括nose_bridge、right_eyebrow、right_eye、chine、left_eyebrow、bottom_lip、nose_tip、top_lip、left_eye几个部分,每个部分包含若干个特征点(x,y),总共有68个特征点。
import cv2
import face_recognition
#加载图像文件
image = face_recognition.load_image_file("images/ldh2.jpg")
image2 = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
face_landmarks_list = face_recognition.face_landmarks(image)
facial_features = [
'chin',
'left_eyebrow',
'right_eyebrow',
'nose_bridge',
'nose_tip',
'left_eye',
'right_eye',
'top_lip',
'bottom_lip'
]
thickness = 2
point_size = 1
point_color = (0, 255, 0) # BGR
for face_landmarks in face_landmarks_list:
for facial_feature in face_landmarks:
points_list = face_landmarks[facial_feature]
print("The {} in this face has the following points: {}".format(facial_feature, points_list))
# 在图像中画出每个人脸特征!
for point in points_list:
cv2.circle(image2, point, point_size, point_color, thickness)
cv2.imshow("dest",image2)
cv2.waitKey(0)

face_encodings() 面部编码
获取每个图像文件中每个面部的面部编码,每张人脸是一个128维的向量
import face_recognition
#加载图像文件
image = face_recognition.load_image_file("images/ldh2.jpg")
face_encodings = face_recognition.face_encodings(image)
for face_encoding in face_encodings:
print("face_encoding len = {} \nencoding:{}\n\n".format(len(face_encoding),face_encoding))
face_encoding len = 128
encoding:[-0.12857245 0.2251953 -0.05680346 -0.009356 -0.07961649 -0.01976449
-0.03006363 -0.2188953 0.18227896 -0.06380306 0.23052536 -0.00517259
-0.24484953 -0.1024491 -0.07127093 0.10542907 -0.15712184 -0.24606238
-0.04181587 0.01180026 0.09390671 0.01014723 0.03316356 0.08216273
-0.08543567 -0.27527595 -0.10761252 -0.08561324 0.09562894 -0.06388975
-0.07860459 -0.01040951 -0.21186014 -0.10162984 0.09300554 0.08929634
-0.01133628 -0.02549033 0.25640246 -0.03459882 -0.22288607 0.06690408
0.05515105 0.31292963 0.14994277 0.06461242 0.00211182 -0.18974975
0.11956067 -0.07477805 0.09382927 0.21616243 0.18040094 0.02733615
0.03574533 -0.20213988 -0.00134893 0.08189995 -0.1417242 -0.00179647
0.10290838 -0.06461908 -0.01702856 -0.11400557 0.20824584 0.04568703
-0.07567319 -0.24240926 0.12750657 -0.16503944 -0.10546687 0.14743967
-0.13195604 -0.13922986 -0.26378733 -0.00479815 0.42761648 0.13261758
-0.17136547 0.09223576 -0.04313177 -0.03386433 0.07099032 0.1085121
-0.10494502 0.06504983 -0.15515642 -0.00395807 0.2313281 -0.01442198
-0.08458654 0.14688034 -0.00217457 0.07351732 0.06130501 0.01452726
-0.10001963 0.01601498 -0.12459313 -0.06574792 0.00460516 -0.01154638
-0.00899489 0.13434561 -0.14506203 0.06423949 -0.01264461 0.03912276
-0.06142928 0.01014899 -0.07765882 -0.04298633 0.10122736 -0.29780281
0.25160897 0.09939459 0.02078611 0.08443137 0.10742732 0.06015015
-0.00302691 -0.00466454 -0.20120506 -0.10478879 0.12094288 -0.0389271
0.19183818 -0.00275789]
compare_faces() 人脸匹配
compare_faces()方法可匹配两个面部特征编码,利用两个向量的内积来衡量相似度,根据阈值确认是否是同一人脸
第一个参数给出一个面部编码列表(很多张脸)
第二个参数给出单个面部编码(一张脸)
默认参数:tolerance=0.6,可根据自己需求更改,tolerance越小匹配越严格
匹配成功的脸返回True,匹配失败的返回False,顺序与第一个参数中脸的顺序一致
matches ==face_recognition.compare_faces(known_face_encodings,face_encoding,tolerance=0.39)
import face_recognition
# 加载已知面孔的图片
known_image = face_recognition.load_image_file("images/yz1.jpg")
# 加载未知面孔的图片
unknown_image = face_recognition.load_image_file("images/yz2.jpg")
# 加载2张已知面孔的图片
ldh_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
# 计算未知图片与已知图片的距离
results = face_recognition.compare_faces([ldh_encoding], unknown_encoding,tolerance=0.39)[0]
print(results)

参考链接:https://blog.csdn.net/weixin_42213622/article/details/108193123
边栏推荐
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- Antlr4 uses keywords as identifiers
- AcWing 242. A simple integer problem (tree array + difference)
- Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
- [download app for free]ineukernel OCR image data recognition and acquisition principle and product application
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
- [蓝桥杯2017初赛]包子凑数
- In the era of DFI dividends, can TGP become a new benchmark for future DFI?
- How to set up voice recognition on the computer with shortcut keys
- MySQL的一些随笔记录
猜你喜欢
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP

One click extraction of tables in PDF

Leetcode 461 Hamming distance
![[Thesis Writing] how to write function description of jsp online examination system](/img/f8/13144e0febf4a576bbcc3290192079.jpg)
[Thesis Writing] how to write function description of jsp online examination system

When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
![[number theory] divisor](/img/ec/036d7e76cc566c08d336444f2898e1.jpg)
[number theory] divisor

QT creator design user interface

How to build a new project for keil5mdk (with super detailed drawings)

软件测试与质量学习笔记3--白盒测试

Did you forget to register or load this tag 报错解决方法
随机推荐
安装numpy问题总结
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
AcWing 1298.曹冲养猪 题解
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Pytorch基础
How to build a new project for keil5mdk (with super detailed drawings)
Swagger、Yapi接口管理服务_SE
MySQL other hosts cannot connect to the local database
Base de données Advanced Learning Notes - - SQL statements
Leetcode 461 Hamming distance
[number theory] divisor
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
Ansible实战系列三 _ task常用命令
Use dapr to shorten software development cycle and improve production efficiency
[recommended by bloggers] C WinForm regularly sends email (with source code)
【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)
Picture coloring project - deoldify
La table d'exportation Navicat génère un fichier PDM
MySQL master-slave replication, read-write separation
Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path