请求URL:https://face-api.shiliuai.com/api/face_liveness/v1
请求方式:POST
返回类型:JSON
参数 | 类型 | 说明 |
---|---|---|
Authorization | string | 'APPCODE ' + 您的AppCode (注意英文空格) |
Content-Type | string | application/json |
参数 | 类型 | 说明 |
---|---|---|
Content-Type | string | application/json |
x-ca-key | string | 您的AppKey |
x-ca-timestamp | string | 时间戳(毫秒) |
x-ca-signature | string | 签名sign |
参数 | 是否必填 | 类型 | 说明 |
---|---|---|---|
image_base64 | 必填 | string | base64编码的图片文件 |
import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Base64; public class FaceLivenessDemo { // 请求接口 private static final String API_URL = "api/face_liveness/v1"; public static void main(String[] args) { String appcode = "你的APPCODE"; String filePath = "本地图片路径"; demo(appcode, filePath); } // 图片转base64 private static String getBase64(String filePath) throws IOException { byte[] fileContent = Files.readAllBytes(Paths.get(filePath)); return Base64.getEncoder().encodeToString(fileContent); } private static void demo(String appcode, String filePath) { try { // 准备请求数据 String base64Image = getBase64(filePath); String jsonInputString = "{\"image_base64\": \"" + base64Image + "\"}"; // 创建HTTP连接 URL url = new URL(API_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求头 connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", "APPCODE " + appcode); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); // 发送请求体 try(OutputStream os = connection.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); } // 获取响应 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { try(BufferedReader br = new BufferedReader( new InputStreamReader(connection.getInputStream(), "utf-8"))) { StringBuilder response = new StringBuilder(); String responseLine; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } System.out.println(response.toString()); } } else { System.out.println("HTTP请求失败,状态码: " + responseCode); } } catch (Exception e) { e.printStackTrace(); } } }
// 请求接口 const URL = "https://face-api.shiliuai.com/api/face_liveness/v1"; // 图片转base64 function get_base64($file_path) { $data = file_get_contents($file_path); return base64_encode($data); } function demo($appcode, $file_path) { // 请求头 $headers = [ 'Authorization: APPCODE ' . $appcode, 'Content-Type: application/json' ]; // 请求体 $b64 = get_base64($file_path); $data = [ "image_base64" => $b64 ]; // 初始化cURL $ch = curl_init(); // 设置cURL选项 curl_setopt($ch, CURLOPT_URL, URL); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行请求 $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭连接 curl_close($ch); // 处理响应 if ($httpCode == 200) { $content = json_decode($response, true); print_r($content); } else { echo "请求失败,HTTP状态码:" . $httpCode; } } // 主程序 $appcode = "你的APPCODE"; $file_path = "本地图片路径"; demo($appcode, $file_path);
# -*- coding: utf-8 -*- import requests import base64 import json # 请求接口 URL = "https://face-api.shiliuai.com/api/face_liveness/v1" # 图片转base64 def get_base64(file_path): with open(file_path, 'rb') as f: data = f.read() b64 = base64.b64encode(data).decode('utf8') return b64 def demo(appcode, file_path): # 请求头 headers = { 'Authorization': 'APPCODE %s' % appcode, 'Content-Type': 'application/json' } # 请求体 b64 = get_base64(file_path) data = { "image_base64": b64 } # 请求 response = requests.post(url=URL, headers=headers, json=data) content = json.loads(response.content) print(content) if __name__=="__main__": appcode = "你的APPCODE" file_path = "本地图片路径" demo(appcode, file_path)
# -*- coding: utf-8 -*- import requests import base64 import json import hashlib import time # 请求接口 URL = "https://face-api.shiliuai.com/api/face_liveness/v1" # 图片转base64 def get_base64(file_path): with open(file_path, 'rb') as f: data = f.read() b64 = base64.b64encode(data).decode('utf8') return b64 # md5 def md5(s): return hashlib.md5(s.encode("utf8")).hexdigest() def demo(app_key, app_secret, file_path): # 请求头 t = int(time.time() * 1000) s = "%s%d%s" % (app_key, t, app_secret) sign = md5(s) headers = {'x-ca-key': app_key, 'x-ca-timestamp': t, 'x-ca-signature': sign, "Content-Type": "application/json"} # 请求体 b64 = get_base64(file_path) data = { "image_base64": b64 } # 请求 response = requests.post(url=URL, headers=headers, json=data) content = json.loads(response.content) print(content) if __name__=="__main__": app_key = "你的APP_KEY" app_secret = "你的APP_SECRET" file_path = "本地图片路径" demo(app_key, app_secret, file_path)
const URL = "https://face-api.shiliuai.com/api/face_liveness/v1"; // 图片转base64 function get_base64($file_path) { $data = file_get_contents($file_path); return base64_encode($data); } // MD5签名生成 function generate_sign($app_key, $timestamp, $app_secret) { $raw_str = $app_key . $timestamp . $app_secret; return md5($raw_str); } function demo($app_key, $app_secret, $file_path) { // 生成时间戳(毫秒级) $t = round(microtime(true) * 1000); // 生成签名 $sign = generate_sign($app_key, $t, $app_secret); // 请求头 $headers = [ 'x-ca-key: ' . $app_key, 'x-ca-timestamp: ' . $t, 'x-ca-signature: ' . $sign, 'Content-Type: application/json' ]; // 请求体 $b64 = get_base64($file_path); $data = [ "image_base64" => $b64 ]; // 初始化cURL $ch = curl_init(); // 设置cURL选项 curl_setopt($ch, CURLOPT_URL, URL); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行请求 $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭连接 curl_close($ch); // 处理响应 if ($httpCode == 200) { $content = json_decode($response, true); print_r($content); } else { echo "请求失败,HTTP状态码:" . $httpCode; } } // 主程序 $app_key = "你的APP_KEY"; $app_secret = "你的APP_SECRET"; $file_path = "本地图片路径"; demo($app_key, $app_secret, $file_path);
import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; import java.util.Base64; public class FaceAuthDemo { private static final String API_URL = "https://face-api.shiliuai.com/api/face_liveness/v1"; public static void main(String[] args) { String appKey = "你的APP_KEY"; String appSecret = "你的APP_SECRET"; String filePath = "本地图片路径"; demo(appKey, appSecret, filePath); } // 图片转Base64 private static String getBase64(String filePath) throws IOException { byte[] fileContent = Files.readAllBytes(Paths.get(filePath)); return Base64.getEncoder().encodeToString(fileContent); } // MD5签名生成 private static String generateSign(String appKey, long timestamp, String appSecret) { try { String rawStr = appKey + timestamp + appSecret; MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digest = md.digest(rawStr.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b)); } return sb.toString(); } catch (Exception e) { throw new RuntimeException("MD5生成失败", e); } } private static void demo(String appKey, String appSecret, String filePath) { try { // 生成时间戳(毫秒级) long timestamp = System.currentTimeMillis(); // 生成签名 String sign = generateSign(appKey, timestamp, appSecret); // 准备请求数据 String base64Image = getBase64(filePath); String jsonInputString = "{\"image_base64\": \"" + base64Image + "\"}"; // 创建HTTP连接 URL url = new URL(API_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求头 connection.setRequestMethod("POST"); connection.setRequestProperty("x-ca-key", appKey); connection.setRequestProperty("x-ca-timestamp", String.valueOf(timestamp)); connection.setRequestProperty("x-ca-signature", sign); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); // 发送请求体 try(OutputStream os = connection.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); } // 获取响应 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { try(BufferedReader br = new BufferedReader( new InputStreamReader(connection.getInputStream(), "utf-8"))) { StringBuilder response = new StringBuilder(); String responseLine; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } System.out.println(response.toString()); } } else { System.out.println("HTTP请求失败,状态码: " + responseCode); } } catch (Exception e) { e.printStackTrace(); } } }
参数 | 参数类型 | 说明 |
---|---|---|
code | int | 错误码 |
msg | string | 错误信息(英文) |
msg_cn | string | 错误信息(中文) |
data | data | 具体看下面 |
错误码 | 说明 |
---|---|
200 | 成功 |
400 | 请求错误 |
401 | 未授权 |
500 | 服务错误 |
人脸活体检测,可以检测图像中的人脸是否为来自认证设备端的近距离裸拍活体人脸对象,可广泛应用在人脸实时采集场景,满足人脸注册认证的真实性和安全性要求。活体判断的前置条件是图像中有人脸。
1. 人脸是否活体
人脸活体检测接口可以从上传的人脸照片中提取独特的面部特征点,判断是否活体。
2. 获取人脸信息
通过检测识别图像中有人脸,返回人脸框与左边距离,人脸框与上边距离,人脸框宽度、高度。
3. 多种输入格式支持
支持多种图片格式(如JPEG、PNG、BMP等)的输入,适应不同应用场景的需求。
4. 实时处理
API具有高效的处理能力,能够在毫秒级时间内完成对比,适合实时性要求高的应用场景。
API类型 | 价格说明 |
---|---|
人脸活体检测接口API | 每次调用消耗一积分 |
如有问题联系右侧“客服” |