import face_recognition import cv2 import os unknow_people_list = [i for i in os.listdir('unknow_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))] know_people_list = [i for i in os.listdir('know_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))] def face_select(): for unknow_people in unknow_people_list: # 读取待识别图片 unknow = face_recognition.load_image_file('unknow_people/' + unknow_people) # 将待识别图片转化为特征向量 unknow_encode = face_recognition.face_encodings(unknow)[0] flag = False for know_people in know_people_list: # 读取计算机已经认识的图片 know = face_recognition.load_image_file('know_people/' + know_people) # 获得面部位置 face_location1 = face_recognition.face_locations(know) face_location2 = face_recognition.face_locations(unknow) # 提取面部关键点 face_landmarks_list1 = face_recognition.face_landmarks(know) face_landmarks_list2 = face_recognition.face_landmarks(unknow) # 图片转化为特征向量 know_encode = face_recognition.face_encodings(know)[0] # 两张图片进行比较的结果 res = face_recognition.compare_faces([know_encode], unknow_encode, tolerance=0.5) if res[0]: flag = True name = know_people.split(".")[0] break if flag: print(f'{name}匹配成功!') else: print(f'匹配失败') name = "UNKNOWN" # 绘制人脸特征点和矩形框 for (x1, y1, w1, h1) in face_location1: cv2.rectangle(know, (y1, x1), (h1, w1), (255, 0, 0), 2) cv2.putText(know, name, (y1 - 10, x1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2) for face_landmarks in face_landmarks_list1: for facial_feature in face_landmarks.keys(): for pt_pos in face_landmarks[facial_feature]: cv2.circle(know, pt_pos, 1, (192, 192, 192), 2) for (x1, y1, w1, h1) in face_location2: cv2.rectangle(unknow, (y1, x1), (h1, w1), (255, 0, 0), 2) cv2.putText(unknow, name, (y1 - 10, x1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2) for face_landmarks in face_landmarks_list2: for facial_feature in face_landmarks.keys(): for pt_pos in face_landmarks[facial_feature]: cv2.circle(unknow, pt_pos, 1, (192, 192, 192), 2) # 显示图片 cv2.imshow("known", know) cv2.imshow("unknown", unknow) cv2.waitKey(0) if __name__ == '__main__': face_select()
时间: 2024-02-14 21:06:48 浏览: 169
dlib_face_recognition_resnet_model_v1.dat.zip_ResNet_dlib_face r
5星 · 资源好评率100%
这段代码使用 face_recognition 和 OpenCV 库来进行人脸识别。它将 "know_people" 和 "unknow_people" 两个文件夹中的图像进行比较,以查找已知人脸。以下是代码的主要步骤:
1. 导入所需库。
```python
import face_recognition
import cv2
import os
```
2. 定义要识别的人脸图像列表。
```python
unknow_people_list = [i for i in os.listdir('unknow_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))]
know_people_list = [i for i in os.listdir('know_people') if (i.endswith('.jpg')) or (i.endswith('.png')) or (i.endswith('.jpeg'))]
```
3. 定义人脸识别函数。
```python
def face_select():
for unknow_people in unknow_people_list:
# 读取待识别图片
unknow = face_recognition.load_image_file('unknow_people/' + unknow_people)
# 将待识别图片转化为特征向量
unknow_encode = face_recognition.face_encodings(unknow)[0]
flag = False
for know_people in know_people_list:
# 读取计算机已经认识的图片
know = face_recognition.load_image_file('know_people/' + know_people)
# 获得面部位置
face_location1 = face_recognition.face_locations(know)
face_location2 = face_recognition.face_locations(unknow)
# 提取面部关键点
face_landmarks_list1 = face_recognition.face_landmarks(know)
face_landmarks_list2 = face_recognition.face_landmarks(unknow)
# 图片转化为特征向量
know_encode = face_recognition.face_encodings(know)[0]
# 两张图片进行比较的结果
res = face_recognition.compare_faces([know_encode], unknow_encode, tolerance=0.5)
if res[0]:
flag = True
name = know_people.split(".")[0]
break
if flag:
print(f'{name}匹配成功!')
else:
print(f'匹配失败')
name = "UNKNOWN"
# 绘制人脸特征点和矩形框
for (x1, y1, w1, h1) in face_location1:
cv2.rectangle(know, (y1, x1), (h1, w1), (255, 0, 0), 2)
cv2.putText(know, name, (y1 - 10, x1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
for face_landmarks in face_landmarks_list1:
for facial_feature in face_landmarks.keys():
for pt_pos in face_landmarks[facial_feature]:
cv2.circle(know, pt_pos, 1, (192, 192, 192), 2)
for (x1, y1, w1, h1) in face_location2:
cv2.rectangle(unknow, (y1, x1), (h1, w1), (255, 0, 0), 2)
cv2.putText(unknow, name, (y1 - 10, x1 - 10), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
for face_landmarks in face_landmarks_list2:
for facial_feature in face_landmarks.keys():
for pt_pos in face_landmarks[facial_feature]:
cv2.circle(unknow, pt_pos, 1, (192, 192, 192), 2)
# 显示图片
cv2.imshow("known", know)
cv2.imshow("unknown", unknow)
cv2.waitKey(0)
```
该函数首先将待识别图片转化为特征向量,然后将其与已知人脸图像进行比较,以查找匹配。如果找到匹配,则在图像上绘制人脸特征点和矩形框,并显示人名。如果未找到匹配,则在图像上显示 "UNKNOWN"。
4. 在主函数中调用人脸识别函数。
```python
if __name__ == '__main__':
face_select()
```
在主函数中,我们只需要调用 `face_select()` 函数即可开始人脸识别。
阅读全文