目录python
杨幂和杨超越到底谁更美,用Python作了一个女神颜值打分系统web
~~看小美和小灰已经开始理论起来了,各执一词。json
下面就来说讲我设计的这套颜值打分系统,先上图片让你们看一下效果,好比看一下杨幂的颜值如何:后端
怎么样,结果是至关的精准吧,你们是否是已经跃跃欲试了呢?下面就针对该颜值打分系统进行讲解。api
该系统最为核心的部分就是颜值的打分,这里实际上是直接采用的是百度的人脸检测平台,大公司,打得分靠谱有保障,你们只须要打开下面的网址:微信
http://ai.baidu.com/tech/face 而后点击“当即使用”后,建立本身的应用便可函数
建立应用后,咱们即可以获得本身的APP_ID 、API_KEY和SECRET_KEY 值,以下图所示:oop
这三个值至关于咱们的门牌号和钥匙,只有有这些值,咱们才可以“打开门”。学习
咱们注册好了api以后,百度提供了Python接口,咱们直接安装以后就能够很是方法的使用了。省去了咱们本身用深度学习搭建模型的麻烦,有API真心好啊。ui
下面看一下核心的代码:
# 配置百度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
由于Python自带tk库,作GUI比较方便,咱们此次的颜值打分系统直接用tk来完成。有兴趣的小伙伴能够用web搭建一个网页来玩一玩,你们先看一下咱们搭建的界面:
界面仍是很简单的,主要的功能按钮在左右两边,左边是输入和运行,以及帮助按钮,右边是输出的结果,下面列出部分核心代码:
def start_interface(self): tk.Button(self.root, text='打开文件', command=self.show_original_pic).place(x=50, y=120) # 进行颜值评分 tk.Button(self.root, text='运行程序', command=self.openfiles2).place(x=50, y=200) # 显示帮助文档 tk.Button(self.root, text='帮助文档', command=self.show_help).place(x=50, y=280) # 退出系统 tk.Button(self.root, text='退出软件', command=self.quit).place(x=50, y=40) 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=260, y=150) self.label_img_original.place(x=260, y=150) tk.Label(self.root, text='性别', font=10).place(x=780, y=120) self.text1 = tk.Text(self.root, width=10, height=2, font=('Helvetica', 10)) tk.Label(self.root, text='年龄', font=10).place(x=780, y=220) self.text2 = tk.Text(self.root, width=10, height=2, font=('Helvetica', 10)) tk.Label(self.root, text='评分', font=10).place(x=780, y=320) self.text3 = tk.Text(self.root, width=10, height=2, font=('Helvetica', 10)) # tk.Text.configure(font='\Library\Fonts\Heiti.ttc') self.text1.place(x=760, y=150) self.text1.place(x=760, y=250) self.text1.place(x=760, y=350) self.root.mainloop()
4个button都绑定了对应的函数;
好比咱们的打开文件button 就是绑定show_original_pic这个函数,读取图片文件,读取图片要用PIL模块来读取:
def show_original_pic(self): self.path_ = tk.askopenfilename(title='选择文件') print(self.path_) img = PIL.Image.open(fr'{self.path_}') img = img.resize((270,270),PIL.Image.ANTIALIAS) # 调整图片大小至270*270 img_png_original = tk.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)
点击运行按钮,就是调用open_files2函数来获取咱们前面的face_core函数分析的图片的年龄,颜值,性别,而后把这3个值填入到右边的文本框便可。
写了这么多,你们想不想知道究竟是杨幂的颜值高仍是杨超越的颜值高,我运行了一下程序,发现仍是杨幂的颜值高呀。
Python是否是很神奇有趣,自动动手打造一个颜值评分系统,用数字给喜欢的女神打分。想一想若是迪丽热巴和古力娜扎PK,到时谁更美。
想要源码的同窗能够添加我微信 nickchen121