赞
踩
合合TextIn产品有着大量的功能,包括但不限于文字识别,表格识别,ppt内容提取,文档格式转化等.
在这里我主要用到的是通用文字识别,先注册一个账号,购买对应的使用次数,在工作台里就可以看到公有API.
在账号管理里可以看到app_id和secret_code,这是调用接口的凭证.
在对应api的文档里有着详细的使用说明,以及不同语言的示例代码帮助你快速上手,这里我使用的是java的Springboot框架.
我在根目录下创建了一个配置文件,用来存储appid等信息,这样之后有变化可以方便更改.
新建了一个PropManager的工具类用来打开配置文件.
- public class PropManager {
- public static Properties getOCRProp(){
- Properties prop = new Properties();
- try{
- prop.load(new FileInputStream("ocrstudy.properties"));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return prop;
- }
- }
因为内容不多,我直接在Controller层完成代码.
这段函数中,传入了多张图片,实现了读取所有图片文字内容并拼接的功能.
先读取配置文件中的url和appid和secretcode,url是api的请求连接,在TextIn的文档中找对应的,通用文字识别url是https://api.textin.com/ai/service/v2/recognize
- public String ocr(List<MultipartFile> imgs) {
-
- Properties prop = PropManager.getOCRProp();
- String url = prop.getProperty("ocr.url");
- String appId = prop.getProperty("ocr.appId");
- String secretCode = prop.getProperty("ocr.secretCode");
- String res = "";
- for (MultipartFile img : imgs
- ) {
- BufferedReader in = null;
- DataOutputStream out = null;
- String result = "";
- StringBuilder allTextContent = new StringBuilder();
- try {
- byte[] imgData = img.getBytes();
- URL realUrl = new URL(url);
- HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("Content-Type", "application/octet-stream");
- conn.setRequestProperty("x-ti-app-id", appId);
- conn.setRequestProperty("x-ti-secret-code", secretCode);
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setRequestMethod("POST"); // 设置请求方式
- out = new DataOutputStream(conn.getOutputStream());
- out.write(imgData);
- out.flush();
- out.close();
- in = new BufferedReader(
- new InputStreamReader(conn.getInputStream(), "UTF-8"));
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
-
- JSONObject jsonResponse = JSONObject.parseObject(result);
- JSONArray linesArray = jsonResponse.getJSONObject("result").getJSONArray("lines");
- for (int i = 0; i < linesArray.size(); i++) {
- String text = linesArray.getJSONObject(i).getString("text");
- allTextContent.append(text + "\n");
- }
- } catch (Exception e) {
- e.printStackTrace();
- return "发送 POST 请求出现异常!" + e;
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- if (in != null) {
- in.close();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- res+=allTextContent.toString();
- }
-
- return res;
- }

代码中,使用HttpUrlConnection类进行api的请求,设置好对应的请求参数,并将图片以字节的形式传输过去,然后读取所有的返回内容.
他的返回是一个json格式,具体的格式可以看TextIn的文档,这里我们接受到后是一个字符串,里面内容很多,而我们只需要其中的关键,所以使用fastjson提供的类将得到的字符串以json格式进行处理,先拿到其中的result列表,然后读取其中lines全部的text中的内容,拼接起来就得到了结果.
接下来使用apifox进行接口测试,这里挑选了荷塘月色的文章图片,可以看到结果非常的成功.如果觉得识别后的格式不是很好,可以用大语言模型进行二次处理.
综合来说,TextIn的文字识别效果是非常好的,而且不需要使用额外的sdk和依赖,使用方面来说非常的简单,前端后端都可以方便的单独使用.
fastjson依赖如下:
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.47</version>
- </dependency>
JSON结构说明如下:
字段名 | 类型 | 描述 |
---|---|---|
code | integer | 错误码,详见“错误码说明” |
message | string | 错误信息 |
version | string | 接口版本号。 |
duration | number | 服务时间消耗,单位是毫秒(ms)。 |
result | object | |
+ angle | integer | 图像角度, 定义0度为人类阅读文字的图像方向,称为正置图像, 本字段表示输入图像是正置图像进行顺时针若干角度的旋转所得。
|
+ width | integer | 输入图像的宽度。 |
+ height | integer | 输入图像的高度。 |
+ lines | array | 以文本行为单位的识别结果 |
++ text | string | 识别内容字符串 |
++ score | number | 识别置信度 |
++ type | string | 文本类型,用于表示文字的形态。 当前版本下,文本类型包括:
|
++ position | array | 以长度为8的整形数组表示四边形, 语义左上角为起始点顺时针构成闭合区域。 |
++ angle | integer | 图像角度, 定义0度为人类阅读文字的图像方向,称为正置图像, 本字段表示输入图像是正置图像进行顺时针若干角度的旋转所得。
|
++ direction | integer | 文字阅读方向。
|
++ handwritten | integer | 文字是否手写所得。
|
++ char_scores | array | 字符置信度,值域范围0-1。 设置character=1时输出。 |
++ char_centers | array | 字符中心点。 设置character=1时输出。 |
++ char_positions | array | 字符四边形点坐标,以顺时针构成闭合区域。 设置character=1时输出。 |
++ char_candidates | array | 候选字数组,表示每一个字符的候选,与候选置信度配套使用。 设置character=1时输出。 |
++ char_candidates_score | array | 候选字置信度数组,表示每一个候选字符的置信度,与候选字符配套使用。 设置character=1时输出。 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。