Internship-day04
In the first beginning, I write a function with DeepFace
1 | from deepface import DeepFace |
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 | import face_recognition |
the key steps include:
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)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]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)
明天做异常处理、报错,多人图片,性能优化