溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python如何使用百度AI接口進(jìn)行人臉對比

發(fā)布時(shí)間:2021-03-17 14:15:26 來源:億速云 閱讀:190 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)python如何使用百度AI接口進(jìn)行人臉對比,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

1. 注冊賬號

注冊并提交申請。

創(chuàng)建應(yīng)用獲取AppID,API Key,Secret Key。

2. 安裝baidu python api

人臉對比 API 文檔

pip install baidu-aip

調(diào)用:

import base64
from aip import AipFace

APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipFace(APP_ID, API_KEY, SECRET_KEY)

result = client.match([
  {
   'image': str(base64.b64encode(open('D:/chenjy/1.png', 'rb').read()), 'utf-8'),
   'image_type': 'BASE64',
  },
  {
   'image': str(base64.b64encode(open('D:/chenjy/2.png', 'rb').read()), 'utf-8'),
   'image_type': 'BASE64',
  }
 ])

print(result)

返回值:

python如何使用百度AI接口進(jìn)行人臉對比

返回主要參數(shù)說明:

參數(shù)名必選類型說明
scorefloat人臉相似度得分,推薦閾值80分
face_listarray人臉信息列表
face_tokenstring人臉的唯一標(biāo)志

3.調(diào)用攝像頭

import cv2

cap = cv2.VideoCapture(0) # 打開攝像頭

while True:
 ret, frame = cap.read()
 frame = cv2.flip(frame, 1)

 cv2.imshow('window', frame)
 cv2.imwrite('D:/chenjy/2.png', frame) # 保存路徑

 cv2.waitKey(2000)

cap.release()
cv2.destroyAllWindows()

4.完整測試程序

import cv2
import base64
from aip import AipFace

APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'


client = AipFace(APP_ID, API_KEY, SECRET_KEY)


def get_result():
 result = client.match([
  {
   'image': str(base64.b64encode(open('D:/chenjy/1.png', 'rb').read()), 'utf-8'),
   'image_type': 'BASE64',
  },
  {
   'image': str(base64.b64encode(open('D:/chenjy/2.png', 'rb').read()), 'utf-8'),
   'image_type': 'BASE64',
  }
 ])

 if result['error_msg'] == 'SUCCESS':
  score = result['result']['score']
  print(result)
  print('相似度:'+str(score))
 else:
  print('服務(wù)器錯誤')


cap = cv2.VideoCapture(0) # 打開攝像頭

while True:
 ret, frame = cap.read()
 frame = cv2.flip(frame, 1)

 cv2.imshow('window', frame)
 cv2.imwrite('D:/chenjy/2.png', frame) # 保存路徑

 cv2.waitKey(2000)

 get_result()

cap.release()
cv2.destroyAllWindows()

結(jié)果:

照片加了模糊處理

python如何使用百度AI接口進(jìn)行人臉對比

python如何使用百度AI接口進(jìn)行人臉對比

關(guān)于“python如何使用百度AI接口進(jìn)行人臉對比”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI