仅限Tess4j OcrEngineMode CUBE:内存访问无效

时间:2016-02-29 11:45:25

标签: java tesseract tess4j

我想要扫描图像(tif图像)来获取数字。当Tess4J设置为默认引擎时,它经常会混淆6为5,0为9 ...我想尝试使用CUBE ONLY引擎。

这是myconfig文件:

  tessedit_ocr_engine_mode 2
  load_system_dawg F
  load_freq_dawg F
  load_punc_dawg F
  load_number_dawg F
  load_unambig_dawg F
  load_bigram_dawg F
  load_fixed_length_dawgs F
  user_words_suffix    user-words
  user_patterns_suffix    user-patterns

这是我的Java代码

   public class App {

    public static final String NUMBERS = "Oo0123456789";
        public static final String TESSDATA_PATH_FOLDER = "D:/compuwork/ambienti/workspace_mars/ocrmaven/tessdata";



    private static final Logger logger = LoggerFactory.getLogger(new LoggHelper().toString());

    Tesseract1 instance;

        String nomeCartella="005";
        String path = "D:\\Documenti\\OCR\\scansioni\\"+nomeCartella;
        String resultPath = "D:\\Documenti\\OCR\\RISULTATI\\"+nomeCartella;
        String resultCorrettiPath = resultPath+"\\"+"corretti";
        String resultErratiPath = resultPath+"\\"+"errati";
        String tmpPath = resultPath+"\\tmpImmagine";
        String anotherCopy = resultPath+"\\"+"ad";
        String preScanPath = resultPath+"\\prescan";

        int validi = 0;
        int nonValidi = 0;

    public static void main( String[] args ) {

        try {    

            //write my dictionary
            File fileDir = new File(TESSDATA_PATH_FOLDER+"\\"+"eng.user-words");
            Writer out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(fileDir), "UTF8"));

            for(int i=120000; i<200000;i++) {
                out.append(""+i).append("\r\n");
            }
            out.flush();
            out.close();

            //write my pattern
            fileDir = new File(TESSDATA_PATH_FOLDER+"\\"+"eng.user-patterns");
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileDir), "UTF8"));
            out.append("\\d\\d\\d\\d\\d\\d");
            out.flush();
            out.close();

            new App().testTesseractGlobalV();
            System.exit(0);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    public App() {
        instance = new Tesseract1(); 
        instance.setLanguage("eng");
        instance.setDatapath(TESSDATA_PATH_FOLDER);
        instance.setPageSegMode(TessPageSegMode.PSM_AUTO);
        instance.setTessVariable("tessedit_char_blacklist", "èéìà§ùòç$£&%éÎÉÈ");
        instance.setTessVariable("file_type", ".tiff");
        List<String> configs = Arrays.asList("myconfig");
        instance.setConfigs(configs);
    }



    public void testTesseractGlobalV() {    

        File samples = new File(path);    

        //my Verify Result
        Verificator verSerieV = new Verificator();       

        File outputFile = null;
        BufferedImage bi = null;

        int imgCount = 0;
        for (File imageFile : samples.listFiles()) {
            System.out.println("******* IMG "+imgCount+++" ******");

            try {                        
                 bi = ImageIO.read(imageFile);
                 verSerieV.setRegionScan(new Rectangle(verSerieV.getRegionScan().x,verSerieV.getRegionScan().y,(int)(bi.getWidth() - verSerieV.getRegionScan().x), verSerieV.getRegionScan().height));
                 bi = ImageHelper.getSubImage(bi, verSerieV.getRegionScan().x, verSerieV.getRegionScan().y, verSerieV.getRegionScan().width, verSerieV.getRegionScan().height);


                 Binirization binarization = new Binirization(bi);
                 binarization.DoBinirization();
                 BufferedImage nuovaTest = binarization.getImg();
                 String nameFile = imageFile.getName();
                 File mFile = new File(preScanPath+"\\"+nameFile);
                 ImageIO.write(nuovaTest,"tif", mFile);      
                 System.out.println("scanning "+nameFile);

                 String result = instance.doOCR(nuovaTest); //throw java.lang.Error

                 ...

这是完整的错误消息

Exception in thread "main" java.lang.Error: Invalid memory access
    at net.sourceforge.tess4j.TessAPI1.TessBaseAPIInit1(Native Method)
    at net.sourceforge.tess4j.Tesseract1.init(Tesseract1.java:338)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:247)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:231)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:212)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:196)
    at tess4j.example.App.testTesseractGlobalV(App.java:158)
    at tess4j.example.App.main(App.java:97)
init_cube_objects(true, &tessdata_manager):Error:Assert failed:in file ..\..\ccmain\tessedit.cpp, line 209

我正在使用eclipse,maven项目:

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

2 个答案:

答案 0 :(得分:0)

您需要download line = reader.ReadLine(); ki = int.Parse(line); line = reader.ReadLine(); kj = int.Parse(line); 个数据文件并放入eng.cube.*文件夹。

tessdata应设置为TESSDATA_PATH_FOLDER

答案 1 :(得分:0)

非常感谢nguyenq!

我修复了我的TESSDATA_PATH_FOLDER并从您的链接中下载了一些丢失的文件 eng.cube.* ...

它使用了这个myconfig:

    tessedit_ocr_engine_mode 1
    user_words_suffix    user-words
    user_patterns_suffix    user-patterns

并从App()构造函数中删除instance.setPageSegMode(TessPageSegMode.PSM_AUTO); ...

相反,请尝试使用myconfig文件:

  tessedit_ocr_engine_mode 1
  load_system_dawg F
  load_freq_dawg F
  load_punc_dawg F
  load_number_dawg F
  load_unambig_dawg F
  load_bigram_dawg F
  load_fixed_length_dawgs F
  user_words_suffix    user-words
  user_patterns_suffix    user-patterns

并从App()构造函数中删除instance.setPageSegMode(TessPageSegMode.PSM_AUTO) ,我发现了以下异常:

    Exception in thread "main" java.lang.Error: Invalid memory access
    at net.sourceforge.tess4j.TessAPI1.TessBaseAPIGetUTF8Text(Native Method)
    at net.sourceforge.tess4j.Tesseract1.getOCRText(Tesseract1.java:402)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:258)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:231)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:212)
    at net.sourceforge.tess4j.Tesseract1.doOCR(Tesseract1.java:196)
    at tess4j.example.TestP.verify(TestP.java:96)
    at tess4j.example.App.main(App.java:77)