Internship-day04

In the first beginning, I write a function with DeepFace

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
from deepface import DeepFace
import pandas as pd
import datetime
# 这个文件用之前要改成main.py

# Representations stored in db/representations_vgg_face.pkl file.Please delete this file when you add new identities in your database.
# 每次动db文件夹,也就是数据库,都要把里面的pkl文件删掉

# 读取 Excel 文件
df = pd.read_excel('info.xlsx')

a=datetime.datetime.now()

# 比对图片和数据库
# 这个是模版
# found = DeepFace.find(img_path = "img.png", db_path = "db")
found = DeepFace.find(img_path="img.png", db_path="db", model_name='DeepFace')

b=datetime.datetime.now()

# 如果找到了匹配的图片
if found:
# 取第一张匹配的图片
match = found[0]

# 提取出不含后缀的文件名,假设找到的图片的文件名(不包括扩展名)是人的 ID
person_id = str(match['identity']).split('/')[-1].split('.')[0]

# 在 Excel 文件中查找这个人的信息
person_info = df[df['id'].astype(str) == person_id]

# 如果找到了这个人的信息
if not person_info.empty:
# 打印这个人的信息
print(person_info.iloc[0])

# print("used time:", b-a)
# # 这里用来测试打印
# print(df)
# print('found:', found)
# print('person_id:', person_id)
# print('person_info:', person_info)

But the effectiveness of the model and algorithms are poor. When “model_name=’DeepFace’”, the recognition speed is fast, with the sacrifice of accuracy. When “model_name=’VGG’”, the recognition speed is slow, about 15s per request.

Consequently, I used ‘face_recognition’ from GitHub, which turned out to be of great effectiveness.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import face_recognition
import os
import pandas as pd


def find_person_info(image_path):
# 文件夹路径
folder_path = "db"

# 加载你想要识别的图片
unknown_image = face_recognition.load_image_file(image_path)
unknown_face_encodings = face_recognition.face_encodings(unknown_image)

if not unknown_face_encodings:
print("No faces found in the unknown_image.")
return None

# 获取未知图片中的面部编码
unknown_face_encoding = unknown_face_encodings[0]

# 遍历文件夹中的每一张图片
for filename in os.listdir(folder_path):
# 只处理图片文件
if filename.endswith(".jpg") or filename.endswith(".png"):
# 加载已知的图片
known_image = face_recognition.load_image_file(os.path.join(folder_path, filename))
known_face_encodings = face_recognition.face_encodings(known_image)

if not known_face_encodings:
print(f"No faces found in the image {filename}.")
continue

# 获取已知图片中的面部编码
known_face_encoding = known_face_encodings[0]

# 比较两个面部编码,看是否匹配,调整tolerance参数
results = face_recognition.compare_faces([known_face_encoding], unknown_face_encoding, tolerance=0.45)

# 打印结果
if results[0]:
# 提取出不含后缀的文件名,假设文件名(不包括扩展名)是人的名字
person_id = filename.split('.')[0]
print(f"The unknown_image has a face that matches {person_id}!")

df = pd.read_excel('info.xlsx')

person_info = df[df['id'].astype(str) == person_id]

# 如果找到了这个人的信息
if not person_info.empty:
# 返回这个人的信息

print(person_info.iloc[0])

# 返回to_dict()格式
return person_info.iloc[0].to_dict()

return 0

the key steps include:

  1. load the unknown picture and get the encoding

    1
    2
    3
    4
    5
    6
    # 文件夹路径
    folder_path = "db"

    # 加载你想要识别的图片
    unknown_image = face_recognition.load_image_file(image_path)
    unknown_face_encodings = face_recognition.face_encodings(unknown_image)
  2. traverse pictures in ‘db’ files and get all encodings

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # 遍历文件夹中的每一张图片
    for filename in os.listdir(folder_path):
    # 只处理图片文件
    if filename.endswith(".jpg") or filename.endswith(".png"):
    # 加载已知的图片
    known_image = face_recognition.load_image_file(os.path.join(folder_path, filename))
    known_face_encodings = face_recognition.face_encodings(known_image)

    if not known_face_encodings:
    print(f"No faces found in the image {filename}.")
    continue

    # 获取已知图片中的面部编码
    known_face_encoding = known_face_encodings[0]
  3. compare unknown_encodings with known_encodings, adjusting parameters: tolerance. Then transfer the output type of name of the picture from int to string

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    # 比较两个面部编码,看是否匹配,调整tolerance参数
    results = face_recognition.compare_faces([known_face_encoding], unknown_face_encoding, tolerance=0.45)

    # 打印结果
    if results[0]:
    # 提取出不含后缀的文件名,假设文件名(不包括扩展名)是人的名字
    person_id = filename.split('.')[0]
    print(f"The unknown_image has a face that matches {person_id}!")

    df = pd.read_excel('info.xlsx')

    person_info = df[df['id'].astype(str) == person_id]

    # 如果找到了这个人的信息
    if not person_info.empty:
    # 返回这个人的信息

    print(person_info.iloc[0])

    # 返回to_dict()格式
    return person_info.iloc[0].to_dict()

Make sure that, the id value in excel file, equals to names of the known pictures(without postfix)

明天做异常处理、报错,多人图片,性能优化