所有文档

          图像识别

          自定义菜品识别

          接口描述

          该请求用于入库自定义的单菜品图,实现上传多菜品图的精准识别,返回具体的菜名、位置、置信度信息 。自定义菜品识别包括三个子接口:单菜品入库、菜品检索、菜品删除。

          注意:在正式使用之前,请先在控制台创建应用,并在产品服务/图像识别-应用列表/应用详情最下方建库后,自定义菜品识别下的所有子接口方可使用。

          自定义菜品-入库

          子接口说明

          该接口实现单张菜品图片入库,入库时需要同步提交图片及可关联至本地菜品图库的摘要信息(具体变量为brief,brief可传入图片在本地标记id、图片url、图片名称等)

          入库图片格式要求

          1、base64编码不超过4M,图片长宽不限,建议长宽范围300pixel~3000pixel, 建议长宽比3:1之内,图片格式为通用的jpg、jpeg、png、bmp。

          2、入库图为单个菜品图,如果检测出多菜或者0个菜,则无法入库。

          3、入库的单个菜品图的背景尽可能和实际场景(比如餐厅)需要识别的场景相似,如快餐店自助结算的场景,建议拍摄【单个菜在餐盘中】之类的图片。

          4、为实现较好的识别效果,单个菜品图尽量入库多张(不同摆盘、不同光线等情况下的图片),越多识别准确率越高。

          请求说明

          请求示例

          HTTP 方法:POST

          请求URL: https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/add

          URL参数:

          参数
          access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

          Header如下:

          参数
          Content-Type application/x-www-form-urlencoded

          Body中放置请求参数,参数详情如下:

          请求参数

          参数 是否必选 类型 可选值范围 说明
          image string - Base64编码字符串,以图片文件形式请求时必填。(支持图片格式:jpg,bmp,png,jpeg-1574500464364.),图片大小不超过4M。最短边至少300px,最长边最大3000px。注意:图片需要base64编码、去掉编码头后再进行urlencode。
          brief string - 菜品名称摘要信息,检索时带回,不超过256B。由于检索后需要与本地数据库关联,建议brief可传入图片在本地标记id、图片url、图片名称等,如"brief": "{\"菜名\":\"鱼香肉丝\"}"

          请求代码示例

          提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。

          提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

          curl -i -k 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/add?access_token=【调用鉴权接口获取的token】' --data 'brief=1&image=【图片Base64编码,需UrlEncode】&sub_lib=[appid]' -H 'Content-Type:application/x-www-form-urlencoded'
          <?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/rest/2.0/image-classify/v1/realtime_search/dish/add?access_token=' . $token;
          $img = file_get_contents('[本地文件路径]');
          $img = base64_encode($img);
          $bodys = array(
              'brief' => 1,
              'image' => $img,
              'sub_lib' => "[appid]"
          );
          $res = request_post($url, $bodys);
          
          var_dump($res);
          package com.baidu.ai.aip;
          
          import com.baidu.ai.aip.utils.Base64Util;
          import com.baidu.ai.aip.utils.FileUtil;
          import com.baidu.ai.aip.utils.HttpUtil;
          
          import java.net.URLEncoder;
          
          /**
          * 菜品识别入库
          */
          public class DishAdd {
          
              /**
              * 重要提示代码中所需工具类
              * 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 dishAdd() {
                  // 请求url
                  String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/add";
                  try {
                      // 本地文件路径
                      String filePath = "[本地文件路径]";
                      byte[] imgData = FileUtil.readFileByBytes(filePath);
                      String imgStr = Base64Util.encode(imgData);
                      String imgParam = URLEncoder.encode(imgStr, "UTF-8");
          
                      String param = "brief=" + 1 + "&image=" + imgParam + "&sub_lib=" + "[appid]";
          
                      // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
                      String accessToken = "[调用鉴权接口获取的token]";
          
                      String result = HttpUtil.post(url, accessToken, param);
                      System.out.println(result);
                      return result;
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return null;
              }
          
              public static void main(String[] args) {
                  DishAdd.dishAdd();
              }
          }
          # encoding:utf-8
          
          import requests
          import base64
          
          '''
          菜品识别入库
          '''
          
          request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/add"
          # 二进制方式打开图片文件
          f = open('[本地文件]', 'rb')
          img = base64.b64encode(f.read())
          
          params = {"brief":1,"image":img,"sub_lib":"[appid]"}
          access_token = '[调用鉴权接口获取的token]'
          request_url = request_url + "?access_token=" + access_token
          headers = {'content-type': 'application/x-www-form-urlencoded'}
          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/rest/2.0/image-classify/v1/realtime_search/dish/add";
          static std::string dishAdd_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格式
              dishAdd_result = std::string((char *) ptr, size * nmemb);
              return size * nmemb;
          }
          /**
           * 菜品识别入库
           * @return 调用成功返回0,发生错误返回其他错误码
           */
          int dishAdd(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_httppost *post = NULL;
                  curl_httppost *last = NULL;
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "brief", CURLFORM_COPYCONTENTS, "1", CURLFORM_END);
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "sub_lib", CURLFORM_COPYCONTENTS, "[appid]", CURLFORM_END);
          
                  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
                  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
                  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 = dishAdd_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 DishAdd
              {
                  // 菜品识别入库
                  public static string dishAdd()
                  {
                      string token = "[调用鉴权接口获取的token]";
                      string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/add?access_token=" + token;
                      Encoding encoding = Encoding.Default;
                      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
                      request.Method = "post";
                      request.KeepAlive = true;
                      // 图片的base64编码
                      string base64 = getFileBase64("[本地图片文件]");
                      String str = "brief=" + 1 + "&image=" + HttpUtility.UrlEncode(base64) + "&sub_lib=" + "[appid]";
                      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("菜品识别入库:");
                      Console.WriteLine(result);
                      return result;
                  }
          
                  public static String getFileBase64(String fileName) {
                      FileStream filestream = new FileStream(fileName, FileMode.Open);
                      byte[] arr = new byte[filestream.Length];
                      filestream.Read(arr, 0, (int)filestream.Length);
                      string baser64 = Convert.ToBase64String(arr);
                      filestream.Close();
                      return baser64;
                  }
              }
          }

          返回说明

          返回参数

          字段 是否必选 类型 说明
          log_id uint64 唯一的log id,用于问题定位
          cont_sign string 输入图片签名

          返回示例

          HTTP/1.1 200 OK
          x-bce-request-id: 73c4e74c-3101-4a00-bf44-fe246959c05e
          Cache-Control: no-cache
          Server: BWS
          Date: Tue, 18 Oct 2016 02:21:01 GMT
          Content-Type: application/json;charset=UTF-8
          {
              "cont_sign": "222,333",
              "log_id": 1837888624
          }

          自定义菜品-检索

          子接口说明

          在已自建菜品库并入库的情况下,该接口实现单菜品/多菜品的识别。

          请求图片格式要求

          base64编码不超过4M,图片长宽不限,建议长宽范围300pixel~3000pixel, 建议长宽比3:1之内,图片格式为通用的jpg、jpeg、png、bmp。

          请求说明

          请求示例

          HTTP 方法:POST

          请求URL: https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/search

          URL参数:

          参数
          access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

          Header如下:

          参数
          Content-Type application/x-www-form-urlencoded

          Body中放置请求参数,参数详情如下:

          请求参数

          参数 是否必选 类型 可选值范围 说明
          image string - Base64编码字符串,以图片文件形式请求时必填。图片大小不超过4M。最短边至少300px,最长边最大3000px。注意:图片需要base64编码、去掉编码头后再进行urlencode。

          请求代码示例

          提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。

          提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

          curl -i -k 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/search?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】&sub_lib=[appid]' -H 'Content-Type:application/x-www-form-urlencoded'
          <?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/rest/2.0/image-classify/v1/realtime_search/dish/search?access_token=' . $token;
          $img = file_get_contents('[本地文件路径]');
          $img = base64_encode($img);
          $bodys = array(
              'image' => $img,
              'sub_lib' => "[appid]"
          );
          $res = request_post($url, $bodys);
          
          var_dump($res);
          package com.baidu.ai.aip;
          
          import com.baidu.ai.aip.utils.Base64Util;
          import com.baidu.ai.aip.utils.FileUtil;
          import com.baidu.ai.aip.utils.HttpUtil;
          
          import java.net.URLEncoder;
          
          /**
          * 菜品识别检索
          */
          public class DishSearch {
          
              /**
              * 重要提示代码中所需工具类
              * 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 dishSearch() {
                  // 请求url
                  String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/search";
                  try {
                      // 本地文件路径
                      String filePath = "[本地文件路径]";
                      byte[] imgData = FileUtil.readFileByBytes(filePath);
                      String imgStr = Base64Util.encode(imgData);
                      String imgParam = URLEncoder.encode(imgStr, "UTF-8");
          
                      String param = "image=" + imgParam + "&sub_lib=" + "[appid]";
          
                      // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
                      String accessToken = "[调用鉴权接口获取的token]";
          
                      String result = HttpUtil.post(url, accessToken, param);
                      System.out.println(result);
                      return result;
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return null;
              }
          
              public static void main(String[] args) {
                  DishSearch.dishSearch();
              }
          }
          # encoding:utf-8
          
          import requests
          import base64
          
          '''
          菜品识别检索
          '''
          
          request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/search"
          # 二进制方式打开图片文件
          f = open('[本地文件]', 'rb')
          img = base64.b64encode(f.read())
          
          params = {"image":img,"sub_lib":"[appid]"}
          access_token = '[调用鉴权接口获取的token]'
          request_url = request_url + "?access_token=" + access_token
          headers = {'content-type': 'application/x-www-form-urlencoded'}
          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/rest/2.0/image-classify/v1/realtime_search/dish/search";
          static std::string dishSearch_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格式
              dishSearch_result = std::string((char *) ptr, size * nmemb);
              return size * nmemb;
          }
          /**
           * 菜品识别检索
           * @return 调用成功返回0,发生错误返回其他错误码
           */
          int dishSearch(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_httppost *post = NULL;
                  curl_httppost *last = NULL;
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "sub_lib", CURLFORM_COPYCONTENTS, "[appid]", CURLFORM_END);
          
                  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
                  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
                  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 = dishSearch_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 DishSearch
              {
                  // 菜品识别检索
                  public static string dishSearch()
                  {
                      string token = "[调用鉴权接口获取的token]";
                      string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/search?access_token=" + token;
                      Encoding encoding = Encoding.Default;
                      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
                      request.Method = "post";
                      request.KeepAlive = true;
                      // 图片的base64编码
                      string base64 = getFileBase64("[本地图片文件]");
                      String str = "image=" + HttpUtility.UrlEncode(base64) + "&sub_lib=" + "[appid]";
                      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("菜品识别检索:");
                      Console.WriteLine(result);
                      return result;
                  }
          
                  public static String getFileBase64(String fileName) {
                      FileStream filestream = new FileStream(fileName, FileMode.Open);
                      byte[] arr = new byte[filestream.Length];
                      filestream.Read(arr, 0, (int)filestream.Length);
                      string baser64 = Convert.ToBase64String(arr);
                      filestream.Close();
                      return baser64;
                  }
              }
          }

          返回说明

          返回参数

          字段 是否必选 类型 说明
          log_id uint64 唯一的log id,用于问题定位
          result_num uint32 检索结果数
          result object[] 识别结果数组
          +location object 菜品在图片中的位置
          ++left int 水平坐标(左上角原点)
          ++top int 垂直坐标(左上角原点)
          ++width int 宽度
          ++height int 高度
          +dishes object[] 识别结果数组
          ++cont_sign string 图片签名,可以用来删除图片或问题定位
          ++score float 图片相关性,0-1
          ++brief string 入库菜品时添加的brief信息

          返回示例

          HTTP/1.1 200 OK
          x-bce-request-id: 73c4e74c-3101-4a00-bf44-fe246959c05e
          Cache-Control: no-cache
          Server: BWS
          Date: Tue, 18 Oct 2016 02:21:01 GMT
          Content-Type: application/json;charset=UTF-8
          {
              "result_num": 2,
              "result": [
                  {
                      "location": {
                          "left": 127,
                          "top": 70,
                          "width": 103,
                          "height": 101
                      },
                      "dishes": [
                          {
                              "score": 0.47976872324944,
                              "brief": "{\"name\":\"baicai\",\"id\":333}",
                              "cont_sign": "475124309,1080176642"
                          }
                      ]
                  },
                  {
                      "location": {
                          "left": 312,
                          "top": 163,
                          "width": 78,
                          "height": 79
                      },
                      "dishes": [
                          {
                              "score": 0.013810452073812,
                              "brief": "{\"name\":\"doufu\",\"id\":333}",
                              "cont_sign": "475124309,1080176642"
                          }
                      ]
                  }
              ],
              "log_id": 1837888624
          }

          自定义菜品-删除

          子接口说明

          该接口实现已入库菜品图片的删除操作。

          请求说明

          请求示例

          HTTP 方法:POST

          请求URL: https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/delete

          URL参数:

          参数
          access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

          Header如下:

          参数
          Content-Type application/x-www-form-urlencoded

          Body中放置请求参数,参数详情如下:

          请求参数

          参数 是否必选 类型 可选值范围 说明
          image string - Base64编码字符串(和cont_sign二选一),以图片文件形式请求时必填。图片大小不超过4M。最短边至少300px,最长边最大3000px。注意:图片需要base64编码、去掉编码头后再进行urlencode。
          cont_sign string - 图片签名(和image二选一,image优先级更高)

          请求代码示例

          提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。

          提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

          curl -i -k 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/delete?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】&sub_lib=[appid]' -H 'Content-Type:application/x-www-form-urlencoded'
          <?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/rest/2.0/image-classify/v1/realtime_search/dish/delete?access_token=' . $token;
          $img = file_get_contents('[本地文件路径]');
          $img = base64_encode($img);
          $bodys = array(
              'image' => $img,
              'sub_lib' => "[appid]"
          );
          $res = request_post($url, $bodys);
          
          var_dump($res);
          package com.baidu.ai.aip;
          
          import com.baidu.ai.aip.utils.Base64Util;
          import com.baidu.ai.aip.utils.FileUtil;
          import com.baidu.ai.aip.utils.HttpUtil;
          
          import java.net.URLEncoder;
          
          /**
          * 菜品识别删除
          */
          public class DishDelete {
          
              /**
              * 重要提示代码中所需工具类
              * 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 dishDelete() {
                  // 请求url
                  String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/delete";
                  try {
                      // 本地文件路径
                      String filePath = "[本地文件路径]";
                      byte[] imgData = FileUtil.readFileByBytes(filePath);
                      String imgStr = Base64Util.encode(imgData);
                      String imgParam = URLEncoder.encode(imgStr, "UTF-8");
          
                      String param = "image=" + imgParam + "&sub_lib=" + "[appid]";
          
                      // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
                      String accessToken = "[调用鉴权接口获取的token]";
          
                      String result = HttpUtil.post(url, accessToken, param);
                      System.out.println(result);
                      return result;
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return null;
              }
          
              public static void main(String[] args) {
                  DishDelete.dishDelete();
              }
          }
          # encoding:utf-8
          
          import requests
          import base64
          
          '''
          菜品识别删除
          '''
          
          request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/delete"
          # 二进制方式打开图片文件
          f = open('[本地文件]', 'rb')
          img = base64.b64encode(f.read())
          
          params = {"image":img,"sub_lib":"[appid]"}
          access_token = '[调用鉴权接口获取的token]'
          request_url = request_url + "?access_token=" + access_token
          headers = {'content-type': 'application/x-www-form-urlencoded'}
          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/rest/2.0/image-classify/v1/realtime_search/dish/delete";
          static std::string dishDelete_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格式
              dishDelete_result = std::string((char *) ptr, size * nmemb);
              return size * nmemb;
          }
          /**
           * 菜品识别删除
           * @return 调用成功返回0,发生错误返回其他错误码
           */
          int dishDelete(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_httppost *post = NULL;
                  curl_httppost *last = NULL;
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
                  curl_formadd(&post, &last, CURLFORM_COPYNAME, "sub_lib", CURLFORM_COPYCONTENTS, "[appid]", CURLFORM_END);
          
                  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
                  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
                  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 = dishDelete_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 DishDelete
              {
                  // 菜品识别删除
                  public static string dishDelete()
                  {
                      string token = "[调用鉴权接口获取的token]";
                      string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/dish/delete?access_token=" + token;
                      Encoding encoding = Encoding.Default;
                      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
                      request.Method = "post";
                      request.KeepAlive = true;
                      // 图片的base64编码
                      string base64 = getFileBase64("[本地图片文件]");
                      String str = "image=" + HttpUtility.UrlEncode(base64) + "&sub_lib=" + "[appid]";
                      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("菜品识别删除:");
                      Console.WriteLine(result);
                      return result;
                  }
          
                  public static String getFileBase64(String fileName) {
                      FileStream filestream = new FileStream(fileName, FileMode.Open);
                      byte[] arr = new byte[filestream.Length];
                      filestream.Read(arr, 0, (int)filestream.Length);
                      string baser64 = Convert.ToBase64String(arr);
                      filestream.Close();
                      return baser64;
                  }
              }
          }

          返回说明

          返回参数

          字段 是否必选 类型 说明
          log_id uint64 唯一的log id,用于问题定位

          返回示例

          HTTP/1.1 200 OK
          x-bce-request-id: 73c4e74c-3101-4a00-bf44-fe246959c05e
          Cache-Control: no-cache
          Server: BWS
          Date: Tue, 18 Oct 2016 02:21:01 GMT
          Content-Type: application/json;charset=UTF-8
          {
              "log_id": 1837888624
          }
          上一篇
          果蔬识别
          下一篇
          菜品识别