Hi, I’m Latiao.
I will try my best to keep the progress of Wednesday. If you have any good suggestions, please share them in the comments. If this article is helpful to you, please remember to give latiao three lian!
Achieve target effect
Tool use
Development environment: Win10, PYTHon3.7
Development tools: PyCharm, Chrome
Toolkits: Requests, Basse64
Interface use: Baidu AI
Project idea analysis
Use the interface provided by Baidu Brain for avatar conversionConsult the interface documentation for the current APIDetermine the requested url of the URL
HTTP method: POST request URL: https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_animeCopy the code
Url parameters need to be generated in additionClinet_id and Cleint_secret are created on the console for the baidu AI application data createdParameters to be passedAfter data acquisition, image data of JSON data is extracted and converted into image base data through base64 decoding
Easy source sharing
The import requests the import base64 url = 'https://aip.baidubce.com/oauth/2.0/token' data = {' grant_type ': 'client_credentials', 'client_id': 'your AK ', 'client_secret': Get (url, data) access_token = response.json()['access_token'] print("access_token = response.json ") ", Access_token) requests_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime" with open('./img/E6I(82]EI7$V2}3SDN(@UGN.png', 'rb') as file: img = base64.b64encode(file.read()) params = {"image": img} headers = {'content-type': 'application/x-www-form-urlencoded'} requests_url = requests_url + "?access_token=" + access_token response = requests.post(requests_url, data=params, headers=headers) with open("001.jpg",'wb') as file: anime = response.json()['image'] anime_image = base64.b64decode(anime) file.write(anime_image)Copy the code
\