本项目是世界上最简洁的人脸识别库,你可以使用Python和命令行工具提取、识别、操作人脸。
本项目的人脸识别是基于业内领先的C++开源库 dlib中的深度学习模型,用Labeled Faces in the Wild人脸数据集进行测试,有高达99.38%的准确率。但对小孩和亚洲人脸的识别准确率尚待提升。
Labeled Faces in the Wild是美国麻省大学安姆斯特分校(University of Massachusetts Amherst)制作的人脸数据集,该数据集包含了从网络收集的13,000多张面部图像。
本项目提供了简易的face_recognition命令行工具,你可以用它处理整个文件夹里的图片。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File : main.py
@Time : 2023/09/25 15:30:22
@Author : Carry
@Version : 1.0
@Desc : 人脸识别、人脸比对
'''
import time
import cv2
import sys
import face_recognition
import gradio as gr
def draw_face_rectangles(image, locations):
"""
# 定义一个函数来绘制人脸区域
"""
for location in locations:
top, right, bottom, left = location
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
def find_largest_face(face_locations):
"""
# 找到每张图片中最大的人脸
"""
if not face_locations:
return None
largest_face = face_locations[0]
largest_area = (largest_face[2] - largest_face[0]) * (largest_face[1] - largest_face[3])
for face_location in face_locations:
area = (face_location[2] - face_location[0]) * (face_location[1] - face_location[3])
if area > largest_area:
largest_area = area
largest_face = face_location
return largest_face
def is_same_person(image1, image2, local=False, save_img=False):
"""
# 判断两张图片中的人脸是否是同一个人
"""
if local:
# 加载两张图片
image1 = face_recognition.load_image_file(image1)
image2 = face_recognition.load_image_file(image2)
# 在每张图片中找到所有人脸的面部位置
t1 = time.time()
face_locations1 = face_recognition.face_locations(image1)
face_locations2 = face_recognition.face_locations(image2)
t2 = time.time()
print("face location time:", t2-t1)
if save_img:
# 绘制人脸区域并保存图像
image1_with_faces = image1.copy()
draw_face_rectangles(image1_with_faces, face_locations1)
cv2.imwrite("image1_with_faces.jpg", image1_with_faces)
image2_with_faces = image2.copy()
draw_face_rectangles(image2_with_faces, face_locations2)
cv2.imwrite("image2_with_faces.jpg", image2_with_faces)
# 找到每张图片中最大的人脸
largest_face1 = find_largest_face(face_locations1)
largest_face2 = find_largest_face(face_locations2)
print(f"largest face1: {largest_face1}")
print(f"largest face2: {largest_face2}")
if largest_face1 is None or largest_face2 is None:
return "未能找到图片中的人脸"
else:
# 提取人脸特征
t1 = time.time()
face_encodings1 = face_recognition.face_encodings(image1, [largest_face1], num_jitters=1, model="large")[0]
face_encodings2 = face_recognition.face_encodings(image2, [largest_face2], num_jitters=1, model="large")[0]
t2 = time.time()
print("face encoding time:", t2-t1)
face_distances = face_recognition.face_distance([face_encodings1], face_encodings2)
print(f"face distance: {face_distances}")
results = face_recognition.compare_faces([face_encodings1], face_encodings2, tolerance=0.4)
print(f"compare faces: {results}")
if results[0]:
return f"这两张图片中的人脸是同一个人。score(越趋近0越相似,当前阈值0.4):{face_distances[0]}"
else:
return f"这两张图片中的人脸不是同一个人。score(越趋近0越相似,当前阈值0.4):{face_distances[0]}"
# 接收命令行输入的图片路径
# pic_1 = sys.argv[1]
# pic_2 = sys.argv[2]
# t1 = time.time()
# is_same_person(pic_1, pic_2, local=True)
# t2 = time.time()
# print("total time:", t2-t1)
# 创建 Gradio 交互式界面
iface = gr.Interface(fn=is_same_person, inputs=["image", "image"], outputs="text")
iface.launch(server_name="0.0.0.0")
python main.py
http://localhost:7861/