一、引入
楊冪和楊超越到底誰更美,用Python做了一個女神顏值打分系統
1.0.0.1 啊呀天氣越來越熱啦,校園里,地鐵上的美女小姐姐越來越多,都說夏天是戀愛的季節,到時什么樣的才算是美女呢?其實我還是覺得電視上的女神好看
~~看小美和小灰已經開始理論起來了,各執一詞。
下面就來講講我設計的這套顏值打分系統,先上圖片讓大家看一下效果,比如看一下楊冪的顏值如何:
怎么樣,結果是相當的精准吧,大家是不是已經躍躍欲試了呢?下面就針對該顏值打分系統進行講解。
二、注冊百度API
該系統最為核心的部分就是顏值的打分,這里其實是直接采用的是百度的人臉檢測平台,大公司,打得分靠譜有保障,大家只需要打開下面的網址:
http://ai.baidu.com/tech/face 然后點擊“立即使用”后,創建自己的應用即可
創建應用后,我們便可以得到自己的APP_ID 、API_KEY和SECRET_KEY 值,如下圖所示:
這三個值相當於我們的門牌號和鑰匙,只有有這些值,我們才能夠“打開門”。
三、用Python調用百度API
我們注冊好了api之后,百度提供了Python接口,我們直接安裝之后就可以非常方法的使用了。省去了我們自己用深度學習搭建模型的麻煩,有API真心好啊。
- 先安裝pip install baidu-aip 這個包非常重要,一定要先安裝,然后引入AipFace這個庫;
- 接着我們需要把圖片讀取出來,因為圖片是二進值的,所以我們用rb讀取,然后把二進制的數據用base64加密,傳給百度后端。
- 然后調用aFace這個接口,把數據喂給它,獲取它的json返回值,我們這里只取了年齡,顏值和性別。
下面看一下核心的代碼:
# 配置百度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
四、用Tk做一個界面
因為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都綁定了對應的函數;
- 打開文件綁定show_original_pic()
- 運行程序綁定open_file2()
- 幫助文件綁定show_help()
- 退出軟件綁定quit()
比如我們的打開文件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