Baidu face registration / search test

获取access_token:https://ai.baidu.com/docs#/Auth/top

= Host ' https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={id}&client_secret={secret} ' .format (ID = ' [] Get console ' , Secret = ' [Control station acquires] ' ) 
headers = {
     ' the Content-the Type ' : ' file application / JSON; charset = UTF-. 8 ' 
} 
RES = requests.get (= Host URL, headers = headers) 
RES = json.loads (res.text)
 Print (res.get ( ' the access_token ' , None))

 

Face Registration api documentation: https: //ai.baidu.com/docs#/Face-Set-V3/top

# Face database Add 
face_dir = os.path.join (os.path.dirname ( __FILE__ ), ' face_test ' , ' face-picture.jpg ' ) 
with Open (face_dir, ' rb ' ) AS f: 
    Image = Base64. b64encode (reached, f.read ()). decode () 

face_add = ' https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token= [] acquired from the console and generates ' 

headers = {
     ' the Content-the Type ' : ' file application / JSON ' 
} 

Data = {
    'image': image,
    'image_type': 'BASE64',
    'group_id': 'archive',
    'user_id': 'zhang',
}

res = requests.post(url=face_add, headers=headers, data=data)
res = json.loads(res.text)
print(res)

Return result:

# RES: 
    {
         ' ERROR_CODE ' : 0,
         ' ERROR_MSG ' : ' SUCCESS ' ,
         ' log_id ' : 1368654417985064851 ,
         ' timestamp ' : 1,571,798,506 ,
         ' cached ' : 0,
         ' Result ' : {
             ' face_token ' : ' Face token { globally unique} ' , # in the new user database face_token field, will face registration id into a unique identity to the human face behind the results of the search were aligned
             'location': {
            'left': 35.18,
            'top': 216.13,
            'width': 380,
            'height': 388,
            'rotation': -3
            }
        }
    }
'''

 

Face search api documentation: https: //ai.baidu.com/docs#/Face-Search-V3/top

# 人脸库搜索
face_dir = os.path.join(os.path.dirname(__file__), 'face_test', 'face-picture.jpg')
with open(face_dir, 'rb') as f:
    image = base64.b64encode(f.read()).decode()

headers = {
    'Content-Type': 'application/json'
}

data = {
    'image': image,
    'image_type': 'BASE64',
    'group_id_list': 'archive'
}
search_url = "https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=【控制台获取并生成】"
res = requests.post(url=search_url, headers=headers, data=data)
res = json.loads(res.text)
print(res)
'' ' 
# RES 
{ 
    ' ERROR_CODE ': 0, 
    ' ERROR_MSG ':' SUCCESS ', 
    ' log_id ': 1,345,050,717,991,180,571, 
    ' timestamp ': 1571799118, 
    ' cached ': 0, 
    ' Result ': { 
        ' face_token ':' Face globally unique token {} ', # query to face token can be used as database fields than to see the human face identity information 
        ' user_list ': [{ 
            ' group_id ':' Archive ', 
            ' user_id ':' zhang ' , 
            'USER_INFO': '', 
            'Score': 100 
        }] 
    } 
} 
'' '

 

Guess you like

Origin www.cnblogs.com/52-qq/p/11725119.html