我们知道现在有一些网站或者软件是用照片来测试美颜的。其实这个功能可以用Python来实现。在这篇文章中,我们使用Python编写了一个美颜测试工具。简介要实现美颜测试功能,大致有两种方式:一种是自己写检测功能,另一种是使用第三方接口实现检测功能,比如:百度云接口。本文为了方便,我们使用百度云接口,接口的注册这里不再赘述。注册流程不清楚的可以参考我之前写的车牌识别文章:https://blog.csdn.net/ityard/article/details/105673451。我们需要用到的Python库主要有:pillow、baidu-aip、tkinter,可以使用pipinstallpillow/baidu-aip/tkinter安装。实现首先我们看一下如何通过百度云接口使用照片获取性别、年龄、外貌信息。代码实现如下:APP_ID='你自己的APP_ID'API_KEY='你自己的API_KEY'SECRET_KEY='你自己的SECRET_KEY'face=AipFace(APP_ID,API_KEY,SECRET_KEY)image_type='BASE64'options={'face_field':'年龄、性别、美女'}defget_file_base64(file_path):withopen(file_path,'rb')asfr:content=base64.b64encode(fr.read())returncontent.decode('utf8')defget_score(file_path):#人脸识别分数result=face.detect(get_file_base64(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']返回年龄,美貌,gender这里我们使用tkinter创建一个GUI,用于图片选择和接口调用操作。我们来看看代码的主要实现。首先,我们创建一个窗口,代码实现如下:root=tk.Tk()#设置窗口大小root.geometry('700x450')#给窗口添加标题root.title('色值测试工具')#设置背景颜色canvas=tk.Canvas(root,width=700,height=450,bg='#EEE8AA')canvas.pack()然后我们在窗口中添加两个按钮,一个用于选择照片,另一个用于调用接口,代码实现如下:#图片选择按钮tk.Button(self.root,text='SelectPhoto',font=('ChineseXingkai',16),command=self.show_img).place(x=40,y=180)#颜值测试按钮tk.Button(self.root,text='查看颜值',font=('中国兴凯',16),command=self.set_score).place(x=40,y=280)我们还需要创建三个输入框来显示接口返回的性别、年龄和外貌信息。代码实现如下:tk.Label(self.root,text='gender',bg='#EEE8AA',fg='#0AB0D5',font=('中文兴凯',20)).place(x=500,y=150)self.text1=tk.Text(self.root,width=10,height=2)tk.Label(self.root,text='age',bg='#EEE8AA',fg='#0AB0D5',font=('中文行楷',20)).place(x=500,y=260)self.text2=tk.Text(self.root,width=10,height=2)tk.Label(self.root,text='面值',bg='#EEE8AA',fg='#0AB0D5',font=('中国行凯',20)).place(x=500,y=360)self.text3=tk.Text(self.root,width=10,height=2)#填充文字self.text1.place(x=580,y=150)self.text2.place(x=580,y=260)self.text3.place(x=580,y=360)看实现效果:源码在后台关注下方公众号,回复200613获取
