Need to use the interface:
Interface for obtaining face information: https://api-cn.faceplusplus.com/facepp/v3/detect
Realize the interface for face change: https://api-cn.faceplusplus.com/imagepp/v1/mergeface
The code is divided into three steps
Code:
import requests
import json
import simplejson
import base64
# Step 1: Get the key points of the face
def find_face(imgpath):"""
: param imgpath:The address of the picture
: return:The key points of a dictionary type face are as follows:{'top':156,'left':108,'width':184,'height':184}"""
http_url ='https://api-cn.faceplusplus.com/facepp/v3/detect' #Interface for obtaining face information
data ={"api_key":"x2NyKaa6vYuArYwat4x0-NpIbM9CrwGU",#Parameters required to access URL
" api_secret":"OuHx-Xaey1QrORwdG7QetGG5JhOIC8g7",#Parameters required to access URL
" image_url":imgpath, #The map's address
" return_landmark":1}
files ={'image_file':open(imgpath,'rb')} #Define the address of a dictionary to store pictures
response = requests.post(http_url,data=data,files=files)
res_con1 = response.content.decode('utf-8')
res_json = simplejson.loads(res_con1)
faces = res_json['faces']
list = faces[0]
rectangle = list['face_rectangle']return rectangle
# Step 2: Realize face change
def merge_face(image_url1,image_url2,image_url,number):"""
: param image_url1:The image path of the changed face
: param image_url2:Picture path for face change
: param image_url:The path where the picture is saved after changing the face
: param number:Similarity of face change
"""
# First get the key points of the faces of the two pictures
face1 =find_face(image_url1)
face2 =find_face(image_url2)
# Convert face to string format
rectangle1 =str(str(face1['top'])+","+str(face1['left'])+","+str(face1['width'])+","+str(face1['height']))
rectangle2 =str(str(face2['top'])+","+str(face2['left'])+","+str(face2['width'])+","+str(face2['height']))
# Read two pictures
f1 =open(image_url1,'rb')
f1_64 = base64.b64encode(f1.read())
f1.close()
f2 =open(image_url2,'rb')
f2_64 = base64.b64encode(f2.read())
f2.close()
url_add ='https://api-cn.faceplusplus.com/imagepp/v1/mergeface' #Implement face-changing interface
data={"api_key":"x2NyKaa6vYuArYwat4x0-NpIbM9CrwGU","api_secret":"OuHx-Xaey1QrORwdG7QetGG5JhOIC8g7","template_base64":f1_64,"template_rectangle":rectangle1,"merge_base64":f2_64,"merge_rectangle":rectangle2,"merge_rate":number
}
response1 = requests.post(url_add,data=data)
res_con1 = response1.content.decode('utf-8')
res_dict = json.JSONDecoder().decode(res_con1)
result = res_dict['result']
imgdata = base64.b64decode(result)
file=open(image_url,'wb')
file.write(imgdata)
file.close()if __name__ =='__main__':
image1 = r"meizi1.jpg"
image2 = r"meizi.jpg"
image3 = r"face1.jpg"merge_face(image1,image2,image3,100)
effect:
Before changing face
Face to change:
After changing the face:
to sum up
So far, this article about the code for implementing AI face change in Python is introduced. For more relevant Python implementation of AI face change content, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts