Python实现人脸识别检测, 对美女主播照片进行评分排名

前言

嗨喽,大家好呀~这里是爱看美女的茜茜呐


素材、视频、代码、插件安装教程我都准备好了,直接在文末名片自取就可点击此处跳转


开发环境:

  • Python 3.8

  • Pycharm 2021.2

模块使用:

  • requests >>> pip install requests

  • tqdm >>> pip install tqdm 简单实现进度条效果

  • os 文件操作

  • base64

如果安装python第三方模块:

  1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车

  2. 在pycharm中点击Terminal(终端) 输入安装命令

本次案例:

一. 采集主播照片

“”"

  1. 发送请求, 模拟浏览器对于url地址发送请求

    伪装模拟 --> headers 请求头

    字典数据类型, 要构建完整键值对

    <Response [200]> 响应对象, 表示请求成功

“”"

请求链接

url = 'https://*****/cache.php?m=LiveList&do=getLiveListByPage&gameId=1663&tagAll=0&page=2'

模拟浏览器

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}

发送请求

response = requests.get(url=url, headers=headers)

“”"

  1. 获取数据, 获取服务器返回响应数据

    开发者工具: response

  • requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    原因: 获取数据不是完整json数据格式

    解决:

    1. 获取文本数据, 查看数据返回效果

    2. 通过正则表达式提取数据

      删掉 请求链接 里面参数 Callback

  1. 解析数据, 提取我们想要的数据内容

    照片url / 昵称

    response.json() --> 字典数据类型

    根据键值对取值 --> 根据冒号左边的内容[键], 提取冒号右边的内容[值]

“”"

for循环遍历, 一个一个提取列表里面元素

for index in response.json()['data']['datas']:

提取照片

    img_url = index['screenshot']

提取昵称

    name = index['nick']
    print(name, img_url)

“”"

  1. 保存数据 --> 需要对图片链接发送请求, 获取二进制<图片>数据

‘img\’<文件夹> + name<文件名> + ‘.jpg’<文件格式>, mode=‘wb’<二进制保存>

“”"

获取图片二进制数据

    img_content = requests.get(url=img_url, headers=headers).content

保存数据

    with open('img\\' + name + '.jpg', mode='wb') as f:
        f.write(img_content)
二. 对于照片进行人脸识别检测, 进行颜值评分

使用百度云API接口

  1. 注册一个百度云账号

  2. 创建应用 --> 领取免费资源

  3. 点击技术文档

  4. Access Token获取

导入数据请求模块

–> 第三方模块, 需要安装 pip install requests

import requests
import base64
import os
import time
from tqdm import tqdm
def score(file):

“”"

定义函数

:param file: 文件路径

“”"

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
    }

client_id 为官网获取的AK, client_secret 为官网获取的SK

    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=AK&client_secret=SK'
    response = requests.get(host, headers=headers)
    access_token = response.json()['access_token']    

读取一张图片数据

    img_content = open(file, mode='rb').read()
    base_data = base64.b64encode(img_content)
    request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
    params = {
        # 图片数据
        "image": base_data,
        "image_type": "BASE64",
        "face_field": "beauty"
    }
    request_url = request_url + "?access_token=" + access_token
    headers_1 = {'content-type': 'application/json'}
    json_data = requests.post(request_url, data=params, headers=headers_1).json()
    try:
        num = json_data['result']['face_list'][0]['beauty']
        return num
    except:
        return '识别失败'
info_list = []

对于所有照片进行颜值检测 --> 获取文件路径/文件名字

files = os.listdir('img\\')
print('正在做颜值评分, 请稍后.....')
for file in tqdm(files):
    # 延时请求慢点
    time.sleep(0.5)
    # 完整的路径
    filename = 'img\\' + file
    # 切片
    name = file[:-4]
    result = score(file=filename)
    if result != '识别失败':
        dit = {
            '主播': name,
            '颜值': result
        }
        # 列表添加元素
        info_list.append(dit)

info_list.sort(key=lambda x:x['颜值'], reverse=True)
i = 1
for info in info_list:
    print(f'颜值排名第{i}的是{info["主播"]}, 颜值评分是{info["颜值"]}')
    i += 1
三. 评分排名

检测得对照标准:


尾语

感谢你观看我的文章呐~本次航班到这里就结束啦 🛬

希望本篇文章有对你带来帮助 🎉,有学习到一点知识~

躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。

最后,宣传一下呀~👇👇👇更多源码、资料、素材、解答、交流皆点击下方名片获取呀👇👇👇

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值