Coding-opencv-人脸识别

摘要

基于OpenCV与face_recognition的人脸识别。(怎么会有这么方便的人脸识别库)

程序一:人脸识别与比较

步骤:

  • 加载与转换图像: 使用face_recognition.load_image_file加载图片img/11.jpgimg/22.jpg,并通过cv2.cvtColor将其从BGR格式转换为RGB。
  • 人脸定位与特征编码: 利用face_recognition.face_locations定位图片中的人脸位置,并使用face_recognition.face_encodings提取这些区域的特征编码。
  • 绘制人脸矩形框: 在原图上为检测到的人脸绘制矩形框,使用cv2.rectangle函数实现。
  • 相似度比较与显示: 通过face_recognition.compare_faces比较两个人脸编码的相似度,并利用face_recognition.face_distance计算欧氏距离,展示比较结果及距离值于图像上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import cv2
import numpy as np
import face_recognition

imgElon = face_recognition.load_image_file('img/11.jpg') # 加载图片
imgElon = cv2.cvtColor(imgElon, cv2.COLOR_BGR2RGB) # 将BGR彩色图像转化为RGB彩色图像

faceLoc = face_recognition.face_locations(imgElon)[0] # 定位人脸位置
encodeElon = face_recognition.face_encodings(imgElon)[0] # 提取人脸的面部特征
cv2.rectangle(imgElon, (faceLoc[3], faceLoc[0]), (faceLoc[1], faceLoc[2]), (255, 0, 255), 2) # 框出人脸

imgTest = face_recognition.load_image_file('img/22.jpg')
imgTest = cv2.cvtColor(imgTest, cv2.COLOR_BGR2RGB)

# 原始的人脸位置
faceLocTest = face_recognition.face_locations(imgTest)[0]

# 计算矩形框的宽度和高度
width = faceLocTest[1] - faceLocTest[3]
height = faceLocTest[2] - faceLocTest[0]

# 确保扩展的大小不会让矩形超出图像边界
expand_width = min(width, imgTest.shape[1] - faceLocTest[1])
expand_height = min(height, imgTest.shape[0] - faceLocTest[2])

# 调整矩形框的坐标以扩大边界,同时确保不超出图像边界
top_left = (max(0, faceLocTest[3] - expand_width), max(0, faceLocTest[0] - expand_height))
bottom_right = (min(imgTest.shape[1], faceLocTest[1] + expand_width), min(imgTest.shape[0], faceLocTest[2] + expand_height))

# 绘制调整后的矩形框
encodeTest = face_recognition.face_encodings(imgTest)[0]
cv2.rectangle(imgTest, top_left, bottom_right, (255, 0, 255), 2)


result = face_recognition.compare_faces([encodeElon], encodeTest) # 比较人脸编码的相似度
faceDis = face_recognition.face_distance([encodeElon], encodeTest) # 计算两个人脸的欧氏距离(欧氏距离用于计算样本之间的相似度或距离)
print(result, faceDis)
cv2.putText(imgTest, f'{result}{round(faceDis[0], 2)}', (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2) # 显示比对结果

cv2.imshow('Elon Musk', imgElon)
cv2.imshow('Elon Test', imgTest)
key = cv2.waitKey(0)
if key == 27: # 按ESC键退出
cv2.destroyAllWindows()

程序二:扩大矩形框范围函数封装

expand_and_draw_face_box函数:

  • 扩展矩形框: 对给定的测试图像,该函数不仅定位人脸,还智能地扩大检测框,保证扩展后不会超出图像边界。
  • 自定义逻辑: 实现了更精细的控制,通过计算安全的扩展尺寸,避免矩形框越界问题。
  • 结果输出: 函数直接在图像上绘制了调整后的矩形框,并可通过打印返回的矩形顶点坐标来验证效果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import cv2
import numpy as np
import face_recognition

def expand_and_draw_face_box(imgTest):
try:
# 检测人脸位置
faceLocTest = face_recognition.face_locations(imgTest)[0]

# 计算矩形框的宽度和高度
width = faceLocTest[1] - faceLocTest[3]
height = faceLocTest[2] - faceLocTest[0]

# 确保扩展的大小不会超出图像边界
expand_width = min(width, imgTest.shape[1] - faceLocTest[1])
expand_height = min(height, imgTest.shape[0] - faceLocTest[2])

# 绘制调整后的矩形框
top_left = (max(0, faceLocTest[3] - expand_width), max(0, faceLocTest[0] - expand_height))
bottom_right = (min(imgTest.shape[1], faceLocTest[1] + expand_width),
min(imgTest.shape[0], faceLocTest[2] + expand_height))

# 返回修改后的图像
cv2.rectangle(imgTest, top_left, bottom_right, (255, 0, 255), 2)

return (top_left, bottom_right)

except IndexError:
print("No face was detected in the image.")
return None

# 加载图像并转换为RGB格式
imgTest = face_recognition.load_image_file('img/11.jpg')
imgTest = cv2.cvtColor(imgTest, cv2.COLOR_BGR2RGB)

a,b = expand_and_draw_face_box(imgTest)
print(a,b)

cv2.imshow('Image with Expanded Face Box', imgTest)
cv2.waitKey(0)
cv2.destroyAllWindows()