基于python的人脸识别工程

一、引入的第三方库:

icrawler:爬取图片

base64:将图片用base64编码,即将图片转化成码(字符)

baidu-aip:分析爬取图片的base64编码并转化为json

pyecharts:图形库,将数据展示在html上

此外,还需要安装一个chardet库

原理:

 二、功能:

1.分析颜值和年龄,求取各年龄段的平均值,并用pyecharts绘制成折线图。

2.分析性别的平均值,颜值的平均值,并绘为柱状图。

3.分析脸型转化为五边形图展示。

三、运行:

四、爬取的图片:

 

五、html数据分析展示:

 

 六、py代码:

from aip import AipFace
import base64
from icrawler.builtin import BingImageCrawler
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.charts import Page
import webbrowser
from pyecharts.charts import Radar
from pyecharts.charts import Line, Grid
from pyecharts.commons.utils import JsCode

AppId = '16966840'
ApiKey = 'B0e6QoxUB0gwQxxzWT6fCgMU'
SecretKey = 'rGQji0R0X76e4CP9rbcdPbcNRdS6EwC9'

def img2base64(path):
    file=open(path,'rb')
    data = file.read()
    base = base64.b64encode(data)
    base = base.decode('utf-8')
    file.close()
    return base


def parse_json(json): #从jason中提取想要属性
    if json['error_msg'] != "SUCCESS":
        print("人脸识别失败!")
        return
    else:
        age = json['result']['face_list'][0]['age']
        beauty = int(json['result']['face_list'][0]['beauty'])
        faceshape=json['result']['face_list'][0]['face_shape']['type']
        gender=json['result']['face_list'][0]['gender']['type']
        return {'age': age, 'beauty': beauty,'faceshape':faceshape,'gender':gender}



def draw_charts(age1,age2,age3,age4,age5, beauty,beauty1,beauty2,beauty3,beauty4,beauty5,malebeauty,femalebeauty,squarebeauty,trianglebeauty,ovalbeauty,heartbeauty,_roundbeauty,square,triganle,oval,heart,_round,male,female):
    if beauty1!=0:
        beauty1 = beauty1 / age1
    else :
        beauty1=0
    if beauty2 != 0:
        beauty2 = beauty2 / age2
    else:
        beauty2 = 0
    if beauty3 != 0:
        beauty3 = beauty3 / age3
    else:
        beauty3 = 0
    if beauty4 != 0:
        beauty4 = beauty4 / age4
    else:
        beauty4 = 0
    if beauty5 != 0:
        beauty5 = beauty5 / age5
    else:
        beauty5 = 0
    x_data = ["1-20岁", "21-40岁", "41-60岁", "61-80岁","81-100岁"]
    y_data = [beauty1, beauty2, beauty3, beauty4,beauty5]

    background_color_js = (
        "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
        "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
    )
    area_color_js = (
        "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
        "[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
    )

    d = (
        Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js)))
            .add_xaxis(xaxis_data=x_data)
            .add_yaxis(
            series_name="注册总量",
            y_axis=y_data,
            is_smooth=True,
            is_symbol_show=True,
            symbol="circle",
            symbol_size=6,
            linestyle_opts=opts.LineStyleOpts(color="#fff"),
            label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
            itemstyle_opts=opts.ItemStyleOpts(
                color="red", border_color="#fff", border_width=3
            ),
            tooltip_opts=opts.TooltipOpts(is_show=False),
            areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title="年龄颜值分数图",
                pos_bottom="5%",
                pos_left="center",
                title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
            ),
            xaxis_opts=opts.AxisOpts(
                type_="category",
                boundary_gap=False,
                axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
                axisline_opts=opts.AxisLineOpts(is_show=False),
                axistick_opts=opts.AxisTickOpts(
                    is_show=True,
                    length=25,
                    linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
                ),
                splitline_opts=opts.SplitLineOpts(
                    is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                ),
            ),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                position="right",
                axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
                ),
                axistick_opts=opts.AxisTickOpts(
                    is_show=True,
                    length=15,
                    linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
                ),
                splitline_opts=opts.SplitLineOpts(
                    is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
                ),
            ),
            legend_opts=opts.LegendOpts(is_show=False),
        )
    )

    (
        Grid()
            .add(
            d,
            grid_opts=opts.GridOpts(
                pos_top="20%",
                pos_left="10%",
                pos_right="10%",
                pos_bottom="15%",
                is_contain_label=True,
            ),
        )

    )


    if malebeauty != 0:
        malebeauty = malebeauty / male
    else:
        malebeauty = 0
    if femalebeauty != 0:
        femalebeauty = femalebeauty / female
    else:
        femalebeauty = 0
    bar = Bar()
    bar.add_xaxis(["性别"])
    bar.add_yaxis("男性", [malebeauty])
    bar.add_yaxis("女性", [femalebeauty])
    bar.set_global_opts(title_opts=opts.TitleOpts(title="性别颜值分数图"))

    if squarebeauty != 0:
        squarebeauty = squarebeauty / square
    else:
        squarebeauty = 0
    if trianglebeauty != 0:
        trianglebeauty = trianglebeauty / triangle
    else:
        trianglebeauty = 0
    if ovalbeauty != 0:
        ovalbeauty = ovalbeauty / oval
    else:
        ovalbeauty = 0
    if heartbeauty != 0:
        heartbeauty = heartbeauty/ heart
    else:
        heartbeauty = 0
    if _roundbeauty != 0:
        _roundbeauty = _roundbeauty / _round
    else:
        _roundbeauty = 0
    v1 = [[squarebeauty, trianglebeauty, ovalbeauty, heartbeauty, _roundbeauty]]

    c = (
        Radar(init_opts=opts.InitOpts(bg_color="#99CCFF"))
            .add_schema(
            schema=[
                opts.RadarIndicatorItem(name="方脸(square)", max_=100),
                opts.RadarIndicatorItem(name="瓜子脸(triangle)", max_=100),
                opts.RadarIndicatorItem(name="鹅蛋脸(oval)", max_=100),
                opts.RadarIndicatorItem(name="心形脸(heart)", max_=100),
                opts.RadarIndicatorItem(name="圆脸(round)", max_=100),

            ],
            splitarea_opt=opts.SplitAreaOpts(
                is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
            ),
            textstyle_opts=opts.TextStyleOpts(color="#FFFF00"),
        )

            .add(
            series_name="颜值分数(Actual Spending)",
            data=v1,
            linestyle_opts=opts.LineStyleOpts(color="#990033"),
        )
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
            .set_global_opts(
            title_opts=opts.TitleOpts(title="脸型颜值分数图"), legend_opts=opts.LegendOpts()
        )

    )
    d.chart_id="d78e83f5b2bb453e9acaac3ad5c349ad"
    bar.chart_id="8eb2e51dc59e43bca1ff5a9f6174cbfa"
    c.chart_id="45fb001fc510481fb33e18b76dbf9e65"
    page = Page(layout=Page.DraggablePageLayout)  # 创建一个组件可拖拽的效果
    # 把所有的统计图对象添加到页面中
    page.add(d,bar,c)
    # 生成网页
    page.render("数据大屏.html")

    # 读取本地配置文件,重新生成对应样式的网页
    # 参数1:原来的html文件
    # 参数2:JSON配置文件
    # 参数3:新生成的html文件
    Page.save_resize_html(source="数据大屏.html",
                          cfg_file="location.json",
                          dest="新数据大屏.html")


def detect_fase(base): #想要的json数据
    client = AipFace(AppId, ApiKey, SecretKey)
    options = {
        "face_field": "age,beauty,face_shape,gender"
    }
    return client.detect(base, "BASE64", options)

def parse_json(json):
    """
    解析所需的人来拿数据
    :param json: 服务器返回的原始JSON数据
    :return: 只包含所需数据的字典
    """
    if json['error_msg'] != "SUCCESS":
        print("人脸识别失败!")
        return
    else:#从jason中提取想要属性
         # 拿到年龄和颜值
        age = json['result']['face_list'][0]['age']
        beauty = json['result']['face_list'][0]['beauty']
        faceshape = json['result']['face_list'][0]['face_shape']['type']
        gender = json['result']['face_list'][0]['gender']['type']
        return {'age': age, 'beauty': beauty, 'faceshape': faceshape, 'gender': gender}



bing_crawler = BingImageCrawler(
    feeder_threads=1,
    parser_threads=2,
    downloader_threads=4,
    storage={'root_dir': 'test'})
max=int(input("请输入图片最大数量:"))
kw=input("请输入关键词:")
filters = dict(
    size='large',
    #color='orange',
    #license='commercial,modify',
    #date=((2017, 1, 1), (2017, 11, 30))
    )
bing_crawler.crawl(keyword=kw,filters=filters,max_num=max)

if __name__ == '__main__':
    square = 0
    triangle = 0
    oval = 0
    heart = 0
    _round = 0
    age1 = 0
    age2 = 0
    age3 = 0
    age4 = 0
    age5 = 0
    male=0
    female=0
    beauty1=0
    beauty2=0
    beauty3=0
    beauty4=0
    beauty5=0
    malebeauty=0
    femalebeauty=0
    squarebeauty=0
    trianglebeauty=0
    ovalbeauty=0
    heartbeauty=0
    _roundbeauty=0

    for i in range(1,max+1):
        if i<10:
            location=f"test/00000{i}.jpg"
            #print(f"test/00000{i}.jpg")
            result = img2base64(location)
            print(result)
            result = detect_fase(result)
            print(result)
            result = parse_json(result)
            if 0 < result['age'] <= 20:
                age1 += 1
                beauty1+=result['beauty']
            if 20 < result['age'] <= 40:
                age2 += 1
                beauty2 += result['beauty']
            if 40 < result['age'] <= 60:
                age3 += 1
                beauty3 += result['beauty']
            if 60 < result['age'] <= 80:
                age4 += 1
                beauty4 += result['beauty']
            if 80 < result['age'] <= 100:
                age5 += 1
                beauty5 += result['beauty']
            if result['faceshape'] == "square":
                square += 1
                squarebeauty += result['beauty']
            if result['faceshape'] == "triangle":
                triangle += 1
                trianglebeauty += result['beauty']
            if result['faceshape'] == "oval":
                oval += 1
                ovalbeauty += result['beauty']
            if result['faceshape'] == "heart":
                heart += 1
                heartbeauty += result['beauty']
            if result['faceshape'] == "round":
                _round += 1
                _roundbeauty += result['beauty']
            if result['gender']=="male":
                male+=1
                malebeauty+=result['beauty']
            if result['gender'] == "female":
                female+=1
                femalebeauty += result['beauty']
        if 10<=i<100:
            location = f"test/0000{i}.jpg"
            result = img2base64(location)
            print(result)
            result = detect_fase(result)
            print(result)
            result = parse_json(result)
            if 0 < result['age'] <= 20:
                age1 += 1
                beauty1 += result['beauty']
            if 20 < result['age'] <= 40:
                age2 += 1
                beauty2 += result['beauty']
            if 40 < result['age'] <= 60:
                age3 += 1
                beauty3 += result['beauty']
            if 60 < result['age'] <= 80:
                age4 += 1
                beauty4 += result['beauty']
            if 80 < result['age'] <= 100:
                age5 += 1
                beauty5 += result['beauty']
            if result['faceshape'] == "square":
                square += 1
                squarebeauty += result['beauty']
            if result['faceshape'] == "triangle":
                triangle += 1
                trianglebeauty += result['beauty']
            if result['faceshape'] == "oval":
                oval += 1
                ovalbeauty += result['beauty']
            if result['faceshape'] == "heart":
                heart += 1
                heartbeauty += result['beauty']
            if result['faceshape'] == "round":
                _round += 1
                _roundbeauty += result['beauty']
            if result['gender'] == "male":
                male += 1
                malebeauty += result['beauty']
            if result['gender'] == "female":
                female += 1
                femalebeauty += result['beauty']
        if i>=100:
            location = f"test/000{i}.jpg"
            result = img2base64(location)
            print(result)
            result = detect_fase(result)
            print(result)
            result = parse_json(result)
            if 0 < result['age'] <= 20:
                age1 += 1
                beauty1 += result['beauty']
            if 20 < result['age'] <= 40:
                age2 += 1
                beauty2 += result['beauty']
            if 40 < result['age'] <= 60:
                age3 += 1
                beauty3 += result['beauty']
            if 60 < result['age'] <= 80:
                age4 += 1
                beauty4 += result['beauty']
            if 80 < result['age'] <= 100:
                age5 += 1
                beauty5 += result['beauty']
            if result['faceshape'] == "square":
                square += 1
                squarebeauty+=result['beauty']
            if result['faceshape'] == "triangle":
                triangle += 1
                trianglebeauty += result['beauty']
            if result['faceshape'] == "oval":
                oval += 1
                ovalbeauty += result['beauty']
            if result['faceshape'] == "heart":
                heart += 1
                heartbeauty += result['beauty']
            if result['faceshape'] == "round":
                _round += 1
                _roundbeauty += result['beauty']
            if result['gender'] == "male":
                male += 1
                malebeauty += result['beauty']
            if result['gender'] == "female":
                female += 1
                femalebeauty += result['beauty']

    draw_charts(age1, age2, age3, age4, age5, result['beauty'],beauty1,beauty2,beauty3,beauty4,beauty5,malebeauty,femalebeauty,squarebeauty,trianglebeauty,ovalbeauty,heartbeauty,_roundbeauty, square, triangle, oval, heart, _round, male, female)
    # 自动弹出浏览器,由于环境原因,没用
    webbrowser.open_new('新数据大屏.html')

 七、总结:

以上就是学校安排的实训中所完成的项目,让我认识到了python在图片处理上的强大之处,也对于python的使用有了更深一步的理解。 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值