准备作业
在写代码之前,你需要先在Baidu开发者平台申请权限,步骤如下:
登录百度智能云
- https://cloud.baidu.com/?from=console,没有Baidu账号的注册一个
- 第一次进入会有这样一个页面,你自己随意填
 .png) 
通过界面右上角进入控制台
.png)
进入控制台后点击左上角的选单栏
.png)
选中产品服务
.png)
点击创建应用
.png)
- 应用名称随便填
- 界面选择默认
- 应用归属选个人
- 应用描述随便填
- 然后点击立即创建
 .png) 
创建完毕后点击回传应用串列
重点点击领取免费资源
 .png)
进行实名认证后领取服务型别里面的所有内容
实名认证需要一定时间
 .png)
领取完毕之后回到应用串列
.png)
 复制API Key和Secret Key里的内容,用于后期的界面认证
先去要一些美女的照片素材回来做颜值检测
开发环境
- Python 3.8
- Pycharm 2021.2
- 会使用API界面 百度云界面
模块使用
- requests >>> pip install requests
- tqdm >>> pip install tqdm
- os
- base64
第一个阶段 去采集主播照片资料
请求资料
url = f'https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2168&tagAll=0&page=1'
# headers 请求头 伪装Python的代码 不被识别出来是爬虫程序...
# headers 是一个字典资料型别
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}
# 通过requests模块去对url地址发送请求
response = requests.get(url=url, headers=headers)
决议资料,提取我们想要资料内容,主播名字,主播封面图url地址
# json资料提取内容 根据冒号左边的内容 提取冒号右边内容
data_list = response.json()['data']['datas']
for index in data_list:
    # pprint.pprint(index)
    name = index['nick']
    img_url = index['screenshot']
翻页
for page in range(1, 11):
    url = f'https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2168&tagAll=0&page={page}'
保存图片资料内容
img_content = requests.get(url=img_url, headers=headers).content
# 'img\\' 档案路径 name 文件名字 '.jpg' 档案后缀 >>> 文件名
# mode 保存方式 wb 二进制模式写入
# as 重命名 为 f
filename = 'img_1\\'
if not os.path.exists(filename):
    os.mkdir(filename)
with open(filename + name + '.jpg', mode='wb') as f:
    f.write(img_content) # 写入资料
    print('正在保存: ', name)
.png)
.png)
颜值检测
呼叫界面进行识别
def get_beauty(img_base64):
    host = 'https://aip.baidubce.com/oauth/2.0/token'
    data = {
        'grant_type': 'client_credentials',
        'client_id': 'vXONiwhiVGlBaI2nRRIYLgz5',
        'client_secret': 'ouZMTMuCGLi7pbeg734ftNxn9h3qN7R4'
    }
    response = requests.get(url=host, params=data)
    token = response.json()['access_token']
    # print(token)
    '''
    人脸检测与属性分析
    '''
    request_url = f"https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token={token}"
    params = {
        "image": img_base64,  # 需要传递 图片 base64
        "image_type": "BASE64",
        "face_field": "beauty"
    }
    headers = {'content-type': 'application/json'}
    response = requests.post(request_url, data=params, headers=headers)
    try:
        beauty = response.json()['result']['face_list'][0]['beauty']
        return beauty
    except:
        return '识别失败'
获取所有图片,进行排名
lis = []
files = os.listdir('img_1\\')
print('正在识别人脸, 颜值检测中, 请稍后.....')
for file in tqdm(files):
    img_file = 'img_1\\' + file
    img_name = file.split('.')[0]
    # print(img_file)
    f = open(img_file, mode='rb')  # 读取一张图片内容
    img_base64 = base64.b64encode(f.read())
    beauty = get_beauty(img_base64)
    if beauty != '识别失败':
        dit = {
            '主播': img_name,
            '颜值': beauty,
        }
        lis.append(dit) # 把字典添加到空串列里面
    # print(f'{img_name}颜值评分是{beauty}')
lis.sort(key=lambda x:x['颜值'], reverse=True)
num = 1
# 前10张照片的颜值排名
for index in lis:
    print(f'颜值排名第{num}的是{index["主播"]}, 颜值评分是{index["颜值"]}')
    num += 1
.png)
 .png)
 .png)
看看排名情况
前三名
.png)
emmm,,,,,,
然我看来看看最后三名
.png)
.png)
 我不服,最后一名居然输给了一只熊和一个男的,而且才得22分?
 .png)
 看了下官方的档案,最后一名可能是因为手挡住了脸部,但被一只熊给打败了,就离谱
.gif)

 
							 
										
										 
										
										 
										
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										
0 评论