-
要求
1.安裝百度python API
2.到百度智能云創建應用
3.調用API,代碼
1.安裝百度python API
使用pip安裝
pip3 install baidu-api
2.到 百度智能云創建應用
-如下圖
創建應用得到
APP_ID
API_KEY
SECRET_KEY
3.調用API,代碼
from aip import AipFace
import base64
import urllib
import cv2
""" 你的 APPID AK SK """
APP_ID = '你的 APP_ID '
API_KEY = '你的 API_KEY'
SECRET_KEY = '你的SECRET_KEY '
client = AipFace(APP_ID, API_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)
#視頻來源,可以來自一段已存好的視頻,也可以直接來自USB攝像頭
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([
{
'image': image1,
'image_type': 'BASE64',
},
{
'image': image2,
'image_type': 'BASE64',
}])
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) #打印出所有信息
if __name__ == '__main__':
#face_comper()
#face_detect('p1.jpg','p2.jpg')
#face_add('p1.jpg','test','one')
#face_search('p2.jpg','test')
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
這里是一些子函數
def test1():
with open("./p1.jpg", 'rb') as f:
data = base64.b64encode(f.read())
#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([
{
'image': image1,
'image_type': 'BASE64',
},
{
'image': image2,
'image_type': 'BASE64',
}])
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
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
