Python实现人脸识别的详细图文教程
作者:轻松学Python 时间:2022-12-28 04:45:53
叨叨几句
哈喽兄弟们,今天实现一下人脸识别。
先问大家一个问题
什么是百度Aip模块?
百度AI平台提供了很多的API接口供开发者快速的调用运用在项目中
本文写的是使用百度AI的在线接口SDK模块(baidu-aip)进行实现人脸识别
除了人脸识别,其他api功能的调用也同理。
准备工作
本机环境
系统:win11
Python版本:3.9.7
编辑器:VS2022
安 * aidu-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
APP_ID API_KEY SECRET_KEY 需要修改为自己的
来源:https://blog.csdn.net/ooowwq/article/details/126405913
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 划分数据集为训练集和测试集的方法
ASP 3.0中的新特性
![](https://img.aspxhome.com/file/UploadPic/20082/27/2008227134234597s.jpg)
K-means聚类算法介绍与利用python实现的代码示例
![](https://img.aspxhome.com/file/2023/6/120606_0s.png)
python数据结构:数据类型
![](https://img.aspxhome.com/file/2023/0/79630_0s.png)
sina和265天气预报调用代码
vue中使用iframe嵌入网页,页面可自适应问题
![](https://img.aspxhome.com/file/2023/9/133169_0s.png)
Golang使用ini库读取配置详情
![](https://img.aspxhome.com/file/2023/5/89035_0s.webp)
python numpy矩阵信息说明,shape,size,dtype
数字人组件反写[asp组件开发实例5]
![](https://img.aspxhome.com/file/UploadPic/20093/9/20093918286639s.gif)
MySQL优化教程之超大分页查询
![](https://img.aspxhome.com/file/2023/1/106591_0s.png)
网页设计布局原则
django 解决扩展自带User表遇到的问题
![](https://img.aspxhome.com/file/2023/0/110470_0s.jpg)
在PyCharm中实现关闭一个死循环程序的方法
![](https://img.aspxhome.com/file/2023/0/100910_0s.jpg)
TensorFlow深度学习之卷积神经网络CNN
![](https://img.aspxhome.com/file/2023/5/91885_0s.jpg)
Python爬虫爬取Bilibili弹幕过程解析
![](https://img.aspxhome.com/file/2023/5/95755_0s.png)
限制文本框只能输入数字和小数点
Python实现随机生成任意数量车牌号
![](https://img.aspxhome.com/file/2023/8/135378_0s.png)