Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c26c1a

Browse files
换脸术代码
1 parent 0c3c8a2 commit 9c26c1a

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

‎FusionFace/fusionFace.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# encoding:utf-8
2+
import requests
3+
import base64
4+
import cv2
5+
6+
ak = 'ak'
7+
sk = 'sk'
8+
9+
def getPhoto():
10+
'''
11+
调用摄像头拍摄照片
12+
:return: 照片路径
13+
'''
14+
print("准备拍摄照片,请保持颜值在线...")
15+
photoSrc = '自拍路径'
16+
cap = cv2.VideoCapture(0)
17+
while (1):
18+
ret, frame = cap.read()
19+
# 显示图像
20+
cv2.imshow("photo", frame)
21+
# 按 q 键退出
22+
if cv2.waitKey(1) & 0xFF == ord('q'):
23+
cv2.imwrite(photoSrc, frame)
24+
print("照片已经拍摄完成!")
25+
break
26+
cap.release()
27+
cv2.destroyAllWindows()
28+
return photoSrc
29+
30+
def getAccessToken():
31+
'''
32+
获取百度 AI 开放平台的 access_token
33+
:return: access_token
34+
'''
35+
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + ak + '&client_secret=' + sk
36+
response = requests.get(host)
37+
if response:
38+
print(response.json())
39+
return response.json()['access_token']
40+
41+
42+
def faceFusion(templateBase64, targetBase64, access_token):
43+
'''
44+
换脸术
45+
:param templateBase64: 模板图片
46+
:param targetBase64: 目标图片
47+
:param access_token: access_token
48+
:return: 换脸后的 base64
49+
'''
50+
request_url = "https://aip.baidubce.com/rest/2.0/face/v1/merge"
51+
52+
params = "{\"image_template\":{\"image\":\"" + templateBase64 + "\",\"image_type\":\"BASE64\",\"quality_control\":\"NONE\"},\"image_target\":{\"image\":\"" + targetBase64 + "\",\"image_type\":\"BASE64\",\"quality_control\":\"NONE\"}}"
53+
54+
request_url = request_url + "?access_token=" + access_token
55+
headers = {'content-type': 'application/json'}
56+
response = requests.post(request_url, data=params, headers=headers)
57+
if response:
58+
print (response.json())
59+
return response.json()['result']['merge_image']
60+
61+
62+
def image2base64(imagePath):
63+
'''
64+
图片转base64
65+
:param image_path: 图片地址
66+
:return: base64
67+
'''
68+
with open(imagePath, 'rb') as f:
69+
base64_data = base64.b64encode(f.read())
70+
s = base64_data.decode()
71+
return s
72+
73+
def base642image(base64str):
74+
'''
75+
base64转图片
76+
:param base64str: base64
77+
'''
78+
imgdata = base64.b64decode(base64str)
79+
with open('换脸后路径', 'wb') as f:
80+
f.write(imgdata)
81+
print('successful')
82+
83+
def main():
84+
photoSrc = getPhoto()
85+
target = image2base64(photoSrc)
86+
template = image2base64('模板图片路径')
87+
print(target)
88+
access_token = getAccessToken()
89+
image_base64 = faceFusion(template, target, access_token)
90+
base642image(image_base64)
91+
92+
if __name__ == '__main__':
93+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /