-
要求
1.安裝百度python API
2.到百度智能云創(chuàng)建應(yīng)用
3.調(diào)用API,代碼
1.安裝百度python API
使用pip安裝
pip3 install baidu-api
2.到 百度智能云創(chuàng)建應(yīng)用
-如下圖
創(chuàng)建應(yīng)用得到
APP_ID
API_KEY
SECRET_KEY
3.調(diào)用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): #打開(kāi)圖片
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)
#視頻來(lái)源,可以來(lái)自一段已存好的視頻,也可以直接來(lái)自USB攝像頭
cap = cv2.VideoCapture(camera_idx)
#識(shí)別出人臉后要畫(huà)的邊框的顏色,RGB格式
color = (0, 255, 0)
while cap.isOpened():
ok, frame = cap.read() #讀取一幀數(shù)據(jù)
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): #人臉對(duì)比
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): #人臉庫(kù)增加 地址 組 用戶
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): #人臉庫(kù)搜索 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打開(kāi)攝像頭來(lái)拍取照片并上傳的
注意要上傳的圖片格式要經(jīng)過(guò) base64編碼并str強(qiáng)制類型轉(zhuǎn)換才能使用,因?yàn)榭赡躡ase64編碼+,-,=這些符號(hào)還是ascll碼,不然可能會(huì)報(bào)錯(cuò),代碼如下
def fileopen(filepath): #打開(kāi)圖片
with open(filepath, 'rb') as f:
data = base64.b64encode(f.read())
image = str(data,'utf-8')
return image
這里是一些子函數(shù)
def test1():
with open("./p1.jpg", 'rb') as f:
data = base64.b64encode(f.read())
#image = "取決于image_type參數(shù),傳入BASE64字符串或URL字符串或FACE_TOKEN字符串"
#image = urllib.parse.urlencode(data)
image = str(data,'utf-8')
imageType = "BASE64"
""" 調(diào)用人臉檢測(cè) """
result = client.detect(image, imageType);
print(result)
def face_detect(filepath1,filepath2): #人臉對(duì)比
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): #人臉庫(kù)增加 地址 組 用戶
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): #人臉庫(kù)搜索 groupIdList="你的用戶組名稱"
image = fileopen(filepath)
imageType="BASE64"
data=client.search(image,imageType,groupIdList)
result = data.get('result')
print(result) #打印出所有信息
if result == None:
return None
else:#分?jǐn)?shù)大于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
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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