logo识别
所有文档

          图像识别

          logo识别

          接口描述

          该请求用于检测和识别图片中的台标、品牌商标等logo信息。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片中logo的名称、位置和置信度。

          使用时,可直接调用logo识别-检索接口,支持识别超过2万类logo名称;当效果欠佳时,可以建立子库(在控制台创建应用并申请建库)并通过调用logo入口接口完成自定义logo入库,再调用logo识别-检索接口,选择在自定义logo库内检索,提高识别效果。

          logo识别-检索

          请求说明

          请求示例

          HTTP 方法:POST

          请求URL: https://aip.baidubce.com/rest/2.0/image-classify/v2/logo

          URL参数:

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

          Header如下:

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

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

          请求参数

          参数 是否必选 类型 可选值范围 说明
          image string - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式.注意:图片需要base64编码、去掉编码头后再进行urlencode。
          custom_lib string - 是否只检索用户子库,true则只检索用户子库,false(默认)为检索底库+用户子库

          请求代码示例

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

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

          curl -i -k 'https://aip.baidubce.com/rest/2.0/image-classify/v2/logo?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】&custom_lib=true' -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/v2/logo?access_token=' . $token;
          $img = file_get_contents('[本地文件路径]');
          $img = base64_encode($img);
          $bodys = array(
              'image' => $img,
              'custom_lib' => true
          );
          $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;
          
          /**
          * logo商标识别
          */
          public class Logo {
          
              /**
              * 重要提示代码中所需工具类
              * 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 logo() {
                  // 请求url
                  String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/logo";
                  try {
                      // 本地文件路径
                      String filePath = "[本地文件路径]";
                      byte[] imgData = FileUtil.readFileByBytes(filePath);
                      String imgStr = Base64Util.encode(imgData);
                      String imgParam = URLEncoder.encode(imgStr, "UTF-8");
          
                      String param = "image=" + imgParam + "&custom_lib=" + true;
          
                      // 注意这里仅为了简化编码每一次请求都去获取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) {
                  Logo.logo();
              }
          }
          # encoding:utf-8
          
          import requests
          import base64
          
          '''
          logo商标识别
          '''
          
          request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/logo"
          # 二进制方式打开图片文件
          f = open('[本地文件]', 'rb')
          img = base64.b64encode(f.read())
          
          params = {"custom_lib":true,"image":img}
          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/v2/logo";
          static std::string logo_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格式
              logo_result = std::string((char *) ptr, size * nmemb);
              return size * nmemb;
          }
          /**
           * logo商标识别
           * @return 调用成功返回0,发生错误返回其他错误码
           */
          int logo(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, "custom_lib", CURLFORM_COPYCONTENTS, "true", 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 = logo_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 Logo
              {
                  // logo商标识别
                  public static string logo()
                  {
                      string token = "[调用鉴权接口获取的token]";
                      string host = "https://aip.baidubce.com/rest/2.0/image-classify/v2/logo?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) + "&custom_lib=" + true;
                      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("logo商标识别:");
                      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 array() 返回结果数组,每一项为一个识别出的logo
          +location object 位置信息(左起像素位置、上起像素位置、像素宽、像素高)
          ++left number 坐起像素位置
          ++top number 上起像素位置
          ++width number 像素宽
          ++height number 像素高
          +name string 识别的品牌名称
          +probability number 分类结果置信度(0--1.0)
          +type number type=0为1千种高优商标识别结果;type=1为2万类logo库的结果;其它type为自定义logo库结果

          返回示例

          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": 843411868,
            "result_num": 1,
            "result": [
              {
                "type": 0,
                "name": "科颜氏",
                "probability": 0.99998807907104,
                "location": {
                  "width": 296,
                  "top": 20,
                  "height": 128,
                  "left": 23
                }
              }
            ]
          }

          logo识别—入库

          使用入库接口请先在控制台创建应用并申请建库,建库成功后方可正常使用。

          添加logo图片规范

          1. logo商标入库需要待识别商标的紧致包围盒图像,下面是一组示例:
          2. 每种商标不同场景多入库一些图像有助于获得更好的识别效果。如上图中“美素佳儿”图标。
          3. 入库的图像最好是每种商标的最小识别单元。例如入库下面第一个图像的效果不如将下面后三个商标图片入库的效果好。

            入库后识别效果一般:

          分别入库后效果更好

          不适合入库的图片示例

          1. 尽量不要含有背景,像下面图入库效果肯定不佳,需要裁剪。

          1. 不含logo的图像入库,不会返回该图像,因为算法先回做商标检测,如果没有商标,返回未识别结果,如下图。

          请求示例

          HTTP 方法:POST

          请求URL: https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add

          URL参数:

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

          Header如下:

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

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

          请求参数

          参数 是否必选 类型 可选值范围 说明
          image string - 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式.注意:图片需要base64编码、去掉编码头后再进行urlencode。
          brief string - 此处需要传对应的品牌名称name字段,必须为json格式,检索时带回。name长度小于100B,示例"{\"name\": \"abc\"}" 。brief需要进行urlencode。

          请求代码示例

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

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

          curl -i -k 'https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】&brief={'name': 'abc'}' -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/realtime_search/v1/logo/add?access_token=' . $token;
          $img = file_get_contents('[本地文件路径]');
          $img = base64_encode($img);
          $bodys = array(
              'image' => $img,
              'brief' => "{'name': 'abc'}"
          );
          $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;
          
          /**
          * logo商标识别—入库
          */
          public class LogoAdd {
          
              /**
              * 重要提示代码中所需工具类
              * 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 logoAdd() {
                  // 请求url
                  String url = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add";
                  try {
                      // 本地文件路径
                      String filePath = "[本地文件路径]";
                      byte[] imgData = FileUtil.readFileByBytes(filePath);
                      String imgStr = Base64Util.encode(imgData);
                      String imgParam = URLEncoder.encode(imgStr, "UTF-8");
          
                      String param = "image=" + imgParam + "&brief=" + "{'name': 'abc'}";
          
                      // 注意这里仅为了简化编码每一次请求都去获取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) {
                  LogoAdd.logoAdd();
              }
          }
          # encoding:utf-8
          
          import requests
          import base64
          
          '''
          logo商标识别—入库
          '''
          
          request_url = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add"
          # 二进制方式打开图片文件
          f = open('[本地文件]', 'rb')
          img = base64.b64encode(f.read())
          
          params = {"brief":"{\"name\": \"abc\"}","image":img}
          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/realtime_search/v1/logo/add";
          static std::string logoAdd_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格式
              logoAdd_result = std::string((char *) ptr, size * nmemb);
              return size * nmemb;
          }
          /**
           * logo商标识别—入库
           * @return 调用成功返回0,发生错误返回其他错误码
           */
          int logoAdd(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, "brief", CURLFORM_COPYCONTENTS, "{'name': 'abc'}", 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 = logoAdd_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 LogoAdd
              {
                  // logo商标识别—入库
                  public static string logoAdd()
                  {
                      string token = "[调用鉴权接口获取的token]";
                      string host = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/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 = "image=" + HttpUtility.UrlEncode(base64) + "&brief=" + "{'name': 'abc'}";
                      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("logo商标识别—入库:");
                      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
          {
            "log_id": 972934653,
            "cont_sign": "217113248,419422301"
          }

          logo识别—删除

          使用删除接口请先在控制台创建应用并申请建库,建库成功后先调用入库接口完成logo图片入库,删除接口用户在已入库的logo图片中删除图片。

          请求示例

          HTTP 方法:POST

          请求URL: https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/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二选一)。要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式. 注意:图片需要base64编码、去掉编码头后再进行urlencode。
          cont_sign string - 图片签名(和image二选一,image优先级更高)

          请求代码示例

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

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

          curl -i -k 'https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/delete?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】' -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/realtime_search/v1/logo/delete?access_token=' . $token;
          $img = file_get_contents('[本地文件路径]');
          $img = base64_encode($img);
          $bodys = array(
              'image' => $img
          );
          $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;
          
          /**
          * logo商标识别—删除
          */
          public class LogoDelete {
          
              /**
              * 重要提示代码中所需工具类
              * 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 logoDelete() {
                  // 请求url
                  String url = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/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;
          
                      // 注意这里仅为了简化编码每一次请求都去获取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) {
                  LogoDelete.logoDelete();
              }
          }
          # encoding:utf-8
          
          import requests
          import base64
          
          '''
          logo商标识别—删除
          '''
          
          request_url = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/delete"
          # 二进制方式打开图片文件
          f = open('[本地文件]', 'rb')
          img = base64.b64encode(f.read())
          
          params = {"image":img}
          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/realtime_search/v1/logo/delete";
          static std::string logoDelete_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格式
              logoDelete_result = std::string((char *) ptr, size * nmemb);
              return size * nmemb;
          }
          /**
           * logo商标识别—删除
           * @return 调用成功返回0,发生错误返回其他错误码
           */
          int logoDelete(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_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 = logoDelete_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 LogoDelete
              {
                  // logo商标识别—删除
                  public static string logoDelete()
                  {
                      string token = "[调用鉴权接口获取的token]";
                      string host = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/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);
                      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("logo商标识别—删除:");
                      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": 50488417
          }
          上一篇
          植物识别
          下一篇
          果蔬识别