Python使用百度api做人臉對比的方法
阿新 • • 發佈:2020-01-09
安裝SDK:
pip install baidu-aip
如果在pycharm裡也可以在setting----Project Interpreter---右邊綠色加號,輸入baidu,安裝baidu-aip
入門程式碼:
先去百度AI開放平臺註冊一個賬號,然後開通人臉識別,免費的
http://ai.baidu.com/tech/face
之後把得到的Api key secretkey 填進去。
from aip import AipFace """ 你的 APPID AK SK """ APP_ID = '你的 App ID' API_KEY = '你的 Api Key' SECRET_KEY = '你的 Secret Key' client = AipFace(APP_ID,API_KEY,SECRET_KEY) """ 讀取圖片 """ def get_file_content(filePath): with open(filePath,'rb') as fp: return fp.read() images = [ get_file_content('example0.jpg'),get_file_content('example1.jpg'),] """ 呼叫人臉比對 """ result_json=client.match(images); print(result_json)
會自動把你當前工程目錄下的example0.jpg 和example1.jpg進行比對。
最後會得到這樣一個json字串
{'result': [{'index_i': '0','index_j': '1','score': 21.207210540771}],'result_num': 1,'log_id': 2864712345030414}
裡面的score就是兩張人臉的相似度 了,這裡我用的不同的人臉,只有21%左右
可以再寫一個判斷的方法:
def judge(images): result_json = client.match(images); result = result_json['result'][0]['score'] if result > 80: print("同一個人") else: print("不是同一個人")
以上這篇Python使用百度api做人臉對比的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。