使用face_recognition进行人脸特征检测

淩亂°似流年 2021-12-24 06:13 486阅读 0赞

效果图
在这里插入图片描述
调用face_recognition.face_landmarks()方法即可得到人脸特征点, 返回一个字典, 下图是返回的数据, 包括chin(下巴), left_eye(左眼)等.
在这里插入图片描述
我画了两种图, 一种是遍历所有的点, 直接给点画图的图(点用实心圆绘制). 第二个是单独画下巴, 连成线, 用的是polylines方法.

我是4.10版本的opencv. 查阅官方py文档, 这是链接在这里插入图片描述
完整代码:

  1. import face_recognition
  2. import numpy as np
  3. import cv2
  4. image = face_recognition.load_image_file("./data/奥巴马.png")
  5. image2 = image.copy()
  6. face_landmarks_list = face_recognition.face_landmarks(image)
  7. # print(face_landmarks_list)
  8. for each in face_landmarks_list:
  9. print(each)
  10. for i in each.keys():
  11. print(i, end=': ')
  12. print(each[i])
  13. for any in each[i]:
  14. image = cv2.circle(image, any, 3, (0,0,255), -1)
  15. cv2.imshow("奥巴马", image)
  16. # 单独画下巴
  17. for each in face_landmarks_list:
  18. pts = np.array(each['chin'])
  19. pts = pts.reshape((-1, 1, 2))
  20. cv2.polylines(image2, [pts], False, (0, 255, 255)) # false 参数使其不闭合
  21. cv2.imshow("奥巴马2", image2)
  22. cv2.waitKey(0)
  23. cv2.destroyAllWindows()

在线摄像机版本:

  1. import face_recognition
  2. import numpy as np
  3. import cv2
  4. camera = cv2.VideoCapture(0)
  5. while True:
  6. ret, image = camera.read()
  7. image = cv2.flip(image, 1)
  8. image2 = image.copy()
  9. face_landmarks_list = face_recognition.face_landmarks(image)
  10. # print(face_landmarks_list)
  11. for each in face_landmarks_list:
  12. print(each)
  13. for i in each.keys():
  14. print(i, end=': ')
  15. print(each[i])
  16. for any in each[i]:
  17. image = cv2.circle(image, any, 3, (0,0,255), -1)
  18. cv2.imshow("奥巴马", image)
  19. # 单独画下巴
  20. for each in face_landmarks_list:
  21. pts = np.array(each['chin'])
  22. pts = pts.reshape((-1, 1, 2))
  23. cv2.polylines(image2, [pts], False, (0, 255, 255)) # false 参数使其不闭合
  24. cv2.imshow("奥巴马2", image2)
  25. if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
  26. break
  27. cv2.destroyAllWindows()
  28. camera.release()

附一份在线的人脸搜索代码, 人脸数据保存在相对路径./data/mans

  1. import cv2
  2. import face_recognition
  3. import numpy as np
  4. import os
  5. import re
  6. # 人脸数据, 文件, 编码, 名字
  7. files = os.listdir("./data/mans")
  8. face_images = [0]*len(files)
  9. face_encodings = [0]*len(files)
  10. face_names = [0]*len(files)
  11. # 获取编码和名称
  12. for i in range(len(files)):
  13. face_images[i] = face_recognition.load_image_file('./data/mans/' + files[i])
  14. face_encodings[i] = face_recognition.face_encodings(face_images[i])
  15. if len(face_encodings[i]) > 0:
  16. face_encodings[i] = face_encodings[i][0]
  17. else:
  18. face_encodings[i] = None
  19. face_names[i] = re.findall(r'(.*)\..*', files[i])[0]
  20. print(face_names)
  21. # 人脸比较
  22. # results = face_recognition.compare_faces(face_encodings[0], face_encodings[1])
  23. # print(results)
  24. # 人脸距离
  25. # face_distances = face_recognition.face_distance(face_encodings[0], face_encodings[1])
  26. # index = np.argmin(face_distances)
  27. # print(index)
  28. # camera = cv2.VideoCapture('./data/test.avi') # 从视频文件
  29. camera = cv2.VideoCapture(0) # 从摄像头
  30. while True:
  31. ret, img = camera.read()
  32. img = cv2.flip(img, 1)
  33. # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 灰度处理
  34. locations = face_recognition.face_locations(img)
  35. for top, right, bottom, left in locations:
  36. cv2.rectangle(img, (left, top), (right, bottom), (255, 0, 0), 2)
  37. sub_img = img[top:bottom, left:right]
  38. sub_img_code = face_recognition.face_encodings(sub_img)
  39. if len(sub_img_code) != 0:
  40. face_distances = face_recognition.face_distance(face_encodings, sub_img_code[0])
  41. print(face_distances)
  42. index = np.argmin(face_distances)
  43. name = face_names[index]
  44. cv2.putText(img, name, (left, top - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
  45. cv2.imshow('Face', img)
  46. if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
  47. break
  48. cv2.destroyAllWindows()
  49. camera.release()

发表评论

表情:
评论列表 (有 0 条评论,486人围观)

还没有评论,来说两句吧...

相关阅读

    相关 人脸特征检测--基于DLIB库

      Dlib是一个C++编写的工具包,它包含了机器学习算法以及一些用来解决现实复杂问题的工具,可以广泛应用于机器人、嵌入式设备、手机,甚至高性能计算中,可以在其官网了解更多。这