zoukankan      html  css  js  c++  java
  • 调用百度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()
  • 相关阅读:
    JSON 序列化类 南京酷得软件
    哈哈哈哈哈哈 找回记忆
    Presto
    (转)在Total Commander下使用SVN
    在ubuntu12.04,64位中安装lnmp一键包mysql的问题
    阿里云服务器上搭建php环境+redis
    在ubuntu12.04,64位中安装nginx+php+redis+mysql
    Redis篇:单线程I/O模型
    工具篇:apachehttpClient 和 jdk11HttpClient的使用
    技能篇:关于缓存数据的一致性探讨
  • 原文地址:https://www.cnblogs.com/tianqianlan/p/11615375.html
Copyright © 2011-2022 走看看