AI换脸技术源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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)

  

posted @   经管时评  阅读(189)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
历史上的今天:
2015-07-07 七种公司永远做不大,十种老板永远不成功
点击右上角即可分享
微信分享提示