百度Aip人臉識別之python代碼


用python來做人臉識別代碼量少 思路清晰,

在使用之前我們需要在我們的配置的編譯器中通過pip install baidu-aip  即可

from aip import AipFace

就可以開始使用api了 我們第一次接觸這個東西不妨

help(AipFace)

你就可以看到他所支持的功能。

在使用之前我們需要在百度的后台創建應用。將我們人臉都存放入庫中。

其次我們要了解一個概念,我們要將本機中的圖片與后台的人臉對比的話我們需要將圖片轉成base64的字符串的格式

import base64
f = open('./photo/mr.jpg', 'rb')
image = base64.b64encode(f.read())
image64 = str(image,'utf-8')
image_type = "BASE64"
 
         

當然我們也可以將base64碼轉換成圖片。

人臉檢測的原理是通過機器學習轉化提前圖片人臉中的七十二個關鍵點。並進行其他的分析。

python3的代碼為

'''
# 人臉檢測與屬性分析
'''

request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"

f = open('./photo/mr.jpg', 'rb')
image = base64.b64encode(f.read())
image64 = str(image,'utf-8')
image_type = "BASE64"



# params = "{\"image\":\"%s\",\"image_type\":\"BASE64\",\"face_field\":\"faceshape,facetype\"}"%image64
params = {'image': image64,'image_type':"BASE64",'face_field': 'faceshape,facetype'}
# 此處的faceshape和facetype需要自己加上去 更具自己需要的返回值

params = urllib.parse.urlencode(params).encode("utf-8")


access_token = '[24.3941b86dfcbc8eaea432d11df4f6660d.2592000.1542368987.282335-14255146]'
request_url = request_url + "?access_token=" + access_token

request = urllib.request.urlopen(url=request_url, data=params)   # 發送請求

content = request.read()  # 將返回結果讀取出來
print(content)  # 顯示返回結果


import urllib.request,sys,base64
import urllib.parse

# 返回實例
a = {'error_code': 0,
     'error_msg': 'SUCCESS',
     'log_id': 1011018945051,
     'timestamp': 1540301526,
     'cached': 0,
     'result': {'face_num': 1,
                'face_list': [{'face_token': '80ed04e5e8771730b3fe240f8ead4e97',
                               'location': {'left': 564.6082764,
                                            'top': 117.9681244,
                                            'width': 263,
                                            'height': 265,
                                            'rotation': 1},
                               'face_probability': 1,
                               'angle': {'yaw': -0.301689893,
                                         'pitch': -15.59528351,
                                         'roll': 0.9747127891
                                         }
                               }
                              ]
                }
     }

具體各種屬性請看百度文檔  https://ai.baidu.com/docs#/Face-Detect-V3/top

介紹完人臉檢測我們就可以進行人臉對比了

import base64

from aip import AipFace

'''新建aipface的配置'''
''' 你的 app id ak sk '''
AppId = '14255146'
ApiKey = 'UoyrHmKFG3nGPL5HmDiGo80G'
SecretKey = 'HUo1z36aDc1UxOwuS8d7Vxldh4GsQg8l'
client = AipFace(AppId, ApiKey, SecretKey)


f = open('./photo/huge.jpg', 'rb')
image = base64.b64encode(f.read())
image64 = str(image,'utf-8')
image_type = "BASE64"
print(client.detect(image64, image_type))  # 此處的返回值為人臉的基本檢測的數值效果

# print(strs)
# 人臉檢測
# image = str(strs)  # 取決於image_type參數,傳入BASE64字符串或URL字符串或FACE_TOKEN字符串

imageType = "BASE64"

groupIdList = "17ai_1"

""" 調用人臉搜索 """


print(client.search(str(image64), image_type, groupIdList))

# 將返回對比結果

'''
""" 如果有可選參數 """
options = {}
options["quality_control"] = "NORMAL"
options["liveness_control"] = "LOW"
options["user_id"] = "233451"
options["max_user_num"] = 3

""" 帶參數調用人臉搜索 """
# print(client.search(image, imageType, , options))


#  人臉搜索返回例子
''' 
face = {
  "face_token": "fid",
  "user_list": [
     {
        "group_id": "test1",
        "user_id": "u333333",
        "user_info": "Test User",
        "score": 99.3
    }
  ]
}

 

轉載至http://www.cnblogs.com/BookMiki/p/9844857.html
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM