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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • drupal学习教程(待续)「建议收藏」

    drupal学习教程(待续)「建议收藏」1.drupal模块安装a.安装captcha模块–>模块–>用户贡献的模块–>b.启用captcha模块–>模块–>选择–>保存配置c.汉化captcha模块打开https://localize.drupal.org/translate/languages/zh-hans下载captcha汉化包–>配置–>翻译–>导入b.配置capt

    2022年6月12日
    28
  • Cloudsim_java源码大全

    Cloudsim_java源码大全CloudSim介绍和使用1.CloudSim简介:2.CloudSim提供了以下新的特点:(1)支持大型云计算的基础设施的建模与仿真;(2)一个自足的支持数据中心、服务代理人、调度和分配策略的平台。(3)CloudSim独特功能有:3.CloudSim源代码介绍几个核心类:4.CloudSim下载,CloudSim在IDEA中配置5.CloudSim环境配置6.CloudSim的扩展7.使用CloudSim仿真的一般步骤1.CloudSim简介:2009年4月8日,澳大利亚墨尔本

    2022年10月13日
    4
  • python中绝对值的表达式

    python中绝对值的表达式abs(x)print(abs(23))#23print(abs(-45))#45

    2022年7月6日
    19
  • Web应用的UML建模与.NET框架开发

    Web应用的UML建模与.NET框架开发

    2022年3月12日
    341
  • Oracle命名规范

    1、编写目的使用统一的命名和编码规范,使数据库命名及编码风格标准化,以便于阅读、理解和继承。2、适用范围本规范适用于公司范围内所有以ORACLE作为后台数据库的应用系统和项目开发工作。3、对象

    2021年12月25日
    47
  • 在毕设中学习02——numpy多维数组的切片,形态变化,维度交换

    在毕设中学习02——numpy多维数组的切片,形态变化,维度交换2022.5.21文章目录关于matplotlib.pyplotcv2工具python课本学习构建三维数组,并按照指定维度输出生成一组随机数,摆放为指定矩阵形式Python中range(start,stop,步长)生成指定范围,指定步长的一组数多维数组切片——过滤信息多维矩阵的维度顺序变换多维矩阵的切片多维矩阵的形态变化关于matplotlib.pyplotcv2工具两篇博客的学习文献学习python课本学习构建三维数组,并按照指定维度输出import numpy as np#a=np.

    2022年8月11日
    5

发表回复

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

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