脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 使用百度AI接口进行人脸对比的步骤

python 使用百度AI接口进行人脸对比的步骤

2021-09-24 00:23可爱的黑精灵 Python

这篇文章主要介绍了python 使用百度AI接口进行人脸对比的步骤,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下

1. 注册百度云账号

注册百度智能云,提交申请。

创建应用获取AppID,API Key,Secret Key。

2. 安装baidu python api

人脸对比 API 文档

pip install baidu-aip

调用:

  1. import base64
  2. from aip import AipFace
  3.  
  4. APP_ID = '你的 App ID'
  5. API_KEY = '你的 Api Key'
  6. SECRET_KEY = '你的 Secret Key'
  7.  
  8. client = AipFace(APP_ID, API_KEY, SECRET_KEY)
  9.  
  10. result = client.match([
  11. {
  12. 'image': str(base64.b64encode(open('D:/chenjy/1.png', 'rb').read()), 'utf-8'),
  13. 'image_type': 'BASE64',
  14. },
  15. {
  16. 'image': str(base64.b64encode(open('D:/chenjy/2.png', 'rb').read()), 'utf-8'),
  17. 'image_type': 'BASE64',
  18. }
  19. ])
  20.  
  21. print(result)

返回值:

python 使用百度AI接口进行人脸对比的步骤

返回主要参数说明:

参数名 必选 类型 说明
score float 人脸相似度得分,推荐阈值80分
face_list array 人脸信息列表
face_token string 人脸的唯一标志

3.调用摄像头

  1. import cv2
  2.  
  3. cap = cv2.VideoCapture(0) # 打开摄像头
  4.  
  5. while True:
  6. ret, frame = cap.read()
  7. frame = cv2.flip(frame, 1)
  8.  
  9. cv2.imshow('window', frame)
  10. cv2.imwrite('D:/chenjy/2.png', frame) # 保存路径
  11.  
  12. cv2.waitKey(2000)
  13.  
  14. cap.release()
  15. cv2.destroyAllWindows()

4.完整测试程序

  1. import cv2
  2. import base64
  3. from aip import AipFace
  4.  
  5. APP_ID = '你的 App ID'
  6. API_KEY = '你的 Api Key'
  7. SECRET_KEY = '你的 Secret Key'
  8.  
  9. client = AipFace(APP_ID, API_KEY, SECRET_KEY)
  10.  
  11. def get_result():
  12. result = client.match([
  13. {
  14. 'image': str(base64.b64encode(open('D:/chenjy/1.png', 'rb').read()), 'utf-8'),
  15. 'image_type': 'BASE64',
  16. },
  17. {
  18. 'image': str(base64.b64encode(open('D:/chenjy/2.png', 'rb').read()), 'utf-8'),
  19. 'image_type': 'BASE64',
  20. }
  21. ])
  22.  
  23. if result['error_msg'] == 'SUCCESS':
  24. score = result['result']['score']
  25. print(result)
  26. print('相似度:'+str(score))
  27. else:
  28. print('服务器错误')
  29.  
  30. cap = cv2.VideoCapture(0) # 打开摄像头
  31.  
  32. while True:
  33. ret, frame = cap.read()
  34. frame = cv2.flip(frame, 1)
  35.  
  36. cv2.imshow('window', frame)
  37. cv2.imwrite('D:/chenjy/2.png', frame) # 保存路径
  38.  
  39. cv2.waitKey(2000)
  40.  
  41. get_result()
  42.  
  43. cap.release()
  44. cv2.destroyAllWindows()

结果:

照片加了模糊处理

python 使用百度AI接口进行人脸对比的步骤

python 使用百度AI接口进行人脸对比的步骤

以上就是python 使用百度AI接口进行人脸对比的步骤的详细内容,更多关于python 人脸对比的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/chenjy1225/p/14371078.html

延伸 · 阅读

精彩推荐