使用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简单格式化时间的方法【strftime函数】
Sep 18 Python
Python编程使用NLTK进行自然语言处理详解
Nov 16 Python
Python编程实现线性回归和批量梯度下降法代码实例
Jan 04 Python
谈谈python中GUI的选择
Mar 01 Python
详解Python 装饰器执行顺序迷思
Aug 08 Python
对python操作kafka写入json数据的简单demo分享
Dec 27 Python
浅谈Pandas Series 和 Numpy array中的相同点
Jun 28 Python
python类中super() 的使用解析
Dec 19 Python
python3实现从kafka获取数据,并解析为json格式,写入到mysql中
Dec 23 Python
python调用API接口实现登陆短信验证
May 10 Python
Python使用UDP实现720p视频传输的操作
Apr 24 Python
Python中zipfile压缩包模块的使用
May 14 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-redis中文文档介绍
2013/02/07 PHP
在win7中搭建Linux+PHP 开发环境
2014/10/08 PHP
Web程序工作原理详解
2014/12/25 PHP
php+croppic.js实现剪切上传图片功能
2018/08/14 PHP
JavaScript 学习笔记 Black.Caffeine 09.11.28
2009/11/30 Javascript
设置iframe的document.designMode后仅Firefox中其body.innerHTML为br
2012/02/27 Javascript
一个背景云变换js特效 鼠标移动背景云变化
2012/12/28 Javascript
node.js中的buffer.slice方法使用说明
2014/12/10 Javascript
js代码实现随机颜色的小方块
2015/07/30 Javascript
纯javascript模仿微信打飞机小游戏
2015/08/20 Javascript
js实现带农历和八字等信息的日历特效
2016/05/16 Javascript
JavaScript中Number对象的toFixed() 方法详解
2016/09/02 Javascript
输入框点击时边框变色效果的实现方法
2016/12/26 Javascript
微信小程序 input输入框详解及简单实例
2017/01/10 Javascript
vue router使用query和params传参的使用和区别
2017/11/13 Javascript
完美解决axios跨域请求出错的问题
2018/02/05 Javascript
实例讲解vue源码架构
2019/01/24 Javascript
一文快速了解JQuery中的AJAX
2019/05/31 jQuery
非常实用的jQuery代码段集锦【检测浏览器、滚动、复制、淡入淡出等】
2019/08/08 jQuery
微信小程序实现搜索框功能及踩过的坑
2020/06/19 Javascript
python开发环境PyScripter中文乱码问题解决方案
2016/09/11 Python
python使用正则表达式替换匹配成功的组
2017/11/17 Python
python检测空间储存剩余大小和指定文件夹内存占用的实例
2018/06/11 Python
tensorflow实现加载mnist数据集
2018/09/08 Python
关于ZeroMQ 三种模式python3实现方式
2019/12/23 Python
python数据类型可变不可变知识点总结
2020/03/06 Python
python中pickle模块浅析
2020/12/29 Python
美国现代家具和家居商店:Apt2B
2016/08/29 全球购物
英国精品买手店:Browns Fashion
2016/09/29 全球购物
Expedia英国:全球最大的在线旅游公司
2017/09/07 全球购物
欧洲第一的摇滚和金属乐队服装网站:EMP
2017/10/26 全球购物
专升本个人自我评价
2013/12/22 职场文书
领导视察欢迎词
2014/01/15 职场文书
财务学生的职业生涯发展
2014/02/11 职场文书
文员岗位职责
2015/02/04 职场文书
实现GO语言对数组切片去重
2022/04/20 Golang