{ "log_id": 4086212218842203806, "location_result": { "width": 447, "top": 226, "height": 209, "left": 188 }, "result": [{ "baike_info": { "baike_url": "http://baike.baidu.com/item/%E5%B8%83%E5%8A%A0%E8%BF%AAChiron/20419512", "description": "布加迪Chiron是法国跑车品牌布加迪出品的豪华超跑车。配置四涡轮增压发动机,420 公里每小时,有23种颜色的选择,售价高达260万美元。" }, "score": 0.98793351650238, "name": "布加迪Chiron", "year": "无年份信息" }, { "score": 0.0021970034576952, "name": "奥迪RS5", "year": "2011-2017" }, { "score": 0.0021096928976476, "name": "奥迪RS4", "year": "无年份信息" }, { "score": 0.0015581247862428, "name": "奥迪RS7", "year": "2014-2016" }, { "score": 0.00082337751518935, "name": "布加迪威航", "year": "2004-2015" } ], "color_result": "颜色无法识别" }
车型识别 curl -i -k 'https://aip.baidubce.com/rest/2.0/image-classify/v1/car?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】&top_num=5' -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/car?access_token=' . $token; $img = file_get_contents('[本地文件路径]'); $img = base64_encode($img); $bodys = array( 'image' => $img, 'top_num' => 5 ); $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 Car { /** * 重要提示代码中所需工具类 * 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 car() { // 请求url String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/car"; try { // 本地文件路径 String filePath = "[本地文件路径]"; byte[] imgData = FileUtil.readFileByBytes(filePath); String imgStr = Base64Util.encode(imgData); String imgParam = URLEncoder.encode(imgStr, "UTF-8"); String param = "image=" + imgParam + "&top_num=" + 5; // 注意这里仅为了简化编码每一次请求都去获取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) { Car.car(); } }
# encoding:utf-8 import base64 import urllib import urllib2 ''' 车型识别 ''' request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/car" # 二进制方式打开图片文件 f = open('[本地文件]', 'rb') img = base64.b64encode(f.read()) params = {"image":img,"top_num":5} params = urllib.urlencode(params) access_token = '[调用鉴权接口获取的token]' request_url = request_url + "?access_token=" + access_token request = urllib2.Request(url=request_url, data=params) request.add_header('Content-Type', 'application/x-www-form-urlencoded') response = urllib2.urlopen(request) content = response.read() if content: print content
#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/car"; static std::string car_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格式 car_result = std::string((char *) ptr, size * nmemb); return size * nmemb; } /** * 车型识别 * @return 调用成功返回0,发生错误返回其他错误码 */ int car(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, "top_num", CURLFORM_COPYCONTENTS, "5", 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 ", curl_easy_strerror(result_code)); is_success = 1; return is_success; } json_result = car_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 Car { // 车型识别 public static string car() { string token = "[调用鉴权接口获取的token]"; string host = "https://aip.baidubce.com/rest/2.0/image-classify/v1/car?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.KeepAlive = true; // 图片的base64编码 string base64 = FileUtils.getFileBase64("[本地图片文件]"); String str = "image=" + HttpUtility.UrlEncode(base64) + "&top_num=" + 5; 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; } } }