Python实现头像换脸(AI换脸)

  • AI换脸
  • 源程序代码如下(注释已经尽可能详细):
  • 程序运行结果:
  • 需要的两个照片:对应代码里的1.png和2.png
  • 运行代码之后生成的3.png(合成效果图还不错~~~~~)
  • 相关变量输出结果(变量的输出位置以及相关描述已经写在代码注释中)
  • response1:
  • req_con:
  • rectangle:
  • imgdata:
  • 接口调用来源:


AI换脸

源程序代码如下(注释已经尽可能详细):

"""
   __author__="dazhi"
    2021/3/19-19:42
"""
# 首先是导入库
import json
import requests
import simplejson
import base64

# 第一步:获取人脸关键点
def find_face(impath):
    # 这个链接是‘旷视平台’向我们开放的接口(请求接口之后,让接口帮我们处理一些东西)
    http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
    # 将相应的变量封装到字典内(api_key和api_secret都是平台上的参数)
    data = {"api_key": '(换成自己的哦。采用的face++网站的)',
            "api_secret": '(换成自己的哦。采用的face++网站的)',
            "image_url": impath,
            "return_landmark": 1}
    # 这个open(impath)的作用是将图片的数据读取进来('rb'表示二进制读取)
    files = {'image_file': open(impath, 'rb')}
    # 发送我们的请求(拥有链接、数据、文件)---类似与某个平台上传自己自定义头像
    response1 = requests.post(http_url, data=data, files=files)
    # print("response1 的值:",response1)
    # 查看response里面的内容。防止出现乱码要进行解码
    # 这里返回的不是字典,是json数据
    req_con = response1.content.decode('utf-8')
    # print("req_con 的值:",req_con)
    # 进行simplejson的加载-之后可以按字典提取方式进行操作
    this_json = simplejson.loads(req_con)
    # 按字典方式进行提取(键值对)
    # 当前faces是一个列表
    faces = this_json['faces']
    # 通过索引提取faces中的内容
    list0 = faces[0]
    # 获取面部轮廓的相关数据
    rectangle = list0['face_rectangle']
    # print("rectangle 的值:",rectangle)
    return rectangle

# 第二步:换脸
# 前3个参数是第一个图片、第二个图片、生成的新图片。number是换脸的相似度
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(str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height']))
    # 打开图片文件
    f1 = open(image_url1, 'rb')
    f2 = open(image_url2, 'rb')
    # 进行b64编码
    f1_64 = base64.b64encode(f1.read())
    f2_64 = base64.b64encode(f2.read())
    # 关闭图片文件
    f1.close()
    f2.close()

    # 合并人脸的接口
    url_add = 'https://api-cn.faceplusplus.com/imagepp/v1/mergeface'
    # data的相关性质看上方代码
    # template_base64模板文件的64编码文件,template_rectangle(字符串格式)模板的面部轮廓内容(“145,123,1212,123”)
    # merge_base64合并的64编码文件 merge_rectangle(字符串格式)合并的面部轮廓内容
    # number是换脸的相似度
    data = {"api_key": '(换成自己的哦。采用的face++网站的)',
            "api_secret": '(换成自己的哦。采用的face++网站的)',
            "template_base64": f1_64, "template_rectangle": rectangle1,
            "merge_base64": f2_64, "merge_rectangle": rectangle2,
            "merge_rate": number}
    # 进行一个request提交
    response2 = requests.post(url_add, data)
    # 将返回的数据进行一个解码操作
    req_con2 = response2.content.decode('utf-8')
    # json处理--进行解码操作
    req_dict = json.JSONDecoder().decode(req_con2)
    # 按照字典提取的特点,提取其中的result数据
    result = req_dict['result']
    # 将结果在进行b64解码(将二进制转换为图像)
    imgdata = base64.b64decode(result)
    # 将生成的效果图,打开图片地址写入图片数据
    # 先打开
    file = open(image_url, 'wb')
    # 写入
    file.write(imgdata)
    # 关闭
    file.close()

if __name__ == '__main__':
    # 加‘r’的作用是因为链接中的‘\’具有转义的意思。加上之后就不按转义进行处理(使链接保持原意)
    image1 = r"C:\Users\26301\Desktop\1.png"
    image2 = r"C:\Users\26301\Desktop\2.png"
    image3 = r"C:\Users\26301\Desktop\3.png"
    merge_face(image1, image2, image3, 100)
    print("恭喜您合成成功!!!!!")

程序运行结果:

python avahi python avahi airprint_python

需要的两个照片:对应代码里的1.png和2.png

python avahi python avahi airprint_python_02


python avahi python avahi airprint_python_03

运行代码之后生成的3.png(合成效果图还不错~~~~~)

python avahi python avahi airprint_python avahi_04

相关变量输出结果(变量的输出位置以及相关描述已经写在代码注释中)

response1:

python avahi python avahi airprint_json_05

req_con:

python avahi python avahi airprint_json_06

rectangle:

python avahi python avahi airprint_json_07

imgdata:

python avahi python avahi airprint_数据_08

接口调用来源:

旷视网站~~~~~~。