python 使用百度API實現人臉對比識別

2021-09-22 19:49:50 字數 3689 閱讀 3182

使用pip安裝

api_key = '你的 api_key'

secret_key = '你的secret_key '

def fileopen(filepath): #開啟

with open(filepath, 'rb') as f:

data = base64.b64encode(f.read())

image = str(data,'utf-8')

return image

def catchusbvideo(window_name,camera_idx):

cv2.namedwindow(window_name)

cap = cv2.videocapture(camera_idx)

#識別出人臉後要畫的邊框的顏色,rgb格式

color = (0, 255, 0)

while cap.isopened():

ok, frame = cap.read() #讀取一幀資料

if not ok:

break

c = cv2.waitkey(10)#按q退出

cv2.imshow(window_name, frame)

if c == ord('p'):

cv2.imwrite('p1.jpg',frame)#儲存

print('儲存成功')

break

if c == ord('o'):

cv2.imwrite('p2.jpg',frame)#儲存

break

if c == ord('q'):

break

#釋放攝像頭並銷毀所有視窗

cap.release()

cv2.destroyallwindows()

def face_detect(filepath1,filepath2): #人臉對比

image1 = fileopen(filepath1)

image2 = fileopen(filepath2)

result = client.match([,])

print(result) #列印出所有的資訊

def face_add(filepath,groupid,userid): #人臉庫增加 位址 組 使用者

image = fileopen(filepath)

imagetype="base64"

result=client.adduser(image,imagetype,groupid,userid)

if result['error_code']==0:

print("增加人臉成功")

else:

print("增加人臉失敗")

def face_search(filepath,groupidlist): #人臉庫搜尋 222207 groupidlist="你的使用者組名稱"

image = fileopen(filepath)

imagetype="base64"

result=client.search(image,imagetype,groupidlist)

print(result) #列印出所有資訊

catchusbvideo('window_name',0)我是使用opencv開啟攝像頭來拍取**並上傳的

注意要上傳的格式要經過 base64編碼並str強制型別轉換才能使用,因為可能base64編碼+,-,=這些符號還是ascll碼,不然可能會報錯,**如下

def fileopen(filepath): #開啟

with open(filepath, 'rb') as f:

data = base64.b64encode(f.read())

image = str(data,'utf-8')

return image

這裡是一些子函式

#image = "取決於image_type引數,傳入base64字串或url字串或face_token字串"

#image = urllib.parse.urlencode(data)

image = str(data,'utf-8')

imagetype = "base64"

""" 呼叫人臉檢測 """

result = client.detect(image, imagetype);

print(result)

def face_detect(filepath1,filepath2): #人臉對比

image1 = fileopen(filepath1)

image2 = fileopen(filepath2)

result = client.match([,])

print(result) #列印出所有的資訊

def face_add(filepath,groupid,userid): #人臉庫增加 位址 組 使用者

image = fileopen(filepath)

imagetype="base64"

result=client.adduser(image,imagetype,groupid,userid)

if result['error_code']==0:

print("增加人臉成功")

else:

print("增加人臉失敗")

def face_search(filepath,groupidlist): #人臉庫搜尋 groupidlist="你的使用者組名稱"

image = fileopen(filepath)

imagetype="base64"

data=client.search(image,imagetype,groupidlist)

result = data.get('result')

print(result) #列印出所有資訊

if result == none:

return none

else:#分數大於70

if result['user_list'][0]['score'] >70 :

print(result['user_list'][0]['user_id'])

return result['user_list'][0]['user_id']

else :

return none

使用python呼叫百度翻譯api

注意看一下產品服務,有的服務有字元數量限制超過了要收費。輸入的引數有 簽名生成的演算法 import hashlib 用來計算md5碼 deffanyi shuru header 隨機碼 金鑰 q shuru md5 hashlib.md5 md5.update sign.encode utf 8 ...

百度api實現人臉對比

第一步 註冊賬號 如圖 第二步 import requests import base64 import json 1,準備好申請的人臉識別api,api key,secret key api1 你的id client secret 你的secret key api2 2,獲取token值,拼接ap...

Python使用百度api做人臉對比

pip install baidu aip如果在pycharm裡也可以在setting project interpreter 右邊綠色加號,輸入baidu,安裝baidu aip 之後把得到的api key secretkey 填進去。from aip import aipface api key...