使用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学习之编写查询ip程序
Feb 27 Python
Python 运行.py文件和交互式运行代码的区别详解
Jul 02 Python
Django使用unittest模块进行单元测试过程解析
Aug 02 Python
win10子系统python开发环境准备及kenlm和nltk的使用教程
Oct 14 Python
python安装gdal的两种方法
Oct 29 Python
django在开发中取消外键约束的实现
May 20 Python
matplotlib.pyplot.matshow 矩阵可视化实例
Jun 16 Python
使用Keras实现Tensor的相乘和相加代码
Jun 18 Python
Python 解析xml文件的示例
Sep 29 Python
详解基于Scrapy的IP代理池搭建
Sep 29 Python
Python Socket多线程并发原理及实现
Dec 11 Python
Python基于mediainfo批量重命名图片文件
Dec 29 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
常用星际术语索引(新手指南)
2020/03/04 星际争霸
PHP echo,print,printf,sprintf函数之间的区别与用法详解
2013/11/27 PHP
PHP数据库链接类(PDO+Access)实例分享
2013/12/05 PHP
对于ThinkPHP框架早期版本的一个SQL注入漏洞详细分析
2014/07/04 PHP
yii数据库的查询方法
2015/12/28 PHP
php求数组全排列,元素所有组合的方法总结
2017/03/14 PHP
让广告代码不再影响你的网页加载速度
2006/07/07 Javascript
用javascript实现分割提取页面所需内容
2007/05/09 Javascript
Domino中运用jQuery读取视图内容的方法
2009/10/21 Javascript
JS 无法通过W3C验证的处理方法
2010/03/09 Javascript
Javascript 网页黑白效果实现代码(兼容IE/FF等)
2010/04/23 Javascript
javascript 精粹笔记
2010/05/09 Javascript
JS识别浏览器类型(电脑浏览器和手机浏览器)
2016/11/18 Javascript
JS闭包与延迟求值用法示例
2016/12/22 Javascript
Input文本框随着输入内容多少自动延伸的实现
2017/02/15 Javascript
Vue和Bootstrap的整合思路详解
2017/06/30 Javascript
JavaScript创建对象的四种常用模式实例分析
2019/01/11 Javascript
Angular 2使用路由自定义弹出组件toast操作示例
2019/05/10 Javascript
[01:27:30]LGD vs Newbee 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
Python编程实现双链表,栈,队列及二叉树的方法示例
2017/11/01 Python
详谈python中冒号与逗号的区别
2018/04/18 Python
对Python中9种生成新对象的方法总结
2018/05/23 Python
Python绘制正余弦函数图像的方法
2018/08/28 Python
python使用pandas处理大数据节省内存技巧(推荐)
2019/05/05 Python
python队列Queue的详解
2019/05/10 Python
Tensorflow模型实现预测或识别单张图片
2019/07/19 Python
解决python 上传图片限制格式问题
2019/10/30 Python
利用python实现后端写网页(flask框架)
2021/02/28 Python
HTML5自定义属性前缀data-及dataset的使用方法(html5 新特性)
2017/08/24 HTML / CSS
详解webapp页面滚动卡顿的解决办法
2018/12/26 HTML / CSS
德国综合购物网站:OTTO
2018/11/13 全球购物
档案接收函范文
2014/01/10 职场文书
大学生优秀班干部事迹材料
2014/05/26 职场文书
团日活动总结模板
2014/06/25 职场文书
酒后驾车标语
2014/06/30 职场文书
Windows Server 2022 超融合部署(图文教程)
2022/06/25 Servers