百度AI图片识别亲测成功详解
百度AI圖片識別親測成功詳解
1、瀏覽器輸入網(wǎng)址http://ai.baidu.com/或者百度一下搜索‘百度ai’點(diǎn)擊第一個(gè)。
2、點(diǎn)擊控制臺,有百度賬號的直接登錄,沒有的注冊后登錄。
3、點(diǎn)擊左側(cè)菜單“文字識別”—>點(diǎn)擊“應(yīng)用列表”—>點(diǎn)擊“創(chuàng)建應(yīng)用”—>填寫相關(guān)信息后—>點(diǎn)“立即創(chuàng)建”—>獲取“AppID、API Key、Secret Key”。
4、SDK下載,下載解壓后將相應(yīng)jar包添加到項(xiàng)目中;
5、點(diǎn)擊“使用說明”查看幫助文檔https://ai.baidu.com/ai-doc/OCR/1k3h7y3db
6、直接上代碼,將如下代碼直接復(fù)制到項(xiàng)目中運(yùn)行看效果(注意需將第三點(diǎn)中獲取的“AppID、API Key、Secret Key”填入代碼中),根據(jù)項(xiàng)目實(shí)際情況結(jié)合幫助文檔再進(jìn)行調(diào)整即可。
?
import java.util.HashMap;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import com.baidu.aip.ocr.AipOcr;
@Component
public class Sample{
? ?// 設(shè)置APPID/AK/SK
? ?public static final String APP_ID = "第三點(diǎn)中獲取的AppID";
? ?public static final String API_KEY = "第三點(diǎn)中獲取的API Key";
? ?public static final String SECRET_KEY = "第三點(diǎn)中獲取的Secret Key";
? ?public static void main(String[] arg){
? ? ? ?JSONObject res = sample();
? ? ? ?System.out.println(res.toString(2));
? ?}
? ?// 初始化用戶對象
? ?public static AipOcr init() {
? ? ? ?// 初始化一個(gè)AipOcr
? ? ? ?AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
? ? ? ?// 可選:設(shè)置網(wǎng)絡(luò)連接參數(shù)
? ? ? ?client.setConnectionTimeoutInMillis(2000);
? ? ? ?client.setSocketTimeoutInMillis(60000);
? ? ? ?return client;
? ?}
? ?public static JSONObject sample() {
? ? ? ?// 傳入可選參數(shù)調(diào)用接口
? ? ? ?HashMap<String, String> options = new HashMap<String, String>();
? ? ? ?options.put("language_type", "CHN_ENG");
? ? ? ?options.put("detect_direction", "true");
? ? ? ?options.put("detect_language", "true");
? ? ? ?options.put("probability", "true");
? ? ? ?//初始化用戶對象
? ? ? ?AipOcr client = init();
? ? ? ?// 參數(shù)為本地圖片路徑
? ? ? ?String path = "F:\\揣搜網(wǎng).png";
? ? ? ?JSONObject res = client.basicGeneral(path, options);
? ? ? ?// 參數(shù)為本地圖片二進(jìn)制數(shù)組
? ? ? ?//String image = "F:\\揣搜網(wǎng).png";
? ? ? ?//File file = new File(image);
? ? ? ?//byte[] fileByte = new byte[0];
? ? ? ?//try {
? ? ? ?// ? ?fileByte = Files.readAllBytes(file.toPath());
? ? ? ?//} catch (IOException e) {
? ? ? ?// ? ?e.printStackTrace();
? ? ? ?//}
? ? ? ?//JSONObject res = client.basicGeneral(fileByte, options);
? ? ? ?// 通用文字識別, 圖片參數(shù)為遠(yuǎn)程url圖片
? ? ? ?//String url = "https://timgsa.baidu.com/timg?" +
// ? ? ? ?"image&quality=80&size=b9999_10000&sec=1600324362220&di=ffc9820b80235fcfa42c3c91b696ead4" +
// ? ? ? ?"&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%" +
// ? ? ? ?"2Fuploads%2Fitem%2F201512%2F29%2F20151229183756_SxVkw.jpeg";
? ? ? ?//JSONObject res = client.basicGeneralUrl(url, options);
? ? ? ?return res;
? ?}
}
?
7、識別效果。(注意免費(fèi)是有次數(shù)限制的具體情況請查看“購買指南”)
?
轉(zhuǎn)載至:http://www.chuaisou.com/index.html#/app/itw?rt=1&pn=I160031022Q521738
?
總結(jié)
以上是生活随笔為你收集整理的百度AI图片识别亲测成功详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS唤起H5微信或支付宝收银台
- 下一篇: 《蛋仔派对》之玩转‘创意工坊’