python 使用百度AI接口進行人臉對比


1. 注冊百度雲賬號

注冊百度智能雲,提交申請。

創建應用獲取AppID,API Key,Secret Key

2. 安裝baidu python api

人臉對比 API 文檔

pip install baidu-aip

調用:


import base64
from aip import AipFace

APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipFace(APP_ID, API_KEY, SECRET_KEY)

result = client.match([
        {
            'image': str(base64.b64encode(open('D:/chenjy/1.png', 'rb').read()), 'utf-8'),
            'image_type': 'BASE64',
        },
        {
            'image': str(base64.b64encode(open('D:/chenjy/2.png', 'rb').read()), 'utf-8'),
            'image_type': 'BASE64',
        }
    ])

print(result)

返回值:

返回主要參數說明:

參數名 必選 類型 說明
score float 人臉相似度得分,推薦閾值80分
face_list array 人臉信息列表
face_token string 人臉的唯一標志

3.調用攝像頭


import cv2

cap = cv2.VideoCapture(0)  # 打開攝像頭

while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)

    cv2.imshow('window', frame)
    cv2.imwrite('D:/chenjy/2.png', frame)  # 保存路徑

    cv2.waitKey(2000)

cap.release()
cv2.destroyAllWindows()

4.完整測試程序


import cv2
import base64
from aip import AipFace

APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'


client = AipFace(APP_ID, API_KEY, SECRET_KEY)


def get_result():
    result = client.match([
        {
            'image': str(base64.b64encode(open('D:/chenjy/1.png', 'rb').read()), 'utf-8'),
            'image_type': 'BASE64',
        },
        {
            'image': str(base64.b64encode(open('D:/chenjy/2.png', 'rb').read()), 'utf-8'),
            'image_type': 'BASE64',
        }
    ])

    if result['error_msg'] == 'SUCCESS':
        score = result['result']['score']
        print(result)
        print('相似度:'+str(score))
    else:
        print('服務器錯誤')


cap = cv2.VideoCapture(0)  # 打開攝像頭

while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)

    cv2.imshow('window', frame)
    cv2.imwrite('D:/chenjy/2.png', frame)  # 保存路徑

    cv2.waitKey(2000)

    get_result()

cap.release()
cv2.destroyAllWindows()


結果:

照片加了模糊處理


免責聲明!

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



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