使用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抓取京东商城手机列表url实例代码
Dec 18 Python
给Python的Django框架下搭建的BLOG添加RSS功能的教程
Apr 08 Python
Windows上配置Emacs来开发Python及用Python扩展Emacs
Nov 20 Python
Python单例模式的两种实现方法
Aug 14 Python
在python中使用正则表达式查找可嵌套字符串组
Oct 24 Python
TensorFlow实现卷积神经网络CNN
Mar 09 Python
解决python 无法加载downsample模型的问题
Oct 25 Python
浅谈python下tiff图像的读取和保存方法
Dec 04 Python
Python 获取指定文件夹下的目录和文件的实现
Aug 30 Python
利用scikitlearn画ROC曲线实例
Jul 02 Python
Django中的JWT身份验证的实现
May 07 Python
Python实现自动玩连连看的脚本分享
Apr 04 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
phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法
2011/08/18 PHP
深入Memcache的Session数据的多服务器共享详解
2013/06/13 PHP
php自定义类fsocket模拟post或get请求的方法
2015/07/31 PHP
中高级PHP程序员应该掌握哪些技术?
2016/09/23 PHP
用javascript替换URL中的参数值示例代码
2014/01/27 Javascript
jquery获取当前点击对象的value方法
2014/02/28 Javascript
jQuery中mouseover事件用法实例
2014/12/26 Javascript
bootstrap模态框实现拖拽效果
2016/12/14 Javascript
JS使用正则实现去掉字符串左右空格的方法
2016/12/27 Javascript
JS ES6中setTimeout函数的执行上下文示例
2017/04/27 Javascript
js+css实现红包雨效果
2018/07/12 Javascript
微信小程序以7天为周期连续签到7天功能效果的示例代码
2020/08/20 Javascript
JS获取当前时间戳方法解析
2020/08/29 Javascript
[02:28]DOTA2亚洲邀请赛 LGD战队巡礼
2015/02/03 DOTA
python查询mysql中文乱码问题
2014/11/09 Python
python3使用PyMysql连接mysql数据库实例
2017/02/07 Python
教你用Python写安卓游戏外挂
2018/01/11 Python
Django rest framework基本介绍与代码示例
2018/01/26 Python
python代码过长的换行方法
2018/07/19 Python
华为校园招聘上机笔试题 扑克牌大小(python)
2020/04/22 Python
关于TensorFlow新旧版本函数接口变化详解
2020/02/10 Python
Pytorch mask_select 函数的用法详解
2020/02/18 Python
Python处理mysql特殊字符的问题
2020/03/02 Python
Python实现发票自动校核微信机器人的方法
2020/05/22 Python
基于Modernizr 让网站进行优雅降级的分析
2013/04/21 HTML / CSS
Everything But Water官网:美国泳装品牌
2019/03/17 全球购物
师范生教师实习自我鉴定
2013/09/27 职场文书
高一家长会邀请函
2014/01/12 职场文书
法学毕业生自我鉴定
2014/01/31 职场文书
活着观后感
2015/06/03 职场文书
大学生干部培训心得体会
2016/01/06 职场文书
Mysql中 unique列插入重复值该怎么解决呢
2021/05/26 MySQL
MySQL提取JSON字段数据实现查询
2022/04/22 MySQL
SpringBoot 集成短信和邮件 以阿里云短信服务为例
2022/04/22 Java/Android
win10忘记pin密码登录不了怎么办?win10忘记pin密码登不进去的解决方法
2022/07/07 数码科技
Redis sentinel哨兵集群的实现步骤
2022/07/15 Redis