Write a beauty testing tool in Python

Keywords: Python pip

We know that there are some websites or software that use photos to test the beauty. In fact, this function can be realized by using python. In this article, we use Python to write a beauty test widget.

brief introduction

There are two ways to realize the beauty test function: one is to write the test function by yourself, and the other is to realize the test function with the help of the third-party interface, such as Baidu cloud interface. For convenience, we use Baidu cloud interface in this paper. The registration of the interface is not mentioned here. If you are not clear about the registration process, you can refer to the license plate recognition I wrote before Articles: https://blog.csdn.net/ityard/article/details/105673451 .

The Python libraries we need to use mainly include: pilot, baidu AIP, tkinter. You can install and use PIP install pilot / Baidu AIP / tkinter.


First, let's take a look at how to use photos to obtain gender, age and beauty information through Baidu cloud interface. The code is as follows:

APP_ID = 'own APP_ID'
image_type = 'BASE64'
options = {'face_field': 'age,gender,beauty'}

def get_file_base64(file_path):
  with open(file_path, 'rb') as fr:
    content = base64.b64encode(fr.read())
    return content.decode('utf8')

def get_score(file_path):
  # Face recognition score
  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']
  return age, beauty, gender

Here we use tkinter to create GUI for photo selection and interface call. Let's see the main implementation of the code.

First, we create a window. The code is as follows:

root = tk.Tk()
# Set window size
# Add title to window
root.title('Beauty test tool')
# Set background color
canvas = tk.Canvas(root,

We then add two buttons to the window, one for selecting photos and the other for calling interfaces. The code is as follows:

# Photo selection button
  tk.Button(self.root, text='Choose a photo', font=('Chinese Xingkai', 16), command=self.show_img).place(x=40, y=180)
  # Beauty test button
  tk.Button(self.root, text='View beauty', font=('Chinese Xingkai', 16), command=self.set_score).place(x=40, y=280)

We also need to create three input boxes to display the gender, age and beauty information returned by the interface. The code implementation is as follows:

tk.Label(self.root, text='Gender', bg='#EEE8AA', fg='#0AB0D5', font = (' Chinese script ', 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 = (' Chinese Xingkai ', 20)).place(x=500, y=260)
self.text2 = tk.Text(self.root, width=10, height=2)
tk.Label(self.root, text='level of appearance', bg='#EEE8AA', fg='#0AB0D5', font = (' Chinese Xingkai ', 20)).place(x=500, y=360)
self.text3 = tk.Text(self.root, width=10, height=2)
# Fill in text
self.text1.place(x=580, y=150)
self.text2.place(x=580, y=260)
self.text3.place(x=580, y=360)

Let's see the effect:

The source code is obtained by replying to 200613 in the background of the public account below.

Posted by rigy73 on Sat, 13 Jun 2020 20:27:35 -0700