Tess-Two(Android中的Tesseract OCR)显示非常不准确的结果

时间:2016-11-09 09:51:47

标签: android ocr tesseract tess-two

我使用以下功能使用Tesseract OCR的Android fork Tess-Two执行离线OCR:

private String startOCR(Uri imgUri) {
    try {
        ExifInterface exif = new ExifInterface(imgUri.getPath());
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        switch(exifOrientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
        }
        Log.d(TAG, "Rotation: " + rotate);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4; // 1 - means max size. 4 - means maxsize/4 size. Don't use value <4, because you need more memory in the heap to store your data.
        // set to 300 dpi
        options.inTargetDensity = 300;
        Bitmap bitmap = BitmapFactory.decodeFile(imgUri.getPath(), options);

        // Change Orientation via EXIF
        if (rotate != 0) {

            // Getting width & height of the given image.
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);

        }

        // To Grayscale
        bitmap = toGrayscale(bitmap);

        final Bitmap b = bitmap;

        final ImageView ivResult = (ImageView)findViewById(R.id.ivResult);
        if(ivResult != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ivResult.setImageBitmap(b);
                }
            });

        }
        return extractText(bitmap);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        return "";
    }
}

这是extractText()方法:

private String extractText(Bitmap bitmap) {
    //Log.d(TAG, "extractText");
    try {
        tessBaseApi = new TessBaseAPI();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        if (tessBaseApi == null) {
            Log.e(TAG, "TessBaseAPI is null. TessFactory not returning tess object.");
        }
    }

    tessBaseApi.init(DATA_PATH, lang);

    //EXTRA SETTINGS
    tessBaseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, "abcdefghijklmnopqrstuvwxyz1234567890',.?;/ ");

    Log.d(TAG, "Training file loaded");
    tessBaseApi.setDebug(true);
    tessBaseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_OSD);
    tessBaseApi.setImage(bitmap);
    String extractedText = "empty result";
    try {
        extractedText = tessBaseApi.getUTF8Text();
    } catch (Exception e) {
        Log.e(TAG, "Error in recognizing text.");
    }
    tessBaseApi.end();
    return extractedText;
}

extractText()返回的值显示在以下屏幕截图中:

inaccurate result

虽然我制作了灰度图像和图像,但精度非常低。在执行OCR之前升级到300 dpi。如何改善结果?受过训练的数据不够好吗?

2 个答案:

答案 0 :(得分:4)

我已经做了一些测试,但是,我有一些观点和结论可以改善你的结果。

  1. 尝试在VAR_WHITE_CHARLIST变量参数中传递小写和大写字母:
  2. 请参阅我的搜索结果:

    enter image description here

    a)仅限小写:

    参数:

    baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, "abcdefghijklmnopqrstuvwxyz1234567890',.?;/ ");
    

    结果:

      

    05 atenienses nnito,hdeleto e laicao,os principais acusadores de   gocrates,nao defendiam apenas que o filosofo corrompia a juventude;   eles lutavam tama bern pelas virtudes da tradigao poetica vinculada a   liornero。 nristofanes,um dos responsaveis,segundo socrates,dos   preconceitos contra o filosofo,era outro grande defensor dessa   virtude。

         socrates,de certa forma,estava em guerra com a tradieao poetica   grega。 0 metodo de socrates era o oposto a narrativa epica de   tlornero。 sua dialetica nao tinha nada de semideuses玉米superpoderes   6

    b)大写和小写字母:

    参数:

    baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1234567890',.?;/ ");
    

    结果:

      

    Os atenienses Anito,Meleto e Licao,os principais acusadores de   苏格拉底,nao defendiam apenas que o filosofo corrompia a juventude;   eles lutavam tama bern pelas virtudes da tradigao poetica vinculada a   奥梅罗。 Aristofanes,um dos responsaveis,segundo socrates,dos   preconceitos contra o filosofo,era outro grande defensor dessa   virtude。

         socrates,de certa forma,estava em guerra com a tradieao poetica   grega。 O metodo de socrates era o Oposto a narrativa epica de Homero。   Sua dialetica nao tinha nada de semideuses玉米superpoderes 6

    PS:我使用葡萄牙语运行了这个例子,检查一下需要不同的字符,例如:'éóç'它不起作用,因为它没有作为char传递到白名单。< / p>

    我也尝试使用你的照片,结果有所改善(不是那么多):

      

    字体20;哪个polrlrcran对Ihe曲线进行了抨击,总结了一个增长   心情。在一个邪恶的演讲中? '你的铁工业已经死了。死了   munon。你的煤炭yum mono在铁Vbur Ilk玛丽是和。 Ø   你的羊毛工业是为什么。你的经典Wilding先生。 blmailf

    所以我检查了tesseract如何将图像二值化:

    Theresholded Image

    你的图像有太大的噪音,然后api尝试将你的图像二值化,使你的图片的很大一部分难以辨认。我建议你再试一次,但没有通过灰度,并尝试研究如何减少图像中的噪音。

    为了帮助您完成调试任务,您可以保存阈值图像:

    WriteFile.writeBitmap(baseApi.getThresholdedImage())
    

    我希望它对你有用!感谢您分享您的问题!

    Abraços!

答案 1 :(得分:0)

在这一行 options.inSampleSize = 4; 将数字从4更改为1并尝试再次执行ocr