美文网首页散文想法简友广场
python调用百度AI接口实现人脸融合

python调用百度AI接口实现人脸融合

作者: Cache_wood | 来源:发表于2021-02-05 00:24 被阅读0次

人脸融合不同于AI换脸,因为人脸融合是将两个人的面部特征都保留了下来,而AI换脸是把原来人的面部特征全部由另一个人替换。
@[toc]

官网地址

[人脸融合

请求access——token

 # encoding:utf-8
import requests 

# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
response = requests.get(host)
if response:
    print(response.json())

处理图片

with open('banben.jpg','rb') as f:
    img = base64.b64encode(f.read())
    img1 = img.decode()
with open('zixia.jpg','rb') as f:
    img = base64.b64encode(f.read())
    img2 = img.decode()

调用人脸融合url

'''
人脸融合
'''

request_url = "https://aip.baidubce.com/rest/2.0/face/v1/merge"

params = "{\"image_template\":{\"image\":\"sfasq35sadvsvqwr5q...\",\"image_type\":\"BASE64\",\"quality_control\":\"NONE\"},\"image_target\":{\"image\":\"sfasq35sadvsvqwr5q...\",\"image_type\":\"BASE64\",\"quality_control\":\"NONE\"}}"
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())

核心概念

模板图(template_image):要求被融合的人脸边缘需要与图片边缘保持一定距离,保证被融合的人脸的的核心区域完全在图片中。

目标图 (target_image):目标图无严格限制, 建议选择 正脸 清晰 图像,如下面的手机自拍照。

完整代码

# -*- coding: utf-8 -*-
"""
Created on Mon Feb  1 23:41:59 2021

@author: lenovo
"""
# encoding:utf-8
import requests 
import base64
import json
from PIL import Image
import matplotlib.pyplot as plt

# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token'
data={
        'grant_type':'client_credentials',  # 固定值
        'client_id':'OEGw4UihfBsjQuef5b9068cX', # 在开放平台注册后所建应用的API Key
        'client_secret':'iBoMMtxZDMgQzsK8zpKr2rVVp1qd7koG'  # 所建应用的Secret Key
    }
response = requests.post(host,data=data)
if response:
    res=response.json()
    access_token=res['access_token']  
    
    #print(access_token)

'''
人脸融合
'''
with open('banben.jpg','rb') as f:
    img = base64.b64encode(f.read())
    img1 = img.decode()
with open('zixia.jpg','rb') as f:
    img = base64.b64encode(f.read())
    img2 = img.decode()
#print(img1)
request_url = "https://aip.baidubce.com/rest/2.0/face/v1/merge"

params = {
        "image_template": {
            "image": img1,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
        "image_target": {
            "image": img2,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
        "merge_degree": "HIGH"
    }
params = json.dumps(params)
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    result = response.json()['result']['merge_image']

imagedata = base64.b64decode(result)
file = open('./result.jpg', "wb")
file.write(imagedata)

image = Image.open('result.jpg')
plt.imshow(image)
plt.axis('off')
plt.show()

效果展示


模板图(template_image)


目标图(target——image)



融合图

相关文章

网友评论

    本文标题:python调用百度AI接口实现人脸融合

    本文链接:https://www.haomeiwen.com/subject/mpnytltx.html