组合接口API
更新时间:2024-12-09
接口描述
提供一个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示例:
Plain Text
1{ // image 与 imgUrl二选其一
2 "image": "/9j/4AAQSkZJRgABAQEPMJkR0FdXVridlPy/9k=",
3 "scenes": ["animal","plant","ingredient","dishs", "red_wine","currency","landmark"],
4 "sceneConf": { // 可不填写
5 "advanced_general": {}, // 可不填写
6 "animal": {
7 "top_num": "3",
8 "baike_num": "3"
9 }
10 }
11}
请求示例代码
提示一:使用示例代码前,请记得替换其中的示例Token、图像地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
1#图像识别组合API
2curl -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'
1<?php
2/**
3 * 发起http post请求(REST API), 并获取REST请求的结果
4 * @param string $url
5 * @param string $param
6 * @return - http response body if succeeds, else false.
7 */
8function request_post($url = '', $param = '')
9{
10 if (empty($url) || empty($param)) {
11 return false;
12 }
13
14 $postUrl = $url;
15 $curlPost = $param;
16 // 初始化curl
17 $curl = curl_init();
18 curl_setopt($curl, CURLOPT_URL, $postUrl);
19 curl_setopt($curl, CURLOPT_HEADER, 0);
20 // 要求结果为字符串且输出到屏幕上
21 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
22 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
23 // post提交方式
24 curl_setopt($curl, CURLOPT_POST, 1);
25 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
26 // 运行curl
27 $data = curl_exec($curl);
28 curl_close($curl);
29
30 return $data;
31}
32
33$token = '[调用鉴权接口获取的token]';
34$url = 'https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?access_token=' . $token;
35$bodys = "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}"
36$res = request_post($url, $bodys);
37
38var_dump($res);
1package com.baidu.ai.aip;
2
3import com.baidu.ai.aip.utils.HttpUtil;
4import com.baidu.ai.aip.utils.GsonUtils;
5
6import java.util.*;
7
8/**
9* 图像识别组合API
10*/
11public class ImgCombination {
12
13 /**
14 * 重要提示代码中所需工具类
15 * FileUtil,Base64Util,HttpUtil,GsonUtils请从
16 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
17 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
18 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
19 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
20 * 下载
21 */
22 public static String imgCombination() {
23 // 请求url
24 String url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
25 try {
26 Map<String, Object> map = new HashMap<>();
27 map.put("imgUrl", "【网络图片地址】");
28 List<Object> scenes = new ArrayList<>();
29 scenes.add("animal");
30 scenes.add("plant");
31 scenes.add("ingredient");
32 scenes.add("dishs");
33 scenes.add("red_wine");
34 scenes.add("currency");
35 scenes.add("landmark");
36 map.put("scenes", scenes);
37
38 String param = GsonUtils.toJson(map);
39
40 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
41 String accessToken = "[调用鉴权接口获取的token]";
42
43 String result = HttpUtil.post(url, accessToken, "application/json", param);
44 System.out.println(result);
45 return result;
46 } catch (Exception e) {
47 e.printStackTrace();
48 }
49 return null;
50 }
51
52 public static void main(String[] args) {
53 ImgCombination.imgCombination();
54 }
55}
1# encoding:utf-8
2
3import requests
4
5'''
6图像识别组合API
7'''
8
9request_url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination"
10
11params = "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}"
12access_token = '[调用鉴权接口获取的token]'
13request_url = request_url + "?access_token=" + access_token
14headers = {'content-type': 'application/json'}
15response = requests.post(request_url, data=params, headers=headers)
16if response:
17 print (response.json())
1#include <iostream>
2#include <curl/curl.h>
3
4// libcurl库下载链接:https://curl.haxx.se/download.html
5// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
6const static std::string request_url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
7static std::string imgCombination_result;
8/**
9 * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
10 * @param 参数定义见libcurl文档
11 * @return 返回值定义见libcurl文档
12 */
13static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
14 // 获取到的body存放在ptr中,先将其转换为string格式
15 imgCombination_result = std::string((char *) ptr, size * nmemb);
16 return size * nmemb;
17}
18/**
19 * 图像识别组合API
20 * @return 调用成功返回0,发生错误返回其他错误码
21 */
22int imgCombination(std::string &json_result, const std::string &access_token) {
23 std::string url = request_url + "?access_token=" + access_token;
24 CURL *curl = NULL;
25 CURLcode result_code;
26 int is_success;
27 curl = curl_easy_init();
28 if (curl) {
29 curl_easy_setopt(curl, CURLOPT_URL, url.data());
30 curl_easy_setopt(curl, CURLOPT_POST, 1);
31 curl_slist *headers = NULL;
32 headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
33 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
34 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}");
35 result_code = curl_easy_perform(curl);
36 if (result_code != CURLE_OK) {
37 fprintf(stderr, "curl_easy_perform() failed: %s\n",
38 curl_easy_strerror(result_code));
39 is_success = 1;
40 return is_success;
41 }
42 json_result = imgCombination_result;
43 curl_easy_cleanup(curl);
44 is_success = 0;
45 } else {
46 fprintf(stderr, "curl_easy_init() failed.");
47 is_success = 1;
48 }
49 return is_success;
50}
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5using System.Web;
6
7namespace com.baidu.ai
8{
9 public class ImgCombination
10 {
11 // 图像识别组合API
12 public static string imgCombination()
13 {
14 string token = "[调用鉴权接口获取的token]";
15 string host = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?access_token=" + token;
16 Encoding encoding = Encoding.Default;
17 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
18 request.Method = "post";
19 request.KeepAlive = true;
20 String str = "{\"imgUrl\":\"【网络图片地址】\",\"scenes\":[\"animal\",\"plant\",\"ingredient\",\"dishs\", \"red_wine\",\"currency\",\"landmark\"]}";
21 byte[] buffer = encoding.GetBytes(str);
22 request.ContentLength = buffer.Length;
23 request.GetRequestStream().Write(buffer, 0, buffer.Length);
24 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
25 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
26 string result = reader.ReadToEnd();
27 Console.WriteLine("图像识别组合API:");
28 Console.WriteLine(result);
29 return result;
30 }
31 }
32}
返回说明
返回参数
字段 | 类型 | 是否必须 | 说明 |
---|---|---|---|
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 | 否 | 「地标识别」服务返回结果。请参照“地标识别接口文档” |
返回示例
- 成功响应示例:
Plain Text
1{
2 "result": {
3 "plant": {
4 "result": [
5 {
6 "score": 0.4343205690383911,
7 "name": "非植物"
8 }
9 ],
10 "log_id": 1888121902459717818
11 },
12 "animal": {
13 "result": [
14 {
15 "score": "0.71102",
16 "name": "松鼠猴"
17 },
18 {
19 "score": "0.0548481",
20 "name": "卷尾猴"
21 },
22 {
23 "score": "0.0444465",
24 "name": "蜘蛛猴"
25 },
26 {
27 "score": "0.041044",
28 "name": "金丝猴"
29 },
30 {
31 "score": "0.0377901",
32 "name": "长尾猴"
33 },
34 {
35 "score": "0.0176039",
36 "name": "长臂猿"
37 }
38 ],
39 "log_id": 7358927584635575930
40 }
41 },
42 "log_id": 15904950726810006
43}
- 失败响应示例:
Plain Text
1{
2 "log_id": 15904942500190005,
3 "error_msg": "service not support",
4 "error_code": 216102
5}
错误码说明
错误码 | 错误信息 | 描述 |
---|---|---|
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 | 业务逻辑层错误 |