发送图像base64数据请求Face++post接口

 

最近所做人脸相关项目,请求face++接口来对比我们的算法效果,记录一下,请求face++人脸融合接口  上传两张照片,转成base64数据,通过post接口上传,返回融合后的json数据,融合图片放在result关键字对应的value为base64。

# encoding: utf-8
import requests
import base64
import cv2
import matplotlib.pyplot as plt
import cv2
from PIL import Image
from io import BytesIO
import numpy as np
import json
from json import JSONDecoder


# 拼接参数
# files = {'modelimg': (file_name, file1, 'image/jpeg'), 'personimg': (file_name2, file2, 'image/jpeg')}
# img_data1 = cv2.imread("./template/t7.jpeg")
img_data1 = cv2.imread("./template/t16.jpeg")

ret1, buf1 = cv2.imencode(".jpg", img_data1)
img_bin1 = Image.fromarray(np.uint8(buf1)).tobytes()
model_base64_1 = base64.b64encode(img_bin1)

# img_data2 = cv2.imread("./template/t8.jpeg")
img_data2 = cv2.imread("./template/t17.jpeg")

ret2, buf2 = cv2.imencode(".jpg", img_data2)
img_bin2 = Image.fromarray(np.uint8(buf2)).tobytes()
model_base64_2 = base64.b64encode(img_bin2)


import time
time_start = time.time()
# http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
http_url = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"

key = "************"  #自己face++账号的key
secret = "XXXXXXXX"   #自己face++账号的secret
# face++提供的一对密钥

data = {"api_key": key, "api_secret": secret, "template_base64": model_base64_1, "merge_base64": model_base64_2, "merge_rate": 50}

response = requests.post(http_url, data=data)
# response = requests.post(http_url, data=data, files=fileImg)

# POTS上传

# req_con = response.content.decode('utf-8')
# # response的内容是JSON格式
#
# req_dict = JSONDecoder().decode(req_con)
# 对其解码成字典格式

# print("cost time is:  ", str(time.time() - time_start))
# print("\n\n\n req_dict is:  ",str(req_dict))


# 获取服务器返回的图片,字节流返回
result = response.content

# print(result)
# img = base64.b64decode(result)
res = str(result, encoding="utf-8")
# print(res)
res = eval(res)
# data = res.get("data")
#
# print(data)
print("request_id:   " + str(res.get("request_id")))
print("time_used:  " + str(res.get("time_used")))

img = res.get("result")
img = base64.b64decode(img)
img = plt.imread(BytesIO(img), "jpg")
img_data = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite("cv_deploy.jpg", img_data)
cv2.imshow("cv_img", img_data)

cv2.waitKey(10000)


 

 

你可能感兴趣的:(机器学习,python,深度学习)