调用百度AI实现人脸检索

 1 import base64
 2 import json
 3 import requests
 4 class BaiduPicIndentify:
 5     def __init__(self, img):
 6         self.AK = "你的AK"
 7         self.SK = "你的SK"
 8         self.img_src = img
 9         self.headers = {
10             "Content-Type": "application/json; charset=UTF-8"
11         }
12 
13     def get_accessToken(self):
14         host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.AK + '&client_secret=' + self.SK
15         response = requests.get(host, headers=self.headers)
16         json_result = json.loads(response.text)
17         return json_result['access_token']
18 
19     def img_to_BASE64(slef, path):
20         with open(path, 'rb') as f:
21             base64_data = base64.b64encode(f.read())
22             return base64_data
23 
24     def detect_face(self):
25         # 人脸检测与属性分析
26         img_BASE64 = self.img_to_BASE64(self.img_src)
27         request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
28         post_data = {
29             "image": img_BASE64,
30             "image_type": "BASE64",
31             "face_field": "gender,age,beauty,gender,race,expression",
32             "face_type": "LIVE",
33             "max_face_num":5,
34         }
35         access_token = self.get_accessToken()
36         request_url = request_url + "?access_token=" + access_token
37         response = requests.post(url=request_url, data=post_data, headers=self.headers)
38         json_result = json.loads(response.text)
39         print(json_result)
40         if json_result['error_msg'] != 'pic not has face':
41             print("图片中包含人脸数:", json_result['result']['face_num'])
42             for i in range(json_result['result']['face_num']):
43                 print("图片中包含人物年龄:", json_result['result']['face_list'][i]['age'])
44                 print("图片中包含人物颜值评分:", json_result['result']['face_list'][i]['beauty'])
45                 print("图片中包含人物性别:", json_result['result']['face_list'][i]['gender']['type'])
46                 print("图片中包含人物种族:", json_result['result']['face_list'][i]['race']['type'])
47                 print("图片中包含人物表情:", json_result['result']['face_list'][i]['expression']['type'])
48                 print("下一张图片")
49 if __name__ == '__main__':
50     img_src = input('请输入需要检测的本地图片路径:')
51     baiduDetect = BaiduPicIndentify(img_src)
52     baiduDetect.detect_face()

 

转载于:https://www.cnblogs.com/tianqianlan/p/11615375.html

作为一个AI模型,人脸识别涉及到多个技术领域。在此我们以常规的人脸识别为基础,实现一个简单的基于springboot/vue/mybatis的人脸识别系统。 1、人脸检测和人脸识别的技术选择 我们可以使用开放的Python的人脸识别库(face_recognition),通过Python调用实现人脸检测和人脸识别。Face_recognition 是使用Dlib进行基于Python的人脸识别的开源项目,仅仅需要几行代码就可以完成一项任务,包括人脸识别,面部比较和面部定位等。 2、技术实现步骤 2.1、环境搭建 开发工具:IntelliJ IDEA 数据库:MySQL 前端技术:Vue.js 后端技术:Spring Boot + MyBatis + Face Recognition Library 2.2、集成Face Recognition Library 通过Python安装Face Recognition Library(face_recognition)库,直接使用Python的Pip命令安装 pip install face_recognition 2.3、指定图片目录 在项目中指定图片目录,将其用于人脸检测和识别,默认将图片存储在本项目路径下的img文件夹中。 2.4、前端设计 使用Vue.js实现前端设计,支持以下功能: - 显示识别结果和置信度 - 支持上传图片,实现人脸识别 - 支持查询人脸信息 2.5、后端设计 使用Spring Boot和MyBatis实现后端功能,包括人脸检测和识别,以及查询人脸信息。 使用Spring Boot实现RESTful API,以处理前端请求和响应。 在MyBatis Mapper文件中定义SQL语句,用于从数据库中检索人脸信息。 2.6、上传图片实现人脸识别 实现上传图片实现人脸识别功能,主要包括以下步骤: - 通过上传功能获取上传图片,并且存储到指定目录下 - 对于新上传的照片进行人脸检测和识别 - 将人脸特征存储到数据库中,用于后续识别和查询 3、总结 我们可以通过Spring Boot和Vue.js对Face Recognition Library(face_recognition)进行集成,实现一个基于人脸识别的系统。同时,我们也应该意识到,在实现基于人脸识别的系统时,我们需要保持对隐私和数据保护的高度警惕性,避免出现不必要的隐私泄露情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值