当前位置: 首页 > news >正文

face_recognition人脸识别与人脸检测

1、安装face_recognition库

pip install face_recognition

face_recognition库的人脸识别是基于业内领先的C++开源库dlib中的深度学习模型,安装face_recognition库的同时会一并安装dlib深度学习框架。

2、face_recognition库的使用

1)load_image_file加载要识别的人脸图像

这个方法主要是用于加载要识别的人脸图像,返回的数据是numpy数组,记录了图片的所有像素的特征向量。

obama_image = face_recognition.load_image_file("ZXH.jpg")

2)face_locations定位图中所有人脸的像素位置

该方法返回值是一个列表形式,列表中每一行是一张人脸的位置信息,包括[top,right,bottom,left]。

import face_recognition
import cv2image = face_recognition.load_image_file("ZXH.jpg")
face_locations = face_recognition.face_locations(image)
for face_location in face_locations:top, right, bottom, left = face_location  star = (left, top)end = (right, bottom)cv2.rectangle(image, star, end, (0, 0, 255), thickness=2)
cv2.imshow('window', image)
cv2.waitKey()

3)face_encodings获取图像中所有面部编码信息

该方法返回值是一个编码列表,参数仍是要识别的图像对象。每张人脸的编码信息是一个128维向量。

import face_recognitionimage = face_recognition.load_image_file("ZXH.jpg")
face_encodings = face_recognition.face_encodings(image)
for face_encoding in face_encodings:print("信息编码长度为:{}\n编码信息为:{}".format(len(face_encoding), face_encoding))

4)compare_faces由面部编码信息进行面部识别匹配

该方法主要用于匹配两个面部特征编码,利用这两个特征向量的内积来衡量相似度,根据阈值确定是否是同一个人。

第一个参数是多张脸的面部编码列表,第二个参数是单张脸面部编码,compare_faces会将第二个参数中的编码信息与第一个参数中的所有编码信息依次匹配,返回一个布尔列表。

tolerance值可根据实际效果调整,值越小,匹配越严格。

import face_recognition# ZXH.jpg为多人合照
image1 = face_recognition.load_image_file("ZXH.jpg")
# ZXH1.jpg为单人照
image2 = face_recognition.load_image_file("ZXH1.jpg")
# 获取多人图片的面部编码信息
known_face_encodings = face_recognition.face_encodings(image1)# 要进行识别的单张图片的特征,只需要拿到第一个人脸的编码信息
compare_face_encoding = face_recognition.face_encodings(image2)[0]
# 注意第二个参数,只能是单个面部特征编码,不能列表
matches = face_recognition.compare_faces(known_face_encodings, compare_face_encoding,tolerance=0.39)print(matches)

5)face_distance获得比较面部的欧式距离

该方法给定面部编码列表,将其与已知面部编码比较,获得每个比较面部的欧式距离,距离就是面孔的相似程度,返回一个列表。

face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
# 参数:
# know_face_encodings:已知多目标编码列表
# face_encoding:要比较的人脸编码

完整代码

