How to realize a face-changing function in Python
This article introduces how to achieve a face-changing function in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Function realization
To realize the face-changing function, we can be roughly divided into two types: one is that all the functions are realized by our own coding, and the other is realized with the help of a third-party API. The first way may require us to carry out a lot of coding, while the second way we only need a small amount of coding to achieve.
In this article, we use a simpler second way to implement this. The API interface we use is provided by Face++,. First of all, we need to register an account on the website with the registered address: https://console.faceplusplus.com.cn/register. After opening it, it is as follows:
We can register by mobile number and email. After registering the account, we log in to the login address https://console.faceplusplus.com.cn/login. After logging in, we will find that the website has created an application for us, as shown in the following figure:
What we need to use are the values of API Key and API Secret in the figure above. Let's take a look at the specific implementation code:
# obtain face key points
Def find_face (imgpath):
Print ("looking for.")
Http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
Data = {"api_key": "your own api_key"
"api_secret": "your own api_secret"
"image_url": imgpath, "return_landmark": 1}
Files = {"image_file": open (imgpath, "rb")}
Response = requests.post (http_url, data=data, files=files)
Req_con = response.content.decode ('utf-8')
Req_dict = json.JSONDecoder () .decode (req_con)
This_json = simplejson.dumps (req_dict)
This_json2 = simplejson.loads (this_json)
Print (this_json2)
Faces = this_json2 ['faces']
List0 = faces [0]
Rectangle = list0 ['face_rectangle']
# print (rectangle)
Return rectangle
# face change. The size of the picture should not exceed 2m _ number to indicate the similarity of the face change.
Def merge_face (image_url1, image_url2, image_url, number):
Ff1 = find_face (image_url1)
Ff2 = find_face (image_url2)
Rectangle1 = str (str (ff1 ['top']) + "," + str (ff1 [' left']) + "," + str (ff1 ['width']) + "," + str (ff1 [' height']))
Rectangle2 = str (ff2 ['top']) + "," + str (ff2 [' left']) + "," + str (ff2 ['width']) + "," + str (ff2 [' height'])
Print (rectangle2)
Url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
F1 = open (image_url1, 'rb')
F1x64 = base64.b64encode (f1.read ())
F1.close ()
F2 = open (image_url2, 'rb')
F2room64 = base64.b64encode (f2.read ())
F2.close ()
Data = {"api_key": "your own api_key"
"api_secret": "your own api_secret"
"template_base64": F1: 64, "template_rectangle": rectangle1
"merge_base64": f2q64, "merge_rectangle": rectangle2, "merge_rate": number}
Response = requests.post (url_add, data=data)
Req_con1 = response.content.decode ('utf-8')
Req_dict = json.JSONDecoder () .decode (req_con1)
Result = req_dict ['result']
Imgdata = base64.b64decode (result)
File = open (image_url, 'wb')
File.write (imgdata)
File.close ()
On how to achieve a face-changing function in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.