0

I downloaded tess-two for testing car plate number recognition, but its accurate as the alphabet characters so downloaded trained digit files from here. The problem I could not find a way to load it, and I did not find a tutorial about, would kindly help me, please.

Thanks,

Marvix
  • 179
  • 3
  • 16

1 Answers1

0

I found the answer, the following java file code:

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

and in the main activity you need to get the image as bitmap, then send it to ocr with String temp = tess.getResults(imgae.jpg)

Marvix
  • 179
  • 3
  • 16