0

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

这是我的配置文件:

  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>
4

2 回答 2

0

您需要下载 eng.cube.*数据文件并放入tessdata文件夹。

TESSDATA_PATH_FOLDER应设置为D:/compuwork/ambienti/workspace_mars/ocrmaven/

于 2016-03-01T00:56:36.133 回答
0

非常感谢 nguyenq!

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

它与这个 myconfig 一起工作:

    tessedit_ocr_engine_mode 1
    user_words_suffix    user-words
    user_patterns_suffix    user-patterns

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

相反,请尝试使用 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

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

    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)    
于 2016-03-01T10:47:52.733 回答