组合接口API
更新时间:2023-01-31
接口描述
提供一个API接口,同时调用多个模型服务。支持图像识别的多个接口:通用物体和场景识别、图像单主体检测、动物识别、植物识别、果蔬识别、自定义菜品识别-检索、菜品识别、红酒识别、货币识别、地标识别、图像多主体检测等12个模型服务的调用。可通过入参灵活指定需要调用的模型服务,传入图像,返回指定模型的识别结果。
- 组合服务接口根据请求参数确定要使用的模型服务类型,具体的请求参数和返回结果见下方描述。
- 图像识别相关接口同时提供独立的服务接口,针对每个模型的出入参,可参考百度AI开放平台官网每个独立服务接口的文档。
接口使用说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/json;charset=utf-8 |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 类型 | 是否必须 | 说明 |
---|---|---|---|
image | string | 是 | 图像数据,base64编码,示例如:/9j/4AAQSkZJRgABAQEPMpJkR0FdXVridlPy/9k= |
imgUrl | string | 否 | 图像Url,不能与image并存,不需要urlEncode,示例如:https://aip.bdstatic.com/portal-pc-node/dist/1591263471100/images/technology/imagerecognition/general/1.jpg |
scenes | array | 是 | 指定本次调用的模型服务,以字符串数组表示。元素含义如下: advanced_general:通用物体和场景识别 object_detect:图像单主体检测 multi_object_detect:图像多主体检测 animal:动物识别 plant:植物识别 ingredient:果蔬识别 dish_search:自定义菜品识别-检索 dishs:菜品识别 red_wine:红酒识别 currency:货币识别 landmark:地标识别 示例如:“scenes”:[“currency”,”animal”,”plant”] |
sceneConf | jsonObject | 否 | 对特定服务,支持的个性化参数,若不填则使用默认设置。jsonObject说明: key为要设置入参的服务类型,可取值同scenes字段,例如: advanced_general:通用物体和场景识别 object_detect:图像单主体检测 value为各模型服务个性化参数,详情请参照下文或百度AI开放平台官网“图像识别具体接口文档” |
请求Body示例:
{ // image 与 imgUrl二选其一
"image": "/9j/4AAQSkZJRgABAQEPMJkR0FdXVridlPy/9k=",
"scenes": ["animal","plant","ingredient","dishs", "red_wine","currency","landmark"],
"sceneConf": { // 可不填写
"advanced_general": {}, // 可不填写
"animal": {
"top_num": "3",
"baike_num": "3"
}
}
}
请求示例代码
提示一:使用示例代码前,请记得替换其中的示例Token、图像地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
#图像识别组合API
curl -i -k 'https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?access_token=【调用鉴权接口获取的token】' --data '{"imgUrl":"【网络图片地址】","scenes":["animal","plant","ingredient","dishs", "red_wine","currency","landmark"]}' -H 'Content-Type:application/json; charset=UTF-8'
<?php
/**
* 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
// 初始化curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// 要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// post提交方式
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
// 运行curl
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
$token = '[调用鉴权接口获取的token]';
$url = 'https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?access_token=' . $token;
$bodys = "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}"
$res = request_post($url, $bodys);
var_dump($res);
package com.baidu.ai.aip;
import com.baidu.ai.aip.utils.HttpUtil;
import com.baidu.ai.aip.utils.GsonUtils;
import java.util.*;
/**
* 图像识别组合API
*/
public class ImgCombination {
/**
* 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
* 下载
*/
public static String imgCombination() {
// 请求url
String url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
try {
Map<String, Object> map = new HashMap<>();
map.put("imgUrl", "【网络图片地址】");
List<Object> scenes = new ArrayList<>();
scenes.add("animal");
scenes.add("plant");
scenes.add("ingredient");
scenes.add("dishs");
scenes.add("red_wine");
scenes.add("currency");
scenes.add("landmark");
map.put("scenes", scenes);
String param = GsonUtils.toJson(map);
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "[调用鉴权接口获取的token]";
String result = HttpUtil.post(url, accessToken, "application/json", param);
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ImgCombination.imgCombination();
}
}
# encoding:utf-8
import requests
'''
图像识别组合API
'''
request_url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination"
params = "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}"
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())
#include <iostream>
#include <curl/curl.h>
// libcurl库下载链接:https://curl.haxx.se/download.html
// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
const static std::string request_url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
static std::string imgCombination_result;
/**
* curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
* @param 参数定义见libcurl文档
* @return 返回值定义见libcurl文档
*/
static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
// 获取到的body存放在ptr中,先将其转换为string格式
imgCombination_result = std::string((char *) ptr, size * nmemb);
return size * nmemb;
}
/**
* 图像识别组合API
* @return 调用成功返回0,发生错误返回其他错误码
*/
int imgCombination(std::string &json_result, const std::string &access_token) {
std::string url = request_url + "?access_token=" + access_token;
CURL *curl = NULL;
CURLcode result_code;
int is_success;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.data());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}");
result_code = curl_easy_perform(curl);
if (result_code != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(result_code));
is_success = 1;
return is_success;
}
json_result = imgCombination_result;
curl_easy_cleanup(curl);
is_success = 0;
} else {
fprintf(stderr, "curl_easy_init() failed.");
is_success = 1;
}
return is_success;
}
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace com.baidu.ai
{
public class ImgCombination
{
// 图像识别组合API
public static string imgCombination()
{
string token = "[调用鉴权接口获取的token]";
string host = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
String str = "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}";
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("图像识别组合API:");
Console.WriteLine(result);
return result;
}
}
}
返回说明
返回参数
字段 | 类型 | 是否必须 | 说明 |
---|---|---|---|
log_id | uint64 | 是 | 唯一的log id,用于问题定位。 |
result | object | 是 | 返回结果json串,其内包含要调用的各个模型服务的返回结果。 |
advanced_general | object | 否 | 「通用物体和场景识别」服务返回结果。请参照“通用物体和场景识别接口文档” |
object_detect | object | 否 | 「图像单主体检测」服务返回结果。请参照“图像单主体检测接口文档” |
multi_object_detect | object | 否 | 「图像多主体检测」服务返回结果。请参照“图像多主体检测接口文档” |
animal | object | 否 | 「动物识别」服务返回结果。请参照“动物识别接口文档” |
plant | object | 否 | 「植物识别」服务返回结果。请参照“植物识别接口文档” |
ingredient | object | 否 | 「果蔬识别」服务返回结果。请参照“果蔬识别接口文档” |
dish_search | object | 否 | 「自定义菜品识别-检索」服务返回结果。请参照“自定义菜品识别-检索接口文档” |
dishs | object | 否 | 「菜品识别」服务返回结果。请参照“菜品识别接口文档” |
red_wine | object | 否 | 「红酒识别」服务返回结果。请参照“红酒识别接口文档” |
currency | object | 否 | 「货币识别」服务返回结果。请参照“货币识别接口文档” |
landmark | object | 否 | 「地标识别」服务返回结果。请参照“地标识别接口文档” |
返回示例
- 成功响应示例:
{
"result": {
"plant": {
"result": [
{
"score": 0.4343205690383911,
"name": "非植物"
}
],
"log_id": 1888121902459717818
},
"animal": {
"result": [
{
"score": "0.71102",
"name": "松鼠猴"
},
{
"score": "0.0548481",
"name": "卷尾猴"
},
{
"score": "0.0444465",
"name": "蜘蛛猴"
},
{
"score": "0.041044",
"name": "金丝猴"
},
{
"score": "0.0377901",
"name": "长尾猴"
},
{
"score": "0.0176039",
"name": "长臂猿"
}
],
"log_id": 7358927584635575930
}
},
"log_id": 15904950726810006
}
- 失败响应示例:
{
"log_id": 15904942500190005,
"error_msg": "service not support",
"error_code": 216102
}
错误码说明
错误码 | 错误信息 | 描述 |
---|---|---|
415 | not support the media type | 请求格式错误,请检查请求参数代码 |
216101 | not enough param | 参数不足 |
216102 | service not support | 输入了不支持的底层服务类型 |
216500 | unknown error | 未知错误 |
282801 | image and imgUrl are empty | image和imgUrl均为空 |
282802 | either image and imgUrl has value | image和imgUrl只能有一个有值 |
282804 | download image error | 图片下载失败 |
282000 | logic internal error | 业务逻辑层错误 |