使用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中利用Into包整洁地进行数据迁移的教程
Mar 30 Python
Python3实现从文件中读取指定行的方法
May 22 Python
详解python单例模式与metaclass
Jan 15 Python
详解python之简单主机批量管理工具
Jan 27 Python
pyqt实现.ui文件批量转换为对应.py文件脚本
Jun 19 Python
python 实现dict转json并保存文件
Dec 05 Python
python输出数组中指定元素的所有索引示例
Dec 06 Python
Python爬虫实现模拟点击动态页面
Mar 05 Python
Python selenium爬虫实现定时任务过程解析
Jun 08 Python
Django Auth用户认证组件实现代码
Oct 13 Python
Python中Cookies导出某站用户数据的方法
May 17 Python
详解PyTorch模型保存与加载
Apr 28 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配置ZendOpcache插件加速
2019/02/14 PHP
thinkphp框架表单数组实现图片批量上传功能示例
2020/04/04 PHP
javascript showModalDialog,open取得父窗口的方法
2010/03/10 Javascript
jQuery Ajax 实例全解析
2011/04/20 Javascript
Ext中下拉列表ComboBox组件store数据格式用法介绍
2013/07/15 Javascript
javascript解三阶幻方(九宫格)
2015/04/22 Javascript
js+html5绘制图片到canvas的方法
2015/06/05 Javascript
JS实现适合于后台使用的动画折叠菜单效果
2015/09/21 Javascript
微信小程序 弹窗自定义实例代码
2017/03/08 Javascript
JavaScript创建对象方式总结【工厂模式、构造函数模式、原型模式等】
2018/12/19 Javascript
Vue起步(无cli)的啊教程详解
2019/04/11 Javascript
浅谈JavaScript中等号、双等号、 三等号的区别
2020/08/06 Javascript
JavaScript实现多文件下载方法解析
2020/08/07 Javascript
微信小程序实现选项卡滑动切换
2020/10/22 Javascript
python实现定时同步本机与北京时间的方法
2015/03/24 Python
OpenCV实现人脸识别
2017/04/07 Python
python生成圆形图片的方法
2020/03/25 Python
Django urls.py重构及参数传递详解
2019/07/23 Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
2019/08/07 Python
如何为Python终端提供持久性历史记录
2019/09/03 Python
详解Python IO口多路复用
2020/06/17 Python
html5 canvas 使用示例
2010/10/22 HTML / CSS
加拿大领先的冒险和户外零售商:Atmosphere
2017/12/19 全球购物
创造美妙香氛体验:Aera扩散器和香水
2018/11/25 全球购物
韩国最大的购物网站:Gmarket
2019/06/20 全球购物
金智子午JAVA面试题
2015/09/04 面试题
招聘专员岗位职责
2014/03/07 职场文书
5.12护士节演讲稿
2014/04/30 职场文书
企业宣传工作方案
2014/06/02 职场文书
伊琍体标语
2014/06/25 职场文书
社区娱乐活动方案
2014/08/21 职场文书
中学生秋季运动会广播稿
2014/09/21 职场文书
2014年社区矫正工作总结
2014/11/18 职场文书
2015年高中班主任工作总结
2015/04/30 职场文书
大学生村官工作心得体会
2016/01/23 职场文书
咖啡厅里的创业计划书
2019/08/21 职场文书