1.注册百度账号
进入百度开发者中心-百度智能云,直接按图操作,领取免费资源、创建应用。少说话,开撸
百度开发者中心-汇聚、开放、助力、共赢百度开发者中心是一个面向开发者的知识分享平台,专注于为开发者打造一个有温度的技术交流社区,开发者通过平台来分享知识、相互交流。https://developer.baidu.com/
车牌识别_车牌识别系统_车牌OCR识别-百度AI开放平台百度AI车牌识别,毫秒级响应,对中国大陆机动车车牌号码进行识别,可同时识别多张车牌,满足汽车场景应用,注册即享每天200次免费调用额,支持云端调用和私有化部署https://ai.baidu.com/tech/ocr_cars/plate
2.查看文档
地址:https://ai.baidu.com/ai-doc/OCR/Ek3h7xypmsss
首先先看调用方式获取Token,以下以车牌号为例:
package com.baidu.ai.aip.auth;import org.json.JSONObject;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;/*** 获取token类*/
public class AuthService {/*** 获取权限token* @return 返回示例:* {* "access_token": "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",* "expires_in": 2592000* }*/public static String getAuth() {// 官网获取的 API Key 更新为你注册的String clientId = "百度云应用的AK";// 官网获取的 Secret Key 更新为你注册的String clientSecret = "百度云应用的SK";return getAuth(clientId, clientSecret);}/*** 获取API访问token* 该token有一定的有效期,需要自行管理,当失效时需重新获取.* @param ak - 百度云官网获取的 API Key* @param sk - 百度云官网获取的 Securet Key* @return assess_token 示例:* "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"*/public static String getAuth(String ak, String sk) {// 获取token地址String authHost = "https://aip.baidubce.com/oauth/2.0/token?";String getAccessTokenUrl = authHost// 1. grant_type为固定参数+ "grant_type=client_credentials"// 2. 官网获取的 API Key+ "&client_id=" + ak// 3. 官网获取的 Secret Key+ "&client_secret=" + sk;try {URL realUrl = new URL(getAccessTokenUrl);// 打开和URL之间的连接HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();connection.setRequestMethod("GET");connection.connect();// 获取所有响应头字段Map<String, List<String>> map = connection.getHeaderFields();// 遍历所有的响应头字段for (String key : map.keySet()) {System.err.println(key + "--->" + map.get(key));}// 定义 BufferedReader输入流来读取URL的响应BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String result = "";String line;while ((line = in.readLine()) != null) {result += line;}/*** 返回结果示例*/System.err.println("result:" + result);JSONObject jsonObject = new JSONObject(result);String access_token = jsonObject.getString("access_token");return access_token;} catch (Exception e) {System.err.printf("获取token失败!");e.printStackTrace(System.err);}return null;}}
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 LicensePlate {/*** 重要提示代码中所需工具类* 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 licensePlate() {// 请求urlString url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";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) {LicensePlate.licensePlate();}
}
或者下面是网络图片识别
public static String getCarNo(String filePath) {// 请求urlString url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";try {log.info("getCarNo调用车牌号接口的图片路径:" + filePath);byte[] imgData = getFileStream(filePath);String imgStr = Base64Util.encode(imgData);String imgParam = URLEncoder.encode(imgStr, "UTF-8");String param = "image=" + imgParam;// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,// 客户端可自行缓存,过期后重新获取。String accessToken = getAuth();String result = HttpUtil.post(url, accessToken, param);System.out.println(result);log.info("getCarNo调用车牌号接口的结果:" + result);Map<String,Object> responseMapMap = JSON.parseObject(result, Map.class);Map words_resultMap = (Map) responseMapMap.get("words_result");if (words_resultMap.isEmpty()) {return null;}String carNo = (String) words_resultMap.get("number");return carNo;} catch (Exception e) {e.printStackTrace();}return null;}/*** 得到文件流* * @param url* 图片地址* @return*/public static byte[] getFileStream(String url) {try {URL httpUrl = new URL(url);HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);// 通过输入流获取图片数据InputStream inStream = conn.getInputStream();byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据return btImg;} catch (Exception e) {e.printStackTrace();}return null;}/*** 从输入流中获取数据* * @param inStream* 输入流* @return* @throws Exception*/public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}inStream.close();return outStream.toByteArray();}
用到的工具类,具体对应的类可以在代码中的链接中下载.
package com.baidu.ai.aip.utils;import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;/*** http 工具类*/
public class HttpUtil {public static String post(String requestUrl, String accessToken, String params)throws Exception {String contentType = "application/x-www-form-urlencoded";return HttpUtil.post(requestUrl, accessToken, contentType, params);}public static String post(String requestUrl, String accessToken, String contentType, String params)throws Exception {String encoding = "UTF-8";if (requestUrl.contains("nlp")) {encoding = "GBK";}return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);}public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)throws Exception {String url = requestUrl + "?access_token=" + accessToken;return HttpUtil.postGeneralUrl(url, contentType, params, encoding);}public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)throws Exception {URL url = new URL(generalUrl);// 打开和URL之间的连接HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");// 设置通用的请求属性connection.setRequestProperty("Content-Type", contentType);connection.setRequestProperty("Connection", "Keep-Alive");connection.setUseCaches(false);connection.setDoOutput(true);connection.setDoInput(true);// 得到请求的输出流对象DataOutputStream out = new DataOutputStream(connection.getOutputStream());out.write(params.getBytes(encoding));out.flush();out.close();// 建立实际的连接connection.connect();// 获取所有响应头字段Map<String, List<String>> headers = connection.getHeaderFields();// 遍历所有的响应头字段for (String key : headers.keySet()) {System.err.println(key + "--->" + headers.get(key));}// 定义 BufferedReader输入流来读取URL的响应BufferedReader in = null;in = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));String result = "";String getLine;while ((getLine = in.readLine()) != null) {result += getLine;}in.close();System.err.println("result:" + result);return result;}
}
打完收工。