ai照片放大python源码_AI换脸技术源码【黑马python培训】

该博客介绍了一种使用Python实现的AI照片融合方法,通过Face++ API进行人脸识别和脸部融合。首先,代码展示了如何找到图片中的人脸位置,然后将两幅图片的脸部区域融合在一起,生成新的图片。整个过程涉及到了requests库来发送HTTP请求,以及base64编码处理图片数据。
摘要由CSDN通过智能技术生成

395ada2f3756ca9ef06eee1f33ed5800.png

import requests

import simplejson

import json

import base64

def find_face(imgpath):

print("finding")

http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'

data = {"api_key": "https://console.faceplusplus.com.cn申请'',

"api_secret": 'https://console.faceplusplus.com.cn申请', "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)

faces = this_json2['faces']

list0 = faces[0]

rectangle = list0['face_rectangle']

# print(rectangle)

return rectangle

# number表示换脸的相似度

def merge_face(image_url_1, image_url_2, image_url, number):

ff1 = find_face(image_url_1)

ff2 = find_face(image_url_2)

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'])

url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"

f1 = open(image_url_1, 'rb')

f1_64 = base64.b64encode(f1.read())

f1.close()

f2 = open(image_url_2, 'rb')

f2_64 = base64.b64encode(f2.read())

f2.close()

data = {"api_key": 'https://console.faceplusplus.com.cn申请L', "api_secret": 'https://console.faceplusplus.com.cn申请',

"template_base64": f1_64, "template_rectangle": rectangle1,

"merge_base64": f2_64, "merge_rectangle": rectangle2, "merge_rate": number}

response = requests.post(url_add, data=data)

req_con = response.content.decode('utf-8')

req_dict = json.JSONDecoder().decode(req_con)

result = req_dict['result']

imgdata = base64.b64decode(result)

file = open(image_url, 'wb')

file.write(imgdata)

file.close()

#E:\test\test

image1 = r"E:\test\test\cover1.jpg"

image2 = r"E:\test\test\cover11.jpg"

image = r"E:\test\test\face7.jpg"

merge_face(image2, image1, image, 100)

---------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值