Android的 - 从苔丝两库识别的文字是错误的 [英] android - recognized text from tess-two library is wrong

查看:650
本文介绍了Android的 - 从苔丝两库识别的文字是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用苔丝两库从imagae识别文本。

I am trying to use the tess-two library to recognize text from imagae.

这是我的code:

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和其他文件下载)

(tessdata folder is in Download with the eng.traineddata and other files)

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;
    }

我收到文本是一个真正的混乱,例如:

此图像:

我得到了这样的文字:

,7‘

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

Sometimes I just get an empty String.

我在做什么错了?

推荐答案

根据图像上您要检测的特点,设置合适的页面分割模式将帮助检测字符的类型。

Based on the type of image on which you are trying to detect the characters, setting an appropriate Page segmentation mode will help detect the characters.

例如:

baseAPI.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_ONLY);

其他各页分割值在TessBaseApi.java present:

The various other Page segmentation values are present in 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;
}

您可以用不同的页面分割枚举值进行试验,看看它给出最好的结果。

You can experiment with different page segmentation enum values and see which gives the best result.

有关上图中,好像页面分割设置为PSM_SINGLE_LINE应该产生您正在寻找的结果。

For the above image, it seems like setting page segmentation to 'PSM_SINGLE_LINE' should yield the result you are looking for.

这篇关于Android的 - 从苔丝两库识别的文字是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