android - 来自tess-two库的公认文本错误

时间:2015-05-01 09:48:18

标签: android ocr tesseract text-recognition tess-two

我正在尝试使用tess-two库来识别来自imagae的文本。

这是我的代码:

load.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // recognize text
                Bitmap temp = loadJustTakenImage(); //loads taken image from sdcard
                Bitmap rotatedImage = rotateIfNeeded(temp); // rotate method i found in some tutorial
                String text1 = recognizeText(rotatedImage);
            }

        });

识别文字方法:

(tessdata文件夹在下载时带有eng.traineddata和其他文件)

private String recognizeText(Bitmap bitmap) {
        // TODO Auto-generated method stub
        TessBaseAPI baseApi = new TessBaseAPI();
        // DATA_PATH = Path to the storage
        // lang = for which the language data exists, usually "eng"
        baseApi.init(Environment.getExternalStorageDirectory().toString()
                + "/Download/", "eng");
        // Eg. baseApi.init("/mnt/sdcard/tesseract/tessdata/eng.traineddata",
        // "eng");
        baseApi.setImage(bitmap);
        String recognizedText = baseApi.getUTF8Text();
        baseApi.end();
        return recognizedText;
    }

旋转图像方法:

private Bitmap rotateIfNeeded(Bitmap bitmap) {
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(directoryPath+"/"+currentFileName+".jpg");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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;
        }

        if (rotate != 0) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

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

            // Rotating Bitmap & convert to ARGB_8888, required by tess
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
        }
        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        return bitmap;
    }

我得到的文字真是一团糟,例如

此图片:

enter image description here

我收到了这个文字:

,7‘

有时我只是得到一个空字符串。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

根据您尝试检测字符的图像类型,设置适当的页面分割模式有助于检测字符。

例如:

baseAPI.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_ONLY);

TessBaseApi.java中存在各种其他页面分段值:

/** Page segmentation mode. */
public static final class PageSegMode {
    /** Orientation and script detection only. */
    public static final int PSM_OSD_ONLY = 0;

    /** Automatic page segmentation with orientation and script detection. (OSD) */
    public static final int PSM_AUTO_OSD = 1;

    /** Fully automatic page segmentation, but no OSD, or OCR. */
    public static final int PSM_AUTO_ONLY = 2;

    /** Fully automatic page segmentation, but no OSD. */
    public static final int PSM_AUTO = 3;

    /** Assume a single column of text of variable sizes. */
    public static final int PSM_SINGLE_COLUMN = 4;

    /** Assume a single uniform block of vertically aligned text. */
    public static final int PSM_SINGLE_BLOCK_VERT_TEXT = 5;

    /** Assume a single uniform block of text. (Default.) */
    public static final int PSM_SINGLE_BLOCK = 6;

    /** Treat the image as a single text line. */
    public static final int PSM_SINGLE_LINE = 7;

    /** Treat the image as a single word. */
    public static final int PSM_SINGLE_WORD = 8;

    /** Treat the image as a single word in a circle. */
    public static final int PSM_CIRCLE_WORD = 9;

    /** Treat the image as a single character. */
    public static final int PSM_SINGLE_CHAR = 10;

    /** Find as much text as possible in no particular order. */
    public static final int PSM_SPARSE_TEXT = 11;

    /** Sparse text with orientation and script detection. */
    public static final int PSM_SPARSE_TEXT_OSD = 12;

    /** Number of enum entries. */
    public static final int PSM_COUNT = 13;
}

您可以尝试使用不同的页面分段枚举值,并查看哪种值可以获得最佳结果。

对于上面的图片,似乎将页面分段设置为“PSM_SINGLE_LINE”应该会产生您要查找的结果。