6 Star 88 Fork 44

李晋江/JavaOCR

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

JavaOCR

Java IDE License

System : Windows 10

Intellij IDEA : Ultimate 2020.3.4

Java : 1.8.0_333

MySQL : 8.0.30

1.项目前言

1.1 项目需求

最近在看外国漫画的时候,发现有些漫画没有汉化的翻译,这哪看得懂。正想把那些日语输入到有道翻译一下,发现日文不会输。正巧之前有接触关于OCR的项目,所以便想用Java做一个小工具,实现图片的文字识别功能,于是我便开始了项目准备工作。

这里我只想简单的文字识别一下,所以为了省事就只考虑本地实现图片文字识别,不去申请云账号啥的了。所以需要对去了解OCR开源项目

1.2 OCR引擎选择

OCR开源项目 简介 优点 劣势
Tesseract Tesseract 是谷歌开发并开源的图像文字识别引擎,使用python开发。 1. github上面star非常多,项目非常活跃
2. 识别的语言和文字非常多
3. 后面做背书的公司非常强(google)
1. 不是专门针对中文场景
2. 相关文档主要是英文,对于阅读和理解起来有一定困难
3. 学习成本比较高
4. 源码较多,并且部分源码是c++,学习起来难度比较大
PaddleOCR PaddleOCR 是百度开源的中文识别的ocr开源软件 1. github上面star非常多,项目非常活跃
2. 模型只针对中文进行训练
3. 后面做背书的公司非常强(baidu)
4. 相关的中文文档非常齐全
5. 识别的精确度比较高
1. 目前使用的训练模型是基于百度公司自己的PaddlePaddle框架,对于小公司来说并不主流(对比于ts或者pytorch),所使用深度学习框架为后续其他深度学习无法做很好的铺垫
2. 项目整体比较复杂,学习成本较高
EasyOCR EasyOCR 是一个用 Python 编写的 OCR 库,用于识别图像中的文字并输出为文本,支持 80 多种语言。 1. github上面的star也是比较多,但是最近不是特别活跃
2. 支持的语言也是非常多的,多达80多种
3. 识别的精确度尚可
1. 从官方的页面体验来说识别的速度较慢
2. 识别的文字种类多,学习难度较高
3. 相关的官方文档是基于英文的,学习难度较高,对于新手不太友好
chineseocr ... 1. github上面的star也是比较多
2. 专门针对中文进行学习和训练的模型
3. 相关的文档比较多,上手相对比较容易
1. 因为没有大厂和公司的背书, 所以存在一些bug
2. 对于复杂场景下的效果不佳
3. 模型都是现成的,如果要新训练模型难度比较高
chineseocr_lite ... 1. github上面的star也是比较多
2. 专门针对中文进行学习和训练的模型
3. 相关的文档比较多,上手相对比较容易
4. 比较轻量级,部署也比较方便
...
TrWebOCR ... 1. 部署简单
2. 使用简单
3. 有对应的web页面,测试方便
4. 有对应的web接口,方便调用
1. 核心模型不开源,无法进行再次学习
2. 无法进行后续训练
3. 必须要联网才能使用
4. 精度识别一般
5. 项目不是很活跃
cnocr ... 1. 使用简单
2. 文档齐全
3. 代码全部开源,可以进行修改
4. 预定义的模型较多
5. 便于学习和模型重新训练
1. 精确度不高
2. 没有对应的web界面和接口
3. 需要配合cnstd进行使用

因为我这里需要英文、日文识别,所以综合考虑,我选择了 Tessract ,因为我使用的是 Java 语言,所以我选择直接调用 Tess4J 来实现我的需求

1.3 Tess4j介绍

Tess4j 的官网:https://tess4j.sourceforge.net

Tess4j 的 Maven 仓库:https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j Tess4J是对Tesseract OCR API 的Java JNA 封装。使 java 能够通过调用 Tess4J 的 API 来使用 Tesseract OCR。支持的格式包括 TIFF、JPEG、GIF、PNG、BMP、JPEG、PDF。Tess4J是 java 直接可使用的 jar 包,而 Tesseract OCR 是支持 Tess4J 进文件文字识别的基础,Tess4J 可直接使用 Maven 方式引入。

下面的是官网的首页图,从这里我们可以获得3个重要的信息:

1.这项目是Apache2.0的开源项目;2.支持Maven依赖导入;3.支持识别的种类很多,包括常用的PNG、JPEG等

2.项目实现

2.1 项目搭建

首先是引入 Maven 依赖,我这里直接使用最新的 Tess4J 依赖

<!-- Tess4J依赖 -->
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>5.4.0</version>
</dependency>

然后就是下载 Tess4J 的所需要的语言库,我下载了 chi_sim.traineddata(中文简体)、eng.traineddata(英文)、jpn.traineddata(日文)三个语言库,存放在 resources 的 data 目录下(如下图所示,osd.traineddata 与页面分割模式有关,这里也一并下载了)

其他语言库可根据自己需求选择下载,tessdata 语言库下载地址:https://github.com/tesseract-ocr/tessdata

注意:如果你下载的语言库只有几十、几百kb,说明你下载异常了,程序启动不了的,解决办法就是连接外网下载

2.2 主要实现代码

    // 执行OCR识别
    private void execute(BufferedImage targetImage) {
        try {
            File tempFile = new File(tempImage);
            if (tempFile == null) {
                tempFile.mkdirs();
            }
            tempFile.mkdirs();
            ImageIO.write(targetImage, "jpg", tempFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file = new File(tempImage);

        ITesseract instance = new Tesseract();
        // 设置语言库位置
        instance.setDatapath("src/main/resources/data");
        // 设置语言
        instance.setLanguage(language);
        Thread thread = new Thread() {
            public void run() {
                String result = null;
                try {
                    result = instance.doOCR(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                resultArea.setText(result);
            }
        };
        ProgressBar.show(this, thread, "图片正在识别中,请稍后...", "执行结束", "取消");
    }

3.效果演示

3.1 中文识别

3.1.1 需要识别的图片

3.1.2 识别过程

3.1.3 识别结果

十 年 生 死 两 茫 茫 。 不 思 量 , 自 难 忘 。
干 里 孤 坟 , 无 处 话 凄 凉 。 纵 使 相 逢 应 不 识 , 尘 满 面 , 鬓 如 霜 。
夜 来 幽 梦 忽 还 乡 。 小 轩 窗 , 正 梳 妆 。
相 顾 无 言 , 惟 有 泪 十 行 。 料 得 年 年 肠 断 处 , 明 月 夜 , 短 松 岗 。

错了 “千里孤坟” 的 字,还有 “惟有泪千行” 的 字,

3.1.4 总结

当图片内的文字比较清晰时,中文的识别度还是挺高的,特别是图像对比度高的话对于文字识别成功影响很大;而且如果字体较为相近,则容易出错。

3.2 英文识别

3.2.1 需要识别的图片

3.2.2 识别过程

3.2.3 识别结果

Success is the sum of small efforts, repeated day in and day out.
Time waits for no one. Treasure every moment you have.
That there's some good in this world, and it's worth fighting for.

全对

3.2.4 总结

英文的识别还是简单,而且基本没有错,因为单词的形体就不会太复杂,这里的测试就一个单词没错,看来用来识别英文是没有问题的

3.3 日文识别

3.3.1 需要识别的图片

3.3.2 识别过程

3.3.3 识别结果

あ な た の 時 間 ( し じ か ん ) は 限 ( か ぎ ) ら れ て い る 。 だ か ら 他 の 誰 か の 人 生
を 生 き る な ん て 無 駄 ( む だ ) な ま ね は よ せ 。
す の も の に ベべ て 美 し さ は あ る が 、 す べ て の 者 に 見 え る わ け で は な い 。

好像也只错了一两处(不认识日文)

3.3.4 总结

从结果来说,日文的识别正确率也挺高的,看来只要中文正确率高,其他文字正确率应该都不低,毕竟复杂度摆在那

3.4 截图功能

这里有一个截图功能,可以直接截取需要识别的图片,然后进行识别。我透,这么简单的两个词都识别不出来,看来还需要好好锻炼

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Java实现OCR图片识别文字功能 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/lijinjiang01/JavaOCR.git
git@gitee.com:lijinjiang01/JavaOCR.git
lijinjiang01
JavaOCR
JavaOCR
master

搜索帮助