无意间了解到图像识别,然后就自然而然的找到了百度AI开发平台。这里只用了OCR图片转文字技术和图像识物
官网地址:http://ai.baidu.com/docs#/OCR-API/top
上面还有各类识别,没太多时间钻研就搞了这2个。
说明:1.测试项目使用SpringBoot搭建,Maven配置文件:
<!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.11.3</version>
</dependency>
2.测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class PicReadApplicationTests {
private final String APP_ID = "xxxxx";
private final String API_KEY = "xxxxxxx";
private final String SECRET_KEY = "xxxxxx";
@Test
public void testPicToStr() throws JSONException {
String path = "C:\\Users\\user\\Desktop\\test pic\\a2.png";
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
options.put("language_type", "ENG");//语言是英文
options.put("detect_direction", "true");// 是否检测图像朝向,默认不检测
options.put("detect_language", "true");//是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语)
options.put("probability", "true");// 是否返回识别结果中每一行的置信度
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
JSONObject res = client.basicGeneral(path, options);
System.out.println(res.toString(2));
}
/**
* 高精度图片转文字
* @throws JSONException
*/
@Test
public void testPicToStrHighPrevision() throws JSONException {
String path = "C:\\Users\\user\\Desktop\\test pic\\a2.png";
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("detect_direction", "true");
paramMap.put("probability", "true");
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
JSONObject res = client.basicAccurateGeneral(path, paramMap);
System.out.println(res.toString(2));
}
/**
* 识别图片中事物
*/
@Test
public void testPic() throws JSONException {
// 初始化一个AipImageClassify
AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
String path = "C:\\Users\\user\\Desktop\\test pic\\read.png";
HashMap<String, String> paramMap = new HashMap<String, String>();
paramMap.put("baike_num", "5");//显示五条
JSONObject jsonObject = client.redwine(path, paramMap);
System.out.println(jsonObject.toString(2));//缩进2位
}
}