import face_recognition
import cv2
import numpy as np
import pyzed.sl as sl# Get a reference to webcam #0 (the default one)
# video_capture = cv2.VideoCapture(0)cam = sl.Camera()
init = sl.InitParameters()
init.camera_resolution = sl.RESOLUTION.HD720
init.depth_mode = sl.DEPTH_MODE.PERFORMANCE
status = cam.open(init)
if status != sl.ERROR_CODE.SUCCESS:print(repr(status))exit(1)runtime = sl.RuntimeParameters()
mat = sl.Mat()print("  Quit : CTRL+C\n")# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("ZXH.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("CW.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [obama_face_encoding,biden_face_encoding
]
known_face_names = ["ZXH","CW"
]# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:# Grab a single frame of video# ret, frame = video_capture.read()if (cam.grab(runtime) == sl.ERROR_CODE.SUCCESS):cam.retrieve_image(mat, sl.VIEW.LEFT)frame = mat.get_data()# 将图像从BGR格式转换为RGB格式# frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# Only process every other frame of video to save time# if process_this_frame:# Resize frame of video to 1/4 size for faster face recognition processingsmall_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)rgb_small_frame = small_frame[:, :, ::-1]cv2.imwrite("new.jpg", rgb_small_frame)rgb_small_frame = face_recognition.load_image_file("new.jpg")# Find all the faces and face encodings in the current frame of videoface_locations = face_recognition.face_locations(rgb_small_frame)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:# See if the face is a match for the known face(s)matches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# # If a match was found in known_face_encodings, just use the first one.# if True in matches:#     first_match_index = matches.index(True)#     name = known_face_names[first_match_index]# Or instead, use the known face with the smallest distance to the new faceface_distances = face_recognition.face_distance(known_face_encodings, face_encoding)best_match_index = np.argmin(face_distances)if matches[best_match_index]:name = known_face_names[best_match_index]face_names.append(name)process_this_frame = not process_this_frame# Display the resultsfor (top, right, bottom, left), name in zip(face_locations, face_names):# Scale back up face locations since the frame we detected in was scaled to 1/4 sizetop *= 4right *= 4bottom *= 4left *= 4# Draw a box around the facecv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# Draw a label with a name below the facecv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Display the resulting imagecv2.imshow('Video', frame)# Hit 'q' on the keyboard to quit!if cv2.waitKey(1) & 0xFF == ord('q'):break# Release handle to the webcam
cv2.destroyAllWindows()
cam.close()

问题1:

compute_face_descriptor(): incompatible function arguments. The following argument types are supported:1. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], face: _dlib_pybind11.full_object_detection, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vector2. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], num_jitters: int = 0) -> _dlib_pybind11.vector3. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], faces: _dlib_pybind11.full_object_detections, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectors...

解决办法:

改变尺寸后的图像无法进行编码,先将其保存成图片,再打开,即可进行编码。

# BGR2RGB
rgb_small_frame = small_frame[:, :, ::-1]
cv2.imwrite("new.jpg", rgb_small_frame)
rgb_small_frame = face_recognition.load_image_file("new.jpg")

参考链接 Github开源人脸识别项目face_recognition - 知乎

Python中因为浅拷贝导致调用face_recognition.encodings(img)出现的典型问题_vivisol的博客-CSDN博客

相关文章:

face_recognition人脸识别与人脸检测

1、安装face_recognition库 pip install face_recognition face_recognition库的人脸识别是基于业内领先的C开源库dlib中的深度学习模型,安装face_recognition库的同时会一并安装dlib深度学习框架。 2、face_recognition库的使用 1)load_image_file加…...

vue3获得url上的参数值

1、引入 import { useRoute } from vue-router2、获得const route useRoute() console.log(route.query.number)...

chapter15:springboot与监控管理

Spring Boot与监控管理视频 1. 简介 通过引入spring-boot-starter-actuator, 可以使用SpringBoot为我们提供的准生产环境下的应用监控和管理功能。我们可以通过http, jmx, ssh协议来进行操作,自动得到审计、健康及指标信息等。 步骤: 引入spring-boo…...

http历史版本

1,HTTP0.9 最早的http版本,后来才被定义为0.9版本。 这时候通信采用的是纯文本格式; 只支持get请求,且在服务器响应之后就关闭连接; 没有请求头的概念,功能比较简单。 2,HTTP1.0 这个版本增…...

【Go语言】Golang保姆级入门教程 Go初学者chapter2

【Go语言】变量 VSCode插件 setting的首选项 一个程序就是一个世界 变量是程序的基本组成单位 变量的使用步骤 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zuxG8imp-1691479164956)(https://cdn.staticaly.com/gh/hudiework/imgmain/image-20…...

关于ETL的两种架构(ETL架构和ELT架构) qt

ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract)、转换(transform)、加载(load)至目的端的过程。ETL一词较常用在数据仓库&#xf…...

