In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Python how to call Baidu AI to achieve face scoring function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python how to call Baidu AI to achieve face scoring function"!
First, call Baidu interface for face attribute recognition
After installing the baidu-aip module and obtaining the Baidu AI interface key, you can call the Baidu interface for face attribute recognition. First of all, take Yang Zi's picture as an example to identify age, sex and face.
The specific python code is as follows:
Import osimport base64from aip import AipFaceos.chdir (ringing F:\ official account 28. Face recognition') # set the path for storing images pictureName = '1_yz.jpg'def get_picture_content (pictureName): with open (pictureName) 'rb') as fp: content = base64.b64encode (fp.read ()) return content.decode (' utf-8') # defines the function APP_ID = 'XXX'API_KEY =' XXXXXXXX'SECRET_KEY = 'XXXXXXXXXXXX'# Baidu account and key options = {} imageType =' BASE64'options ["face_field"] = "age, gender, beauty" aipFace = AipFace (APP_ID, API_KEY) SECRET_KEY) result = aipFace.detect (get_picture_content (pictureName), imageType, options) print (result)
Note: the content in os.chdir should be replaced with the address where you store the picture, and APP_ID, API_KEY and SECRET_KEY should be replaced with the Baidu key you obtained.
The results are as follows:
Among them, the corresponding value of age is age, the corresponding value of gender is gender, and the corresponding value of beauty is face score. To further standardize the output to a standard format, it is normalized by the following code:
Import pandas as pdface_character = pd.DataFrame ({"age": [result ['result'] [' face_list'] [0] ['age']], "gender": [result [' result'] ['face_list'] [0] [' gender'] ['type']], "beauty": [result [' result'] ['face_list'] [0] [' beauty']]})
The results are as follows:
It can be found that Yang Zi's face score is still very high, and follow-up articles will also test some other stars.
Second, evaluate the appearance according to age and sex.
According to the score of gender and appearance, the recognition results are evaluated as follows:
If face_character ['beauty'] [0] > 80: if face_character [' gender'] [0] = 'female': print ("Little Sister") else: print ("Little Brother") elif face_character [' beauty'] [0] > 70: if face_character ['gender'] [0] =' female': print ("Little Sister is born Beautiful") Else: print ("little brother is born beautiful") elif face_character ['beauty'] [0] > 60: if face_character [' gender'] [0] = 'female': print ("pretty little sister") else: print ("pretty little brother") elif face_character [' beauty'] [0] > 40: if face_character ['gender'] [0] =' female': Print ("the little sister is not born enough. Else: print ("little brother is not born enough, come the day after tomorrow") else: if face_character ['gender'] [0] =' female': print ("little sister wash and sleep") else: print ("little brother wash up and sleep")
The results are obtained:
Third, recognize other people's face attributes in batches
If there are several face images in the folder, and you want to score the appearance of these images in batches, you can execute the following code:
Import osimport reimport timeimport base64from aip import AipFaceos.chdir (ringing F:\ official account 28. Face recognition') # set the path for storing images pictureName = '6_brzp.jpg'def get_picture_content (pictureName): with open (pictureName) 'rb') as fp: content = base64.b64encode (fp.read ()) return content.decode () # defines the function APP_ID =' XXX'API_KEY = 'XXXXXXXX'SECRET_KEY =' XXXXXXXXXXXX'# Baidu account and key options = {} imageType = 'BASE64'options ["face_field"] = "beauty" options ["max_face_num"] = 2aipFace = AipFace (APP_ID, API_KEY) SECRET_KEY) root_path = ringing F:\ official account\ 28. Face recognition 'list = os.listdir (root_path) # lists all directories and files under the folder for i in range (0, len (list)): time.sleep (1) path = os.path.join (root_path, list [I]) if os.path.isfile (path): result = aipFace.detect (get_picture_content (path), imageType Options) print (list [I] + 'score:' + str (result ['result'] [' face_list'] [0] ['beauty'])) # shows the face score of all pictures
The results are obtained:
It can be found that Yang Zi's photo has a high appearance score of more than 80 points. For a better user experience, we set up a window to display evaluation photos and voice broadcast face scores.
4. Face score of voice broadcast in custom window
In order to evaluate people's appearance more clearly, it will be better to wrap the pictures and results in tkinter libraries and voice playback libraries. The specific code is as follows:
Import osimport reimport timeimport base64import pandas as pdimport tkinter as tkfrom aip import AipFacefrom tkinter import filedialogroot = tk.Tk () root.withdraw () Folderpath = filedialog.askdirectory (title = 'Please select the folder where the pictures are stored') picturePath = filedialog.askopenfilename (title = 'Please select the pictures to be evaluated') root.destroy () os.chdir (Folderpath) # set the path where the pictures are stored def get_picture_content (picturePath): with open (picturePath) 'rb') as fp: content = base64.b64encode (fp.read ()) return content.decode () # defines the function APP_ID =' XXX'API_KEY = 'XXXXXXXX'SECRET_KEY =' XXXXXXXXXXXX'# Baidu account and key options = {} options ["max_face_num"] = 2options ["face_field"] = "gender" aipFace = AipFace (APP_ID, API_KEY) SECRET_KEY) imageType = 'BASE64'result = aipFace.detect (get_picture_content (picturePath), imageType, options) gender = result [' result'] ['face_list'] [0] [' gender'] ['type'] options ["face_field"] = "age" time.sleep (1) result = aipFace.detect (get_picture_content (picturePath), imageType Options) age = result ['result'] [' face_list'] [0] ['age'] options ["face_field"] = "beauty" time.sleep (2) result = aipFace.detect (get_picture_content (picturePath), imageType, options) beauty = result [' result'] ['face_list'] [0] [' beauty'] # get age, sex and appearance information face_character = pd.DataFrame ({"age": age, "gender": gender, "beauty": beauty} Index = ['value']) if face_character [' beauty'] [0] > 80: if face_character ['gender'] [0] = =' female': str_list = "the younger sister's age:" + str (age) + " In the end, the appearance is worth being divided into:'+ str (beauty) else: str_list = "little brother Guigeng:" + str (age) + ", and finally the appearance is worth being divided into:'+ str (beauty) elif face_character ['beauty'] [0] > 70: if face_character [' gender'] [0] = = 'female': str_list =" younger sister: + str (age) + " Natural beauty "+', the final beauty should be divided into:'+ str (beauty) else: str_list =" little brother Guigeng: "+ str (age) +", natural beauty "+', and the final beauty should be divided into:'+ str (beauty) elif face_character ['beauty'] [0] > 50: if face_character [' gender'] [0] = 'female': str_list =" younger sister: "+ str (age) +" The appearance can be divided into'+ str (beauty) else: str_list = "little brother Guigeng:" + str (age) + ", and the appearance can be" +'. Finally, the appearance can be divided into:'+ str (beauty) elif face_character ['beauty'] [0] > 30: if face_character [' gender'] [0] = = 'female': str_list = "younger sister:" + str (age) + ". To gather together the "+" the day after tomorrow, the final beauty should be divided into:'+ str (beauty) else: str_list = "little brother Guigeng:" + str (age) + ", the nature is not enough, the acquired" + ", and finally the beauty deserves to be divided into:'+ str (beauty) else: if face_character ['gender'] [0] =' female': str_list =" Sister Fang Ying: "+ str (age) +", wash up and go to bed early "+' In the end, Yan should be divided into:'+ str (beauty) else: str_list = "little brother Guigeng:" + str (age) + ", wash up and go to bed early" +' In the end, the appearance should be divided into:'+ str (beauty) # face definition from tkinter import * from PIL import Image ImageTkfrom win32com.client import Dispatch# Import package speaker = Dispatch ("SAPI.SpVoice") def roll_call (): speaker.Speak (str_list) # Control the playback of voice os.chdir (Folderpath) # set file path root = Tk () root.title ("appearance Test Mini Program") root.iconbitmap ("pikaqiu2.ico") # Settings window image = Image.open (picturePath) # load picture root.geometry ("400x300") # set according to image size Set window size img_pic = ImageTk.PhotoImage (image) label = Label (root Image=img_pic) label.pack () b2 = tk.Button (root, bg='lightyellow', text=' face evaluation', font= ("KaiTi", 8), width=8, height=2, command=roll_call) b2.place (x, y) root.mainloop () so far I believe that the "Python how to call Baidu AI to achieve face scoring function" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.