目錄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