{ "log_id": 2495538539661269738, "result": { "classifyByColor": "红葡萄酒/Red Wine", "subRegionCn": "梅多克", "wineNameCn": "拉图嘉利庄园红葡萄酒(正牌)", "subRegionEn": "Medoc", "regionEn": "Bordeaux", "color": "深紫红色/Dark Violet", "wineNameEn": "Chateau La Tour Carnet", "hasdetail": 1, "wineryCn": "拉图嘉利庄园", "classifyBySugar": "干型/Dry", "tasteTemperature": "16-18℃", "regionCn": "波尔多", "wineryEn": "Chateau La Tour-Carnet", "grapeCn": "", "grapeEn": "", "countryCn": "法国", "countryEn": "France", "description": "此酒充满红果和黑果味道,并带有矿物质和花香(紫罗兰,玫瑰),混合些许香草气息,单宁柔软,余香悠长。在口中留下清新的味道,香料和香草味道萦绕口中。" } }
<?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/redwine?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; /** * 细粒度图像识别—红酒识别 */ public class Redwine { /** * 重要提示代码中所需工具类 * 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 redwine() { // 请求url String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine"; 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) { Redwine.redwine(); } }