Python人工智能第二篇:人臉檢測和圖像識別

Python人工智能第二篇:人臉檢測和圖像識別

人臉檢測

詳細內容請看技術文檔:https://ai.baidu.com/docs#/Face-Python-SDK/toppython

from aip import AipFace
import base64

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

face_client = AipFace(APP_ID, API_KEY, SECRET_KEY)

""" 讀取圖片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


bytes_str = base64.b64encode(get_file_content('people/1.jpg'))
image = str(bytes_str, "utf8")

imageType = "BASE64"

options = {}
options["face_field"] = "age,beauty"
""" 調用人臉檢測 """
res = face_client.detect(image, imageType, options)

age = res.get("result").get("face_list")[0].get("age")
beauty = res.get("result").get("face_list")[0].get("beauty")
print(f"年齡:{age}歲", f"顏值:{beauty}分")

圖像識別

詳細內容請看技術文檔:https://ai.baidu.com/docs#/ImageClassify-Python-SDK/top人工智能

from aip import AipImageClassify

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

""" 讀取圖片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('erha.jpg')
""" 若是有可選參數 """
options = {}
options["top_num"] = 1  #返回預測得分top結果數,默認爲6
options["baike_num"] = 5    #返回百科信息的結果數,默認不返回

""" 帶參數調用動物識別 """
res = client.animalDetect(image, options)

print(res)
相關文章
相關標籤/搜索