哈嘍兄弟們,今天實現一下人臉識別。 先問大家一個問題什麼是百度Aip模塊? 百度AI平臺提供了很多的API介面供開發者快速的調用運用在項目中本文寫的是使用百度AI的線上介面SDK模塊(baidu-aip)進行實現人臉識別 除了人臉識別,其他api功能的調用也同理。 準備工作 本機環境 系統:win1 ...
哈嘍兄弟們,今天實現一下人臉識別。
先問大家一個問題
什麼是百度Aip模塊?
百度AI平臺提供了很多的API介面供開發者快速的調用運用在項目中
本文寫的是使用百度AI的線上介面SDK模塊(baidu-aip)進行實現人臉識別
除了人臉識別,其他api功能的調用也同理。
準備工作
本機環境
系統:win11
Python版本:3.9.7
編輯器:VS2022
安裝baidu-aip模塊
win + R 輸入cmd打開命令提示符
執行安裝百度AI模塊
pip install baidu-aip # 完整代碼 文末獲取
登錄百度AI平臺創建應用
打開百度AI平臺 進行登錄
在控制臺中找到人臉識別
按自己要求創建應用
最後得到應用的AppID API Key Secret Key
記下值 等等會用到
AppID:10000000
API Key:xxxxxxxxxxxxxxxxxxxxxxxx
Secret Key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
代碼流程
導入baidu-aip模塊
打開VS2022(VSCode PyCharm Sypder等同理)創建一個py文件
輸入
from aip import AipFace
聲明上文獲取的AppID API Key Secret Key
APP_ID = '10000000' API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx' SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
初始化百度AIP 人臉識別模塊
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
創建人臉檢測函數
def face_detect(image): result = client.detect(image, image_type='BASE64') print(result) return result
輸入的圖片image必須是BASE64格式
將圖片轉為BASE64格式
導入base64包
import base64
將圖片打開為 BASE64格式
但是導入到百度AI中需要為字元串格式,所以返回為字元串
def imageToBase64(imagePath): with open(imagePath, 'rb') as f: image = base64.b64encode(f.read()) return str(image, encoding='utf-8')
打開圖片進行檢測
先準備一張圖片pic1.jpg
調用函數
face_detect(imageToBase64("pic1.jpg"))
提示調用成功:
遇到的問題
運行時候提示:
requests.exceptions.ProxyError: HTTPSConnectionPool(host='aip.baidubce.com', port=443)
win + R 輸入 regedit打開註冊表,找到
\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
把ProxyEnable的值改為0
再運行即可
延伸出使用其他功能
除了人臉檢測還可以使用人臉比、人臉搜索對等函數,調用方法同理,比如人臉比對。
def face_match(image1, image2): result = client.match([ { 'image': image1, 'image_type': 'BASE64', }, { 'image': image2, 'image_type': 'BASE64', } ]) print(result) return result
人臉搜索
def face_search(image,group_id_list): result = client.search(image, image_type='BASE64',group_id_list=group_id_list) print(result) return result #完整代碼都在這個群啦 279199867
APP_ID API_KEY SECRET_KEY 需要修改為自己的
今天的分享就到這裡結束了,完整代碼點 我 獲取.
推薦一套Python教程,涵蓋了常見的一百多個實戰案例,每一個都非常詳細。
代碼總是學完就忘記?100個爬蟲實戰項目!讓你沉迷學習丨學以致用丨下一個Python大神就是你!