【Linux】进程间通信——管道

目录 写在前面的话 什么是进程间通信 为什么要进行进程间通信 进程间通信的本质理解 进程间通信的方式 管道 System V IPC POSIX IPC 管道 什么是管道 匿名管道 什么是匿名管道 匿名管道通信的原理 pipe()的使用 匿名管道通信的特点 拓展代码 命名管道 什么是命…...

Element-plus中tooltip 提示框修改宽度——解决方案

tooltip 提示框修改宽度方法&#xff1a; 在element中&#xff0c;想要设置表格的内容&#xff0c;超出部分隐藏&#xff0c;鼠标悬浮提示 可以在el-table 上添加show-overflow-tooltip属性 同时可以通过tooltip-options配置提示信息 如下图代码 <el-tableshow-overflo…...

java实现当前系统时间格式化

import java.text.SimpleDateFormat; import java.util.Date;public class DateTest {public static void main(String[] args) {Date date new Date();System.out.println("当前系统时间&#xff1a;" date);SimpleDateFormat simpleDateFormat new SimpleDateFo…...

篇十一:享元模式:共享细粒度对象

篇十一&#xff1a;“享元模式&#xff1a;共享细粒度对象” 设计模式是软件开发中的重要工具&#xff0c;享元模式&#xff08;Flyweight Pattern&#xff09;是结构型设计模式的一种。享元模式旨在通过共享细粒度的对象&#xff0c;减少内存消耗和提高性能。在设计模式学习中…...

Dev控件 Gridcontrol,gridview 实现多选功能

在网上看了好多实现dev控件GridControl多选功能的方法&#xff0c;都很麻烦&#xff0c;其实GridControl有一个自带的实现多选功能的控件&#xff0c;很简单。 实现效果如下 无需代码代码&#xff0c;使用GridControl中自带的多选功能&#xff0c;在界面直接设置即可 1.找到要…...

内网穿透:如何通过公网访问本地Web服务器?

文章目录 前言1. 首先安装PHPStudy2.下载一个开源的网页文件3. 选择“创建网站”并将网页内容指向下载好的开源网页文件4. 打开本地网页5. 打开本地cpolar客户端6. 保存隧道设置 生成数据隧道 前言 随着科技进步和时代发展&#xff0c;计算机及互联网已经深深融入我们的生活和…...

在qemu中挂载镜像文件

将镜像文件作为交换分区 创建镜像文件&#xff1a; dd if/dev/zero ofswap.img bs512 count131072 qemu-system-arm -nographic -M vexpress-a9 -m 64M -kernel arch/arm/boot/zImage -append "rdinit/linuxrc consolettyAMA0 loglevel8" -dtb arch/arm/boot/dts/…...

报错注入(主键重复)攻击原理

基本原理 利用数据表中主键不能重复的特点&#xff0c;通过构造重复的主键&#xff0c;使得数据库报错&#xff0c;并将报错结果返回到前端。 SQL说明函数 以pet数据表为例进行说明 rond(): 返回[0,1)区间内的任意浮点数。 count(): 返回每个组的列行数。 如&#xff0…...

Golang基础教程

Golang基础教程 golang简介安装golanggolang开发工具go常用命令golang开发 vscode快捷键如何编写golang代码golang标识符、关键字、命名规则golang变量go语言常量go语言数据类型go语言布尔类型go语言数字类型golang字符串golang格式化输出golang运算符go语言中的流程控制golan…...

ppt压缩文件怎么压缩最小?文件压缩技巧分享

在日常的工作和学习中&#xff0c;难免会遇到PPT太大&#xff0c;需要将其压缩变小的情况&#xff0c;但很多朋友还不知道怎么压缩PPT文件&#xff0c;下面就给大家分享几个简单的方法&#xff0c;分分钟缩小过大的PPT文件。 一、PowerPoint PowerPoint就是微软公司的演示文稿…...

实例033 制作闪烁的窗体

实例说明 Windows系统中&#xff0c;当程序在后台运行时&#xff0c;如果某个窗口的提示信息需要用户浏览&#xff0c;该窗口就会不停的闪烁&#xff0c;这样就会吸引用户的注意。同样&#xff0c;如果在自己的程序中使某个窗口不停的闪烁就会吸引用户的注意。本例设计了一个闪…...

【JavaEE进阶】Spring创建与使用

文章目录 一. 创建 Spring 项目1.1 创建一个Maven项目1.2 添加Spring依赖1.4. 创建一个启动类 二. 将 Bean 对象存放至 Spring 容器中三. 从 Spring 容器中读取到 Bean1. 得到Spring对象2. 通过Spring 对象getBean方法获取到 Bean对象【DI操作】 一. 创建 Spring 项目 接下来使…...

PHP8的循环控制语句-PHP8知识详解

我们在上一节讲的是条件控制语句&#xff0c;本节课程我们讲解循环控制语句。循环控制语句中&#xff0c;主要有for循环、while循环、do...while循环和foreach循环。 在编写代码时&#xff0c;经常需要反复运行同一代码块。我们可以使用循环来执行这样的任务&#xff0c;而不是…...

第八次作业

一&#xff0c;.什么是数据认证&#xff0c;有什么作用&#xff0c;有哪些实现的技术手段? 数据认证是指保证数据的真实性、完整性和可信度&#xff0c;以确保数据不被篡改或伪造。其作用包括但不限于&#xff1a; 保护关键数据不被恶意篡改或损坏 提供数据来源的可靠性和安全…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查

在对接支付宝API的时候&#xff0c;遇到了一些问题&#xff0c;记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...

Cesium1.95中高性能加载1500个点

一、基本方式&#xff1a; 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

循环冗余码校验CRC码 算法步骤+详细实例计算

通信过程&#xff1a;&#xff08;白话解释&#xff09; 我们将原始待发送的消息称为 M M M&#xff0c;依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)&#xff08;意思就是 G &#xff08; x ) G&#xff08;x) G&#xff08;x) 是已知的&#xff09;&#xff0…...

visual studio 2022更改主题为深色

visual studio 2022更改主题为深色 点击visual studio 上方的 工具-> 选项 在选项窗口中&#xff0c;选择 环境 -> 常规 &#xff0c;将其中的颜色主题改成深色 点击确定&#xff0c;更改完成...

[10-3]软件I2C读写MPU6050 江协科技学习笔记(16个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...

如何为服务器生成TLS证书

TLS&#xff08;Transport Layer Security&#xff09;证书是确保网络通信安全的重要手段&#xff0c;它通过加密技术保护传输的数据不被窃听和篡改。在服务器上配置TLS证书&#xff0c;可以使用户通过HTTPS协议安全地访问您的网站。本文将详细介绍如何在服务器上生成一个TLS证…...

ETLCloud可能遇到的问题有哪些?常见坑位解析

数据集成平台ETLCloud&#xff0c;主要用于支持数据的抽取&#xff08;Extract&#xff09;、转换&#xff08;Transform&#xff09;和加载&#xff08;Load&#xff09;过程。提供了一个简洁直观的界面&#xff0c;以便用户可以在不同的数据源之间轻松地进行数据迁移和转换。…...

Neo4j 集群管理:原理、技术与最佳实践深度解析

Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...

论文浅尝 | 基于判别指令微调生成式大语言模型的知识图谱补全方法(ISWC2024)

笔记整理&#xff1a;刘治强&#xff0c;浙江大学硕士生&#xff0c;研究方向为知识图谱表示学习&#xff0c;大语言模型 论文链接&#xff1a;http://arxiv.org/abs/2407.16127 发表会议&#xff1a;ISWC 2024 1. 动机 传统的知识图谱补全&#xff08;KGC&#xff09;模型通过…...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...