Python implements AI face change function

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

Python implements AI face change function
How Python implements FTP function
Python implements image stitching function
How Python implements the mail function
Python implements ftp file transfer function
How Python implements the timer function
Python implements AI automatic version greedy snake
Python enumerate() function
Python function buffer
Python implements tic-tac-toe game
Python custom function basics
Python implements tic-tac-toe game
Join function in Python
Python built-in function -compile()
Python implements man-machine gobang
Python function basic learning
Python implements Tetris game
Python data analysis-apply function
Python implements image stitching
Python implements minesweeper game
Python3 built-in function table.md
Python implements scanning tools
Python Print print timer function
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python defines a function method
Python implements simple tank battle
Python high-order function usage summary!
Python implements udp chat window
Python implements WeChat airplane game
Python realizes online translation function
Python implements word guessing game
Python implements parking management system
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements car management system
Python realizes face sign-in system
Python tornado upload file function
Python implements code block folding
Python implements panoramic image stitching
Python magic function eval () learning
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python high-order function usage summary!
Python implements the brick-and-mortar game