我怎样才能在TESS-2中加载数字训练数据?

时间:2018-10-31 19:13:37

标签: android ocr tess-two

我下载了tess-two来测试车牌号识别,但是它像字母一样准确,因此从here下载了经过训练的数字文件。我找不到加载方法的问题,也没有找到相关的教程,请给我帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

我找到了答案,下面的java文件代码:

public class TessOCR {

public static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/Android/data/myOcr/";
public static final String lang = "eng";
private static final String TAG = "---------> TESS_OCR";

private AssetManager assetManager;

private TessBaseAPI mTess;

public TessOCR(AssetManager assetManager) {

    Log.i(TAG, DATA_PATH);

    this.assetManager = assetManager;

    String[] paths = new String[]{DATA_PATH, DATA_PATH + "tessdata/"};

    for (String path : paths) {
        File dir = new File(path);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
                return;
            } else {
                Log.v(TAG, "Created directory " + path + " on sdcard");
            }
        }
    }

    if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
        try {
            InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
            OutputStream out = new FileOutputStream(new File(DATA_PATH + "tessdata/", lang + ".traineddata"));

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

            Log.v(TAG, "Copied " + lang + " traineddata");
        } catch (IOException e) {
            Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
        }
    }

    mTess = new TessBaseAPI();
    mTess.setDebug(true);
    mTess.init(DATA_PATH, lang);


   }


public String getResults(Bitmap bitmap) {
    if(bitmap != null) {
        mTess.setImage(bitmap);
        String result = mTess.getUTF8Text();
        return result;
    }else{
        return "empty";
    }

}

public void onDestroy() {
    if (mTess != null)
        mTess.end();
}
}

在主要活动中,您需要将图像获取为位图,然后使用String temp = tess.getResults(imgae.jpg)将其发送到ocr