zoukankan      html  css  js  c++  java
  • 065 女神颜值打分系统

    一、引入

    杨幂和杨超越到底谁更美,用Python做了一个女神颜值打分系统

    065-女神颜值打分系统-11.jpg?x-oss-process=style/watermark

    1.0.0.1 啊呀天气越来越热啦,校园里,地铁上的美女小姐姐越来越多,都说夏天是恋爱的季节,到时什么样的才算是美女呢?其实我还是觉得电视上的女神好看

    ~~看小美和小灰已经开始理论起来了,各执一词。

    065-女神颜值打分系统-02.jpg?x-oss-process=style/watermark

    065-女神颜值打分系统-03.jpg?x-oss-process=style/watermark

    065-女神颜值打分系统-04.jpg?x-oss-process=style/watermark

    065-女神颜值打分系统-05.jpg?x-oss-process=style/watermark

    下面就来讲讲我设计的这套颜值打分系统,先上图片让大家看一下效果,比如看一下杨幂的颜值如何:

    065-女神颜值打分系统-08.jpg?x-oss-process=style/watermark

    怎么样,结果是相当的精准吧,大家是不是已经跃跃欲试了呢?下面就针对该颜值打分系统进行讲解。

    二、注册百度API

    该系统最为核心的部分就是颜值的打分,这里其实是直接采用的是百度的人脸检测平台,大公司,打得分靠谱有保障,大家只需要打开下面的网址:

    http://ai.baidu.com/tech/face 然后点击“立即使用”后,创建自己的应用即可

    065-女神颜值打分系统-06.jpg?x-oss-process=style/watermark

    创建应用后,我们便可以得到自己的APP_ID 、API_KEY和SECRET_KEY 值,如下图所示:

    065-女神颜值打分系统-07.jpg?x-oss-process=style/watermark

    这三个值相当于我们的门牌号和钥匙,只有有这些值,我们才能够“打开门”。

    三、用Python调用百度API

    我们注册好了api之后,百度提供了Python接口,我们直接安装之后就可以非常方法的使用了。省去了我们自己用深度学习搭建模型的麻烦,有API真心好啊。

    • 先安装pip install baidu-aip 这个包非常重要,一定要先安装,然后引入AipFace这个库;
    • 接着我们需要把图片读取出来,因为图片是二进值的,所以我们用rb读取,然后把二进制的数据用base64加密,传给百度后端。
    • 然后调用aFace这个接口,把数据喂给它,获取它的json返回值,我们这里只取了年龄,颜值和性别。

    下面看一下核心的代码:

    # 配置百度aip参数
    APP_ID = '15768642'
    API_KEY = 'xhiiGmGPRCRj10XIqVlVeCky'
    SECRET_KEY = 'ZDMMAO7StwTKzW8BspVQxvoGtdgSW4yI'
    a_face = AipFace(APP_ID, API_KEY, SECRET_KEY)
    image_type = 'BASE64'
    
    options = {'face_field': 'age,gender,beauty'}
    
    
    def get_file_content(file_path):
        """获取文件内容"""
        with open(file_path, 'rb') as fr:
            content = base64.b64encode(fr.read())
    
            return content.decode('utf8')
    
    
    def face_score(file_path):
        """脸部识别分数"""
        result = a_face.detect(get_file_content(file_path), image_type, options)
        print(result)
        age = result['result']['face_list'][0]['age']
        beauty = result['result']['face_list'][0]['beauty']
        gender = result['result']['face_list'][0]['gender']['type']
    
        return age, beauty, gender
    

    四、用Tk做一个界面

    因为Python自带tk库,做GUI比较方便,我们这次的颜值打分系统直接用tk来完成。有兴趣的小伙伴可以用web搭建一个网页来玩一玩,大家先看一下我们搭建的界面:

    065-女神颜值打分系统-09.jpg?x-oss-process=style/watermark

    界面还是很简单的,主要的功能按钮在左右两边,左边是输入和运行,以及帮助按钮,右边是输出的结果,下面列出部分核心代码:

        def start_interface(self):
            tk.Button(self.root, text='打开文件', command=self.show_original_pic).place(x=50, y=120)
            # 进行颜值评分
            tk.Button(self.root, text='运行程序', command=self.openfiles2).place(x=50, y=200)
            # 显示帮助文档
            tk.Button(self.root, text='帮助文档', command=self.show_help).place(x=50, y=280)
            # 退出系统
            tk.Button(self.root, text='退出软件', command=self.quit).place(x=50, y=40)
    
            tk.Label(self.root, text='原图', font=10).place(x=380, y=120)
    
            self.label_img_original = tk.Label(self.root)
            self.cv_orinial = tk.Canvas(self.root, bg='white', width=270, height=270)
            self.cv_orinial.create_rectangle(8, 8, 260, 260, width=1, outline='red')
            self.cv_orinial.place(x=260, y=150)
            self.label_img_original.place(x=260, y=150)
    
            tk.Label(self.root, text='性别', font=10).place(x=780, y=120)
            self.text1 = tk.Text(self.root, width=10, height=2, font=('Helvetica', 10))
            tk.Label(self.root, text='年龄', font=10).place(x=780, y=220)
            self.text2 = tk.Text(self.root, width=10, height=2, font=('Helvetica', 10))
            tk.Label(self.root, text='评分', font=10).place(x=780, y=320)
            self.text3 = tk.Text(self.root, width=10, height=2, font=('Helvetica', 10))
            # tk.Text.configure(font='LibraryFontsHeiti.ttc')
    
            self.text1.place(x=760, y=150)
            self.text1.place(x=760, y=250)
            self.text1.place(x=760, y=350)
    
            self.root.mainloop()
    

    4个button都绑定了对应的函数;

    • 打开文件绑定show_original_pic()
    • 运行程序绑定open_file2()
    • 帮助文件绑定show_help()
    • 退出软件绑定quit()

    比如我们的打开文件button 就是绑定show_original_pic这个函数,读取图片文件,读取图片要用PIL模块来读取:

    def show_original_pic(self):
        self.path_ = tk.askopenfilename(title='选择文件')
        print(self.path_)
        img = PIL.Image.open(fr'{self.path_}')
        img = img.resize((270,270),PIL.Image.ANTIALIAS)  # 调整图片大小至270*270
        img_png_original = tk.ImageTk.PhotoImage(img)
        self.label_img_original.config(image=img_png_original)
        self.label_img_original.image = img_png_original  # 
        self.cv_orinial.create_image(5,5,anchor='nw',image=img_png_original)
    

    点击运行按钮,就是调用open_files2函数来获取我们前面的face_core函数分析的图片的年龄,颜值,性别,然后把这3个值填入到右边的文本框即可。

    写了这么多,大家想不想知道到底是杨幂的颜值高还是杨超越的颜值高,我运行了一下程序,发现还是杨幂的颜值高呀。

    065-女神颜值打分系统-10.jpg?x-oss-process=style/watermark

    Python是不是很神奇有趣,自动动手打造一个颜值评分系统,用数字给喜欢的女神打分。想想如果迪丽热巴和古力娜扎PK,到时谁更美。

    想要源码的同学可以添加我微信 nickchen121

  • 相关阅读:
    Orchard part8
    最有效地优化 Microsoft SQL Server 的性能
    MSSQL优化之索引优化
    Orchard使用中的坎坎坷坷
    GridView----CustomRowCellEdit 使用注意事项
    VS 编辑并继续(转载)
    Visual Studio 2010(.NET 4.0)中使用SQLite.NET
    .net环境下ckeditor与ckfinder中文文件链接乱码的问题
    jQuery常用方法集锦
    checkbox、select、radio的设置与获取
  • 原文地址:https://www.cnblogs.com/nickchen121/p/11209875.html
Copyright © 2011-2022 走看看