preface
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 host
1.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
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
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
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
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
Click to enter face recognition
Create an app
After creation, enter the management application, open the application, and click download SDK
Do not download, click the instructions
Install SDK
pip install baidu-aip
Write code according to Baidu interface example
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
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