Skip to content

Commit b5f13c2

Browse files
authored
create api.py
create api.py
1 parent bf21c35 commit b5f13c2

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

2020/api/api.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import requests
2+
import base64
3+
import json
4+
import cv2
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
%matplotlib inline
8+
9+
10+
beautify_url = "https://api-cn.faceplusplus.com/facepp/v2/beautify"
11+
# 你创建的应用的 API Key 和 API Secret(也叫 Secret Key)
12+
AK = ''
13+
SK = ''
14+
15+
# 可选参数,不填写,默认50
16+
# 美白程度 0 - 100
17+
whitening = 80
18+
# 磨皮程度 0 - 100
19+
smoothing = 80
20+
# 瘦脸程度 0 - 100
21+
thinface = 20
22+
# 小脸程度 0 - 100
23+
shrink_face = 50
24+
# 大眼程度 0 - 100
25+
enlarge_eye = 50
26+
# 去眉毛程度 0 - 100
27+
remove_eyebrow = 50
28+
# 滤镜名称,不填写,默认无滤镜
29+
filter_type = ''
30+
31+
# 二进制方式打开图片
32+
img_name = 'test_1.png'
33+
f = open(img_name, 'rb')
34+
# 转 base64
35+
img_base64 = base64.b64encode(f.read())
36+
37+
# 使用 whitening、smoothing、thinface 三个可选参数,其他用默认值
38+
data = {
39+
'api_key': AK,
40+
'api_secret': SK,
41+
'image_base64': img_base64,
42+
'whitening': whitening,
43+
'smoothing': smoothing,
44+
'thinface': thinface,
45+
}
46+
47+
r = requests.post(url=beautify_url, data=data)
48+
html = json.loads(r.text)
49+
50+
# 解析base64图片
51+
base64_data = html['result']
52+
imgData = base64.b64decode(base64_data)
53+
nparr = np.frombuffer(imgData, np.uint8)
54+
img_res = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
55+
img_res_BGR = cv2.cvtColor(img_res, cv2.COLOR_RGB2BGR)
56+
57+
# 原始图片
58+
img = cv2.imread(img_name)
59+
img_BGR = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
60+
61+
# 显示图片
62+
fig, axs = plt.subplots(nrows=1, ncols=2, sharex=False, sharey=False, figsize=(10,10))
63+
axs[0].imshow(img_BGR)
64+
axs[1].imshow(img_res_BGR)
65+
plt.show()

0 commit comments

Comments
 (0)