preparation
Before writing code, you need to apply for permission on Baidu developer platform. The steps are as follows:
Login Baidu Intelligent Cloud
- https://cloud.baidu.com/?from=console , if you don't have a Baidu account, you can register one
- The first time you enter, there will be such a page, which you can fill in at will
Enter the console through the upper right corner of the interface
After entering the console, click the menu bar in the upper left corner
Select product service
Click Create app
- Fill in the application name casually
- Interface selection default
- Select individual by application attribution
- Application description
- Then click create now
After creation, click to return to the application list
Click to get free resources
Receive all contents of the service type after real name authentication
Real name authentication takes some time
Return to the application list after receiving
Copy the contents of API Key and Secret Key for later interface authentication
For students who have questions about this article, you can add [data white whoring, answer exchange group: 910981974]
First, ask for some photos of beautiful women and come back for beauty detection
development environment
- Python 3.8
- Pycharm 2021.2
- Can use API interface Baidu cloud interface
Module use
- requests >>> pip install requests
- tqdm >>> pip install tqdm
- os
- base64
The first stage is to collect anchor photo data
Request data
url = f'https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2168&tagAll=0&page=1' # headers Request header camouflage Python The code is not recognized as a crawler... # headers Is a dictionary data type headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36' } # adopt requests Module to right url Address send request response = requests.get(url=url, headers=headers)
Analyze the data and extract the data content we want, the anchor name, the anchor cover map, and the url address
# json Data extraction content extracts the content on the right of the colon according to the content on the left of the colon data_list = response.json()['data']['datas'] for index in data_list: # pprint.pprint(index) name = index['nick'] img_url = index['screenshot']
Turn pages
for page in range(1, 11): url = f'https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2168&tagAll=0&page={page}'
Save picture data content
img_content = requests.get(url=img_url, headers=headers).content # 'img\\' File path name File name '.jpg' file extension >>> file name # mode Save mode wb Binary mode write # as Rename to f filename = 'img_1\\' if not os.path.exists(filename): os.mkdir(filename) with open(filename + name + '.jpg', mode='wb') as f: f.write(img_content) # Write data print('Saving: ', name)
Color value detection
Call the interface for identification
def get_beauty(img_base64): host = 'https://aip.baidubce.com/oauth/2.0/token' data = { 'grant_type': 'client_credentials', 'client_id': 'vXONiwhiVGlBaI2nRRIYLgz5', 'client_secret': 'ouZMTMuCGLi7pbeg734ftNxn9h3qN7R4' } response = requests.get(url=host, params=data) token = response.json()['access_token'] # print(token) ''' Face detection and attribute analysis ''' request_url = f"https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token={token}" params = { "image": img_base64, # Need to pass pictures base64 "image_type": "BASE64", "face_field": "beauty" } headers = {'content-type': 'application/json'} response = requests.post(request_url, data=params, headers=headers) try: beauty = response.json()['result']['face_list'][0]['beauty'] return beauty except: return 'Recognition failed'
Get all pictures and rank them
lis = [] files = os.listdir('img_1\\') print('Recognizing faces, Color value detection in progress, Please wait.....') for file in tqdm(files): img_file = 'img_1\\' + file img_name = file.split('.')[0] # print(img_file) f = open(img_file, mode='rb') # Read a picture img_base64 = base64.b64encode(f.read()) beauty = get_beauty(img_base64) if beauty != 'Recognition failed': dit = { 'anchor': img_name, 'level of appearance': beauty, } lis.append(dit) # Add the dictionary to the empty list # print(f'{img_name}The appearance value score is{beauty}') lis.sort(key=lambda x:x['level of appearance'], reverse=True) num = 1 # Appearance ranking of the top 10 photos for index in lis: print(f'Appearance ranking No{num}Yes{index["anchor"]}, The appearance value score is{index["level of appearance"]}') num += 1
Look at the ranking
Top three
emmm. . . . . .
But I think look at the last three
I don't agree. The last one lost to a bear and a man, and only got 22 points?
After reading the official documents, the last one may be because his hand covered his face, but he was defeated by a bear