我使用以下函数使用 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()
显示在以下屏幕截图中:
精度超低,尽管我在执行 OCR 之前将图像灰度和放大到 300 dpi。我怎样才能改善结果?训练出来的数据不够好?