[图像识别] java语言使用tess4j识别图片中的文字

之所以写下这篇文章,是因为在实践中使用时,发现在识别整张图时,准确率降低。尤其是中文,英文,字符,数字,水印都有的时候。出错率很高。原先一直认为是水印的问题。结果将图片二值化以后,没有水印,识别准确度依旧很低。
偶然的情况,使用局部区域识别,发现,效果很好,准确度大大提高。
然后体用api切换中文,英文识别库,很大程度的提高了准确率。

maven依赖

https://github.com/nguyenq/tess4j

  <dependency>
      <groupId>net.sourceforge.tess4j</groupId>
      <artifactId>tess4j</artifactId>
      <version>4.5.4</version>
  </dependency>

代码示例

代码示例,工程中,必须要有tessdata目录,存放语言库,如eng.traineddata,chi_sim.traineddata等。

public class TesseractExample {

    public static void main(String[] args) {
        File imageFile = new File("eurotext.tif");
        ITesseract instance = new Tesseract();  // JNA Interface Mapping
        // ITesseract instance = new Tesseract1(); // JNA Direct Mapping

        try {
            String result = instance.doOCR(imageFile);
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
    }
}

工程目录结构

这里写图片描述

设置目录。为什么要这么做呢?因为jar包中已经有个英文库了,所以我们只需要再添加其它需要的库就行,放在classpath下就ok。不需要在外层多一层了。

File imageFile = new File("C:\\random.png");
Tesseract instance = Tesseract.getInstance();

//In case you don't have your own tessdata, let it also be extracted for you
//这样就能使用classpath目录下的训练库了
File tessDataFolder = LoadLibs.extractTessResources("tessdata");

instance.setLanguage("eng");//英文库识别数字比较准确

//Set the tessdata path
instance.setDatapath(tessDataFolder.getAbsolutePath());

try {
    String result = instance.doOCR(imageFile);
    System.out.println(result);
} catch (TesseractException e) {
    System.err.println(e.getMessage());
}

目录结构
这里写图片描述

当然也可以使用中文库

instance.setLanguage("chi_sim");//中文库识别中文

也可以只识别图片中的某一部分,实践发现,识别小区域,准确度会变高。

instance.setLanguage("eng");//英文库识别数字比较准确
//提取金额
Rectangle rectangle = new Rectangle(366, y_Axis, 92, h);
String decrease = instance.doOCR(bufferedImage,rectangle)
        .replace(" ",".").replace(",","");

网络图片

 String url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F365jia.cn%2Fuploads%2Fnews%2Ffolder_1659049%2Fimages%2Fbe826380357baa44d81250e09036a92e.jpg&refer=http%3A%2F%2F365jia.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1612690508&t=f12c7c702f72d6113639cd3e937a7718";

BufferedImage img = ImageIO.read(new URL(url));
String result = instance.doOCR(img);

去掉水印

图片二值化,水印也可以被去掉。

BufferedImage grayImage = ImageHelper.convertImageToBinary(ImageIO.read(imageFile));
ImageIO.write(grayImage, "jpg", new File("data/", "test2.jpg"));

tess4j 和tesseract版本对应关系

  • 0.4 (Tesseract 2.04)
  • 1.0 (Tesseract 3.02)
  • 1.1 (Tesseract 3.02)
  • 1.2 (Tesseract 3.02)
  • 1.4 (Tesseract 3.02)
  • 1.5 (Tesseract 3.02)
  • 2.0 (Tesseract 3.03RC)
  • 3.0 (Tesseract 3.04)
  • 3.3 (Tesseract 3.05)
  • 3.4 (Tesseract 3.05.01)
  • 4.0 (Tesseract 4.0.0-beta.1)
  • 4.1 (Tesseract 4.0.0-beta.3)
  • 4.3 (Tesseract 4.0.0)
  • 4.4 (Tesseract 4.1.0)

安装tesseract

参考 https://tesseract-ocr.github.io/tessdoc/Installation.html
mac

brew install tesseract

For CentOS 8 run the following as root:

dnf config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/CentOS_8/
rpm --import https://build.opensuse.org/projects/home:Alexander_Pozdnyakov/public_key
dnf install tesseract
dnf install tesseract-langpack-deu

参考

  • http://tess4j.sourceforge.net/codesample.html
  • https://github.com/nguyenq/tess4j/issues/9
    tessdata来源
  • https://github.com/tesseract-ocr/tessdata/tree/3.04.00
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值