With the rise of live broadcasting, the career of anchor has gradually come into people's vision. Now each platform has the title of "master Huadan", "first brother", "first sister", etc. In fact, popularity is one aspect, but beauty is the hard power.
Next, I'll take you to the beauty test score of the anchor to see who is the most beautiful baby
General content of this article:
1. Crawling the live face image of the host
2. Call Baidu face detection open interface to score the face value
Environment introduction:
python 3.6
pycharm
requests
parsel(xapth)
1. Crawling the picture of the host1.1 import module
import requests import parsel
1.2 analyze the target web page to determine the url path to be crawled and the headers parameter
![](/images/blog/875f2f63fc9021f3aa727ce0a8a6ec8c.jpg)
![](/images/blog/30a1da7b1b4a240ca8a1f3049b7bd01a.jpg)
The code is as follows:
base_url = 'https://www.huya.com/g/4079' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
}
1.3 send request -- requests simulate browser to send request and get response data
![](/images/blog/45ebfe24d1809a7c3c78fec5fc9cdebd.jpg)
The code is as follows:
response = requests.get(url=base_url, headers=headers) html_data = response.text
1.4 parse data -- parsel is converted to Selector object, which has the method of xpath and can process the converted data
![](/images/blog/5ecff795d9963d30bd68aab176b7d33c.jpg)
The code is as follows:
parse = parsel.Selector(html_data) data_list = parse.xpath('//li[@class="game-live-item"]') # print(data_list) for data in data_list: img_url = data.xpath('./a/img/@data-original').get() # Host face picture url address img_title = data.xpath('.//span/i/@title').get() # Name of the anchor print(img_url, img_title) # Request picture data img_data = requests.get(url=img_url, headers=headers).content
1.5 save data
![](/images/blog/90106581bbbcf7ba053f40ca29795c24.jpg)
The code is as follows:
# Prepare file name file_name = img_title + '.jpg' with open('img\\' + file_name, mode='wb') as f: print('Saving:', file_name) f.write(img_data)
2. Call Baidu face detection open interface
Register account in Baidu AI open platform
![](/images/blog/ce9c43e9486ce14202484100529ac9a4.jpg)
Click to enter face recognition
![](/images/blog/311ae7044f461fcf7f1836953294fca9.jpg)
Create an app
![](/images/blog/89a9c6bb11bf6203cbd06d8a72770afe.jpg)
After creation, enter the management application, open the application, and click download SDK
![](/images/blog/968dd91f4720952cbbc32cca3cb8b4f2.jpg)
Do not download, click the instructions
![](/images/blog/6ef5bae075ad2ab7aca3916235d28831.jpg)
![](/images/blog/d2dca8d83ed227102458265741856a16.jpg)
Install SDK
![](/images/blog/51bb0b4abfe8a1a1ef3b298f2e2d1dbb.jpg)
pip install baidu-aip
Write code according to Baidu interface example
![](/images/blog/c33c7e995bfc88046134bf47d81be61f.jpg)
![](/images/blog/59aa3da095f879ac50c3b34070e04ade.jpg)
The code is as follows:
from aip import AipFace import base64 def face_rg(file_Path): """ Your api_id AK SK """ api_id = '20107883' api_key = 'Xela0yPoFtERUBSTFtNlEKbO' secret_key = 'E5TmneyfAzxfzowgwRErLT8RYe7MfkfG' client = AipFace(api_id, api_key, secret_key) # Call the interface of color detection with open(file_Path, 'rb') as file: data = base64.b64encode(file.read()) # Picture type BASE64:Picturesque base64 Value, base64 Encoded picture data image = data.decode() imageType = "BASE64" options = {} options["face_field"] = 'beauty' """ Call face detection """ result = client.detect(image, imageType, options) print(result) return result['result']['face_list'][0]['beauty'] if __name__ == '__main__': face_rg(r'..\Host color detection\img\Blue cloud-Summer flowers still.jpg')
3. Test score
3.1 import module, interface for cycle detection
The code is as follows:
import os from Host color detection.Beauty detection_Interface import face_rg path = './img' image_list = os.listdir(path) # print(image_list) score_dict = {} for image in image_list: try: name = image.split('.')[0] # print(name) image_path = path + '\\' + image # Path to picture face_score = face_rg(image_path) # print(face_score) score_dict[name] = face_score # print(score_dict) except Exception as e: print('Detecting:{}|Detection failed!!!'.format(str(name))) else: print('Detecting:{}|Beauty score:{}'.format(str(name), str(face_score))) print('\n===========================================Test complete===========================================') print(score_dict.items()) # Dictionary in descending order of values change_score = sorted(score_dict.items(), key=lambda x: x[1], reverse=True) # lambda 1 in is the index of the tuple print(change_score)
3.2 data output
for a, b in enumerate(change_score): print('The little sister's name is:{}Beauty value rank: No{}Her beauty score is:{}'.format(change_score[a][0], a+1, change_score[a][1]))
After running the code, get the final test result of beauty rating
![](/images/blog/c5af66464f598f5c746d92c080830bf3.jpg)
Add Penguin Group 695185429 and you can get it for free. All the information is in the group file. Materials can be obtained, including but not limited to Python practice, PDF electronic documents, interview brochures, learning materials, etc