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()
  • 相关阅读:
    用DOS把一个文件夹下的所有文件名打印到txt文件里
    多线程时的CreateDispatch问题
    [转载]惟楚有才
    [转] 数学专业参考书整理推荐
    大整数处理类(cpp文件)
    [转]英文文献检索
    大整数处理类(头文件)
    国外遥感GIS期刊整理(转)
    [转载]Hibernate中No row with the given identifier exis
    在武汉,总有那么一个词~~~
  • 原文地址:https://www.cnblogs.com/tianqianlan/p/11615375.html
Copyright © 2011-2022 走看看