Tess4j maven demo[通俗易懂]

Tess4j maven demo[通俗易懂]tess4j实现文字识别Demo,下面为内容实现源码,内容仅为一个demo,demo下载地址:tess4jDemopublicclassTess4JTest{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(newLoggHelper().toString());staticfinaldo…

大家好,又见面了,我是你们的朋友全栈君。


tess4j 实现文字识别Demo,下面为内容实现源码,内容仅为一个demo,demo下载地址:tess4jDemo

public class Tess4JTest {

    private static final Logger logger = LoggerFactory.getLogger(new LoggHelper().toString());
    static final double MINIMUM_DESKEW_THRESHOLD = 0.05d;
    ITesseract instance;

    private final String datapath = "src/test/resources";
    private final String testResourcesDataPath = "src/test/resources/test-data";
    private final String testResourcesLanguagePath = "src/test/resources/tessdata";

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
        instance = new Tesseract();
        instance.setDatapath(new File(datapath).getPath());
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of doOCR method, of class Tesseract.
     * 根据图片文件进行识别
     * @throws Exception while processing image.
     */
    @Test
    public void testDoOCR_File() throws Exception {
        logger.info("doOCR on a jpg image");
        File imageFile = new File(this.testResourcesDataPath, "0099.png");
        //set language
        instance.setDatapath(testResourcesLanguagePath);
        instance.setLanguage("chi_sim");
        String result = instance.doOCR(imageFile);
        logger.info(result);
    }

    /**
     * Test of doOCR method, of class Tesseract.
     * 根据图片流进行识别
     * @throws Exception while processing image.
     */
    @Test
    public void testDoOCR_BufferedImage() throws Exception {
        logger.info("doOCR on a buffered image of a PNG");
        File imageFile = new File(this.testResourcesDataPath, "ocr.png");
        BufferedImage bi = ImageIO.read(imageFile);

        //set language
        instance.setDatapath(testResourcesLanguagePath);
        instance.setLanguage("chi_sim");

        String result = instance.doOCR(bi);
        logger.info(result);
    }

    /**
     * Test of getSegmentedRegions method, of class Tesseract.
     * 得到每一个划分区域的具体坐标
     * @throws java.lang.Exception
     */
    @Test
    public void testGetSegmentedRegions() throws Exception {
        logger.info("getSegmentedRegions at given TessPageIteratorLevel");
        File imageFile = new File(testResourcesDataPath, "ocr.png");
        BufferedImage bi = ImageIO.read(imageFile);
        int level = TessPageIteratorLevel.RIL_SYMBOL;
        logger.info("PageIteratorLevel: " + Utils.getConstantName(level, TessPageIteratorLevel.class));
        List<Rectangle> result = instance.getSegmentedRegions(bi, level);
        for (int i = 0; i < result.size(); i++) {
            Rectangle rect = result.get(i);
            logger.info(String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d", i, rect.x, rect.y, rect.width, rect.height));
        }

        assertTrue(result.size() > 0);
    }


    /**
     * Test of doOCR method, of class Tesseract.
     * 根据定义坐标范围进行识别
     * @throws Exception while processing image.
     */
    @Test
    public void testDoOCR_File_Rectangle() throws Exception {
        logger.info("doOCR on a BMP image with bounding rectangle");
        File imageFile = new File(this.testResourcesDataPath, "ocr.png");
        //设置语言库
        instance.setDatapath(testResourcesLanguagePath);
        instance.setLanguage("chi_sim");
        //划定区域
        // x,y是以左上角为原点,width和height是以xy为基础
        Rectangle rect = new Rectangle(84, 21, 15, 13);
        String result = instance.doOCR(imageFile, rect);
        logger.info(result);
    }

    /**
     * Test of createDocuments method, of class Tesseract.
     * 存储结果
     * @throws java.lang.Exception
     */
    @Test
    public void testCreateDocuments() throws Exception {
        logger.info("createDocuments for png");
        File imageFile = new File(this.testResourcesDataPath, "ocr.png");
        String outputbase = "target/test-classes/docrenderer-2";
        List<RenderedFormat> formats = new ArrayList<RenderedFormat>(Arrays.asList(RenderedFormat.HOCR, RenderedFormat.TEXT));

        //设置语言库
        instance.setDatapath(testResourcesLanguagePath);
        instance.setLanguage("chi_sim");

        instance.createDocuments(new String[]{imageFile.getPath()}, new String[]{outputbase}, formats);
    }

    /**
     * Test of getWords method, of class Tesseract.
     * 取词方法
     * @throws java.lang.Exception
     */
    @Test
    public void testGetWords() throws Exception {
        logger.info("getWords");
        File imageFile = new File(this.testResourcesDataPath, "ocr.png");

        //设置语言库
        instance.setDatapath(testResourcesLanguagePath);
        instance.setLanguage("chi_sim");

        //按照每个字取词
        int pageIteratorLevel = TessPageIteratorLevel.RIL_SYMBOL;
        logger.info("PageIteratorLevel: " + Utils.getConstantName(pageIteratorLevel, TessPageIteratorLevel.class));
        BufferedImage bi = ImageIO.read(imageFile);
        List<Word> result = instance.getWords(bi, pageIteratorLevel);

        //print the complete result
        for (Word word : result) {
            logger.info(word.toString());
        }
    }

    /**
     * Test of Invalid memory access.
     * 处理倾斜
     * @throws Exception while processing image.
     */
    @Test
    public void testDoOCR_SkewedImage() throws Exception {
        //设置语言库
        instance.setDatapath(testResourcesLanguagePath);
        instance.setLanguage("chi_sim");

        logger.info("doOCR on a skewed PNG image");
        File imageFile = new File(this.testResourcesDataPath, "ocr_skewed.jpg");
        BufferedImage bi = ImageIO.read(imageFile);
        ImageDeskew id = new ImageDeskew(bi);
        double imageSkewAngle = id.getSkewAngle(); // determine skew angle
        if ((imageSkewAngle > MINIMUM_DESKEW_THRESHOLD || imageSkewAngle < -(MINIMUM_DESKEW_THRESHOLD))) {
            bi = ImageHelper.rotateImage(bi, -imageSkewAngle); // deskew image
        }

        String result = instance.doOCR(bi);
        logger.info(result);
    }

}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130445.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于input type=“file”的及其files对象的深层探究

    关于input type=“file”的及其files对象的深层探究我们都知道,html5中有个inputtype=file元素。用该元素可以实现页面上传文件的功能但一般的做法只是简单的在表单中操作,我来研究一下深层东西想要了解它,就要知道它的内置对象,files页面上写一个input,然后选俩个图片,打印这个input对象$(“input[name=’file1′]”).change(function(e){console.log…

    2022年4月30日
    34
  • Appium – iOS Mac环境结构

    Appium – iOS Mac环境结构

    2022年1月7日
    46
  • 郑州java面试难吗_java开发人员培训机构

    郑州java面试难吗_java开发人员培训机构前言周末花了2天时间学习了额RabbitMQ,总结了最核心的知识点,带大家快速掌握RabbitMQ,整理不易希望帮忙点赞,转发,分享下,谢谢目录进入SpringBoot世界讲述Sping、SpringBoot和SpringCloud之间的关系,还重点讲述了如何利用开发工具(如IDEA)来实现开发,如何通过API文档来寻找类对象方法,告诉我们在开发过程中如何学习、发现和解决问题需要免费领取这份Alibaba架构师耗时一年整理的《SpringBoot实战,让你的开发像搭积木一样简单

    2022年9月28日
    2
  • 深度学习 — BP算法详解(误差反向传播算法)「建议收藏」

    深度学习 — BP算法详解(误差反向传播算法)「建议收藏」本节开始深度学习的第一个算法BP算法,本打算第一个算法为单层感知器,但是感觉太简单了,不懂得找本书看看就会了,这里简要的介绍一下单层感知器:图中可以看到,单层感知器很简单,其实本质上他就是线性分类器,和机器学习中的多元线性回归的表达式差不多,因此它具有多元线性回归的优点和缺点。单层感知器只能对线性问题具有很好的解决能力,但是非线性问题就无法解决了,但是多层感知器却可以解决非线性问题,多层感…

    2022年5月3日
    95
  • PyQuery库[通俗易懂]

    PyQuery库[通俗易懂]PyQuery库PyQuery库也是一个非常强大又灵活的网页解析库,PyQuery是Python仿照jQuery的严格实现。语法与jQuery几乎完全相同,所以不用再去费心去记一些奇怪的方法了。1、初始化初始化的时候一般有三种传入方式:传入字符串,传入url,传入文件字符串初始化eg1:html=”'<div><ul>…

    2022年6月11日
    34
  • Jdk下载需要登录账号解决「建议收藏」

    Jdk下载需要登录账号解决「建议收藏」大家好,目前在官网下载jdk1.8的时候需要登陆,这边分享一个账号,方便下载:账号:2696671285@qq.com密码:Oracle123

    2022年7月16日
    183

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号