请求URL:https://api.shiliuai.com/api/face_swap/v1
请求方式:POST
请求参数-请求头(Header):
参数 | 类型 | 说明 |
---|---|---|
Content-Type | string | application/json |
APIKEY | string | 您的API KEY |
请求参数-请求内容(Body):
参数 | 类型 | 说明 |
---|---|---|
source_base64 | string, optional | base64编码的源图片,source_base64和source_id必须提供一个 |
source_id | string, optional | 源图片的id |
target_base64 | string, required | base64编码的目标图片 |
参数 | 说明 |
---|---|
code | 错误码 |
msg | 错误信息(英文) |
msg_cn | 错误信息(中文) |
result_base64 | 高清图片的base64编码,(当code==0时会有该返回值) |
source_id | 源图片id,(当code==0时会有该返回值) |
错误码说明:
错误码 | 说明 |
---|---|
0 | 成功 |
1 | 参数错误 |
2 | 未知错误 |
3 | 原图片未检到人脸 |
4 | 目标图片未检到人脸 |
5 | 处理错误 |
6 | 服务器繁忙 |
101 | API-KEY不正确 |
102 | 未知用户 |
103 | 积分已用完 |
104 | 扣除积分失败 |
Python 代码示例:
import requests import base64 import cv2 import json import numpy as np api_key = '******' # 你的API KEY source_file_path = '...' # 源图片路径 target_file_path = '...' # 目标图片路径 with open(source_file_path, 'rb') as fp: source_base64 = base64.b64encode(fp.read()).decode('utf8') with open(target_file_path, 'rb') as fp: target_base64 = base64.b64encode(fp.read()).decode('utf8') url = 'https://api.shiliuai.com/api/face_swap/v1' headers = {'APIKEY': api_key, "Content-Type": "application/json"} data = { "source_base64": source_base64, "target_base64": target_base64 } response = requests.post(url=url, headers=headers, json=data) response = json.loads(response.content) """ 成功:{'code': 0, 'msg': 'OK', 'msg_cn': '成功', 'source_id': source id, ''result_base64': result_base64} or 失败:{'code': error_code, 'msg': error_msg, 'msg_cn': 错误信息} """ result_base64 = response['result_base64'] file_bytes = base64.b64decode(result_base64) f = open('result.jpg', 'wb') f.write(file_bytes) f.close() image = np.asarray(bytearray(file_bytes), dtype=np.uint8) image = cv2.imdecode(image, cv2.IMREAD_UNCHANGED) cv2.imshow('result', image) cv2.waitKey(0)
PHP 代码示例:
$url = "https://api.shiliuai.com/api/face_swap/v1"; $method = "POST"; $apikey = "******"; $header = array(); array_push($header, "APIKEY:" . $apikey); array_push($header, "Content-Type:application/json"); $source_file_path = "..."; $source_handle = fopen($source_file_path, "r"); $source_image = fread($source_handle, filesize($source_file_path)); fclose($source_handle); $source_base64 = base64_encode($source_image); $target_file_path = "..."; $target_handle = fopen($target_file_path, "r"); $target_image = fread($target_handle, filesize($target_file_path)); fclose($target_handle); $target_base64 = base64_encode($target_image); $data = array( "source_base64"=> $source_base64, "target_base64"=> $target_base64 ); $post_data = json_encode($data); $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($curl); var_dump($response);