使用python实现男神女神颜值打分系统(推荐)


Posted in Python onOctober 31, 2019

先给大家展示效果图,感觉不错,请参考实现代码。

使用python实现男神女神颜值打分系统(推荐)
使用python实现男神女神颜值打分系统(推荐)

具体代码如下所示:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
pip install pillow
pip install baidu-aip
pip install tkinter
"""
import PIL
import time
import base64
import tkinter as tk
from PIL import Image
from PIL import ImageTk
from aip import AipFace
from tkinter.filedialog import askopenfilename
# 配置百度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
class ScoreSystem():
  """打分系统类"""
  root = tk.Tk()
  # 修改程序框的大小
  root.geometry('800x500')
  # 添加程序框标题
  root.title('女神/男神颜值打分系统')
  # 修改背景色
  canvas = tk.Canvas(root,
            width=800, # 指定Canvas组件的宽度
            height=500, # 指定Canvas组件的高度
            bg='#E6E6FA') # 指定Canvas组件的背景色
  canvas.pack()
  def start_interface(self):
    """主运行函数"""
    self.title()
    self.time_component()
    # 打开本地文件
    tk.Button(self.root, text='打开文件', command=self.show_original_pic).place(x=50, y=150)
    # 进行颜值评分
    tk.Button(self.root, text='运行程序', command=self.open_files2).place(x=50, y=230)
    # 显示帮助文档
    tk.Button(self.root, text='帮助文档', command=self.show_help).place(x=50, y=310)
    # 退出系统
    tk.Button(self.root, text='退出软件', command=self.quit).place(x=50, y=390)
    # 显示图框标题
    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=265, y=150)
    # 显示图片位置
    self.label_img_original.place(x=265, y=150)
    # 设置评分标签
    tk.Label(self.root, text='性别', font=10).place(x=680, y=150)
    self.text1 = tk.Text(self.root, width=10, height=2)
    tk.Label(self.root, text='年龄', font=10).place(x=680, y=250)
    self.text2 = tk.Text(self.root, width=10, height=2)
    tk.Label(self.root, text='评分', font=10).place(x=680, y=350)
    self.text3 = tk.Text(self.root, width=10, height=2)
    # 填装文字
    self.text1.place(x=680, y=175)
    self.text2.place(x=680, y=285)
    self.text3.place(x=680, y=385)
    # 开启循环
    self.root.mainloop()
  def show_original_pic(self):
    """放入文件"""
    self.path_ = askopenfilename(title='选择文件')
    # 处理文件
    img = Image.open(fr'{self.path_}')
    img = img.resize((270, 270), PIL.Image.ANTIALIAS) # 调整图片大小至270*270
    # 生成tkinter图片对象
    img_png_original = 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)
  def open_files2(self):
    # 获取百度API接口获得的年龄、分数、性别
    age, score, gender = face_score(self.path_)
    # 清楚text文本框内容并进行插入
    self.text1.delete(1.0, tk.END)
    self.text1.tag_config('red', foreground='RED')
    self.text1.insert(tk.END, gender, 'red')
    self.text2.delete(1.0, tk.END)
    self.text2.tag_config('red', foreground='RED')
    self.text2.insert(tk.END, age, 'red')
    self.text3.delete(1.0, tk.END)
    self.text3.tag_config('red', foreground='RED')
    self.text3.insert(tk.END, score, 'red')
  def show_help(self):
    """显示帮助"""
    pass
  def quit(self):
    """退出"""
    self.root.quit()
  def get_time(self, lb):
    """获取时间"""
    time_str = time.strftime("%Y-%m-%d %H:%M:%S") # 获取当前的时间并转化为字符串
    lb.configure(text=time_str) # 重新设置标签文本
    self.root.after(1000, self.get_time, lb) # 每隔1s调用函数 get_time自身获取时间
  def time_component(self):
    """时间组件"""
    lb = tk.Label(self.root, text='', fg='blue', font=("黑体", 15))
    lb.place(relx=0.75, rely=0.90)
    self.get_time(lb)
  def title(self):
    """标题设计"""
    lb = tk.Label(self.root, text='女神/男神颜值打分系统',
           bg='#6495ED',
           fg='lightpink', font=('华文新魏', 32),
           width=20,
           height=2,
           # relief=tk.SUNKEN
           )
    lb.place(x=200, y=10)
score_system = ScoreSystem()
score_system.start_interface()

总结

以上所述是小编给大家介绍的使用python实现男神女神颜值打分系统,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
Python基于pygame实现的弹力球效果(附源码)
Nov 11 Python
Python3 伪装浏览器的方法示例
Nov 23 Python
Python3实现购物车功能
Apr 18 Python
Python3.7 新特性之dataclass装饰器
May 27 Python
python web框架中实现原生分页
Sep 08 Python
pyinstaller还原python代码过程图解
Jan 08 Python
对python中 math模块下 atan 和 atan2的区别详解
Jan 17 Python
python使用pyecharts库画地图数据可视化的实现
Mar 25 Python
基于python实现检索标记敏感词并输出
May 07 Python
Python命名空间及作用域原理实例解析
Aug 12 Python
如何用Django处理gzip数据流
Jan 29 Python
如何在Python项目中引入日志
May 31 Python
python实现根据文件格式分类
Oct 31 #Python
Python简易计算器制作方法代码详解
Oct 31 #Python
python3 pillow模块实现简单验证码
Oct 31 #Python
利用Python校准本地时间的方法教程
Oct 31 #Python
python实现计算器功能
Oct 31 #Python
python中的Elasticsearch操作汇总
Oct 30 #Python
django实现用户注册实例讲解
Oct 30 #Python
You might like
PHP利用Mysql锁解决高并发的方法
2018/09/04 PHP
laravel5表单唯一验证的实例代码
2019/09/30 PHP
event对象的方法 兼容多浏览器
2009/06/27 Javascript
jquery+json实现数据列表分页示例代码
2013/11/15 Javascript
Javascript添加监听与删除监听用法详解
2014/12/19 Javascript
深入理解MVC中的时间js格式化
2016/05/19 Javascript
form+iframe解决跨域上传文件的方法
2016/11/18 Javascript
jquery网页加载进度条的实现
2017/06/01 jQuery
在一般处理程序(ashx)中弹出js提示语
2017/08/16 Javascript
CSS3结合jQuery实现动画效果及回调函数的实例
2017/12/27 jQuery
vue.js中npm安装教程图解
2018/04/10 Javascript
vue实现循环切换动画
2018/10/17 Javascript
微信小程序日历/日期选择插件使用方法详解
2018/12/28 Javascript
在layui中layer弹出层点击事件无效的解决方法
2019/09/05 Javascript
解决Vue 刷新页面导航显示高亮位置不对问题
2019/12/25 Javascript
[02:41]DOTA2英雄基础教程 亚巴顿
2014/01/02 DOTA
[15:15]教你分分钟做大人:狙击手
2014/10/30 DOTA
使用python Django做网页
2013/11/04 Python
python中使用百度音乐搜索的api下载指定歌曲的lrc歌词
2014/07/18 Python
Python中利用sqrt()方法进行平方根计算的教程
2015/05/15 Python
Python的Flask框架及Nginx实现静态文件访问限制功能
2016/06/27 Python
python编程实现希尔排序
2017/04/13 Python
网站渗透常用Python小脚本查询同ip网站
2017/05/08 Python
Python django实现简单的邮件系统发送邮件功能
2017/07/14 Python
Python二叉树定义与遍历方法实例分析
2018/05/25 Python
对python多线程SSH登录并发脚本详解
2019/02/14 Python
Python 存储字符串时节省空间的方法
2019/04/23 Python
python视频按帧截取图片工具
2019/07/23 Python
django多对多表的创建,级联删除及手动创建第三张表
2019/07/25 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
Python中SQLite如何使用
2020/05/27 Python
H5页面适配iPhoneX(就是那么简单)
2019/12/02 HTML / CSS
老公给老婆的道歉信
2014/01/10 职场文书
优秀教师先进事迹
2014/01/22 职场文书
优秀创业计划书分享
2019/07/19 职场文书
js作用域及作用域链工作引擎
2022/07/07 Javascript