What is tesseract in android

Использование Open Source OCR библиотеки Tesseract в Android на примере простого приложения

Сегодня я покажу, как добавить опцию распознавания текста (OCR) в Ваше Android приложение.

Наш тестовый проект — это один единственный Activity, в который я запихнула и распознавание. Итого в общем счете всего 200 строк кода.

Важная фича — опция OCR работает offline. OCR увеличивает ваш .apk приблизительно на 17mb.

Tesseract — пожалуй, самая популярная и качественная бесплатная OCR библиотека, у которой до сих пор выходят апдейты. Создатели Tesseract — разработчики из Google.

Tesseract написан на С, но существует проект tess-two — готовый инструмент по использованию Tesseract на Android платформе. Он предоставляет Java API для доступа к изначально скомпилированным Tesseract классам. Все, что нужно — это добавить tess-two в ваш build.gradle:

Кроме этого тессеракту потребуется .traineddata файл. Этот файл содержит данные для эффективного распознавания, словари слов и прочее. Файл уникален для каждого языка. Скачать .traineddata для любого языка можно по ссылке. Замечу, что есть возможность создать свой собственный .traineddata файл. Это может быть полезно, если вы распознаете специфический текст или у вас свой словарь возможных слов. Теоретически, кастомизация повысит качество распознавания.

Перед тем, как перейти к java коду, убедитесь в том, что вы положили в проект файл для английского языка eng.traideddata. Например, в src\main\assets\tessdata.

Вам потребуется сконфигурировать тессеракт, перед тем как запускать распознавание. Для этого нужно передать в метод конфигурации (init) два параметра — путь к папке tessdata на вашем Android устройстве и язык («eng»). Будьте внимательны, путь к папке tessdata, а не к .traideddata файлу, если папка будет названа иначе, код не будет работать. Очевидно, вам эту папку нужно создать на external storage и поместить в неё eng.traideddata.

Привожу метод, который из Bitmap получает распознанный текст:

Да-да, очень просто.

Результат

Рекомендации

1. Лучше запускать OCR на сервер стороне. Если у вас Java проект — используйте tess4j — JNA wrapper для Tesseract. Качество распознавания выше на 20-30%. Не садит батарею, не утяжеляет .apk.

2. Используйте предобработку изображения, перед тем как распознавать. Самый простой способ — заставить юзера выделить блок с текстом, что бы уменьшить площадь распознавания. Сюда можно отнести и выравнивание искажений, удаление шумов, цветокоррекцию.

Источник

What is tesseract in android

Fork of tess-two rewritten from scratch to build with CMake and support latest Android Studio and Tesseract OCR.

The Java/JNI wrapper files and tests for Leptonica / Tesseract are based on the tess-two project, which is based on Tesseract Tools for Android.

This project uses additional libraries (with their own specific licenses):

  • Android 4.1 (API 16) or higher
  • A v4.0.0 trained data file(s) for language(s) you want to use. Data files must be copied to the Android device to a directory named tessdata .
  • Application must hold permission READ_EXTERNAL_STORAGE to access tessdata directory.
Читайте также:  Как узнать формат флешки андроид

This library is available in two variants.

  • Standard — Single-threaded. Best for single-core processors or when using multiple Tesseract instances in parallel.
  • OpenMP — Multi-threaded. Provides better performance on multi-core processors when using only single instance of Tesseract.

You can get compiled version of Tesseract4Android from JitPack.io.

Add the JitPack repository to your project root build.gradle file at the end of repositories:

Add the dependency to your app module build.gradle file:

You can use Android Studio to open the project and build the AAR. Or you can use gradlew from command line.

To build the release version of the library, use task tesseract4android:assembleRelease . After successful build, you will have resulting AAR files in the

  • Open this project in Android Studio.
  • Open Gradle panel, expand Tesseract4Android / :tesseract4Android / Tasks / other and run assembleRelease .

In project directory create local.properties file containing:

Note for paths on Windows you must use \ to escape some special characters, as in example above.

Call gradlew tesseract4android:assembleRelease from command line.

About

Fork of tess-two rewritten from scratch to support latest version of Tesseract OCR.

Источник

What is tesseract in android

This repository provides cross compile instructions and compiled binaries of tesseract for Android. The official repository of tesseract can be found here. The repository with android compilation support can be found here and its port which this repository uses can be found here.

  • Android for armeabi-v7a x86 arm64-v8a x86_64 archs. Users may want to run tesseract commands using Termux App.

Install Instructions For Termux In Android:

The tesseract file should be placed in termux bin directory /data/data/com.termux/files/usr/bin and it should have termux uid:gid ownership and have executable 700 permission before it can be run in the termux terminal without specifying its path.

Copy the file to termux bin directory: Either cd to the download/extraction directory and run following commands

Or use a file browser like root explorer to copy the file to the termux bin directory.

Set correct ownership and permission: Either run following commands to set them automatically, requires su binary to be in $PATH .

Or manually set them with your file browser. You can find termux uid and gid by running the command id -u in a non root shell in termux or by checking the properties of the termux bin directory from your file browser.

Download trained language data file for your language and 4.1 from here. For english the file is eng.traineddata .

Then create /data/data/com.termux/files/home/image_text_searcher/tessdata directory and place the trained language data file eng.traineddata in it or whatever trained language data file you downloaded. The tessdata directory is created inside the image_text_searcher directory to provide consistency with the [Image Text Searcher] project’s default values.

Test OCR on a test jpg with following commands.

To print output text to stdout

To print output text to a file

Читайте также:  Android list files from directory

Cross Compile Instructions for Android Using NDK

Download Android NDK r20 from latest releases or old releases and extract to $HOME/Android/ndk/android-ndk-r20

Get tesseract and leptonica source code

Either clone this repository

Or clone tess-two repository by alexcohn which may be updated with latest versions.

Add NDK path to $PATH

Compile With NDK

Compile tesseract static binary for arm64-v8a

Compile tesseract static binary for all APP_ABIs

tesseract static binary should be compiled at tess-two/tess-two/obj/local/

Extract and zip compiled tesseract binaries to tesseract-out directory with the format tesseract- . This step is optional.

Check if a compiled binary is the correct ABI

Источник

The Code City

Search This Blog

Creating OCR Android app using Tesseract in Android Studio Tutorial

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps

Text recognition in Android has become relatively easier. There are various library that allows you to perform OCR using and Android app. Some like Abby, are commercial text recognition solutions while others like Tesseract are free and open source, hence tesseract is the most common Text recognition library for Android. If you want to detect text regions and not read it, you can refer to my post here — Text detection in Android using openCV.

In this post we will learn —

How to create Android app that performs OCR in Android Studio using Tesseract library :

There are various approaches to do this but this is the most simple and quick approach —
1. Adding tess-two to dependency
2. Creating a class to manage Tesseract calls.
3. Initialize the object of the class and call methods on that object.

We will be using tess-two library for using Tesseract in Android. To use tess-two with Android Studio, just add the following to dependencies of app module-

Now sync the project and you will be able to use Tesseract with Android Studio. So out first step is complete, now let’s move on to the next step. We will create a class that will handle the initialization of TessBaseAPI and contain methods to facilitate call to recognize text from images. Here is the code for the class :

public class MyTessOCR <
private String datapath;
private TessBaseAPI mTess; Context context;
public MyTessOCR(Context context) <
// TODO Auto-generated constructor stub
this.context = context;
datapath = Environment.getExternalStorageDirectory() + «/ocrctz/»;
File dir = new File(datapath + «/tessdata/»);
File file = new File(datapath + «/tessdata/» + «eng.traineddata»);
if (!file.exists()) <
Log.d(«mylog», «in file doesn’t exist»);
dir.mkdirs();
copyFile(context);
>

mTess = new TessBaseAPI();
String language = «eng»;
mTess.init(datapath, language);
//Auto only
mTess.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_ONLY);
>

public void stopRecognition() <
mTess.stop();
>

public String getOCRResult(Bitmap bitmap) <
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
return result;
>

public void onDestroy() <
if (mTess != null)
mTess.end();
>

private void copyFile(Context context) <
AssetManager assetManager = context.getAssets();
try <
InputStream in = assetManager.open(«eng.traineddata»);
OutputStream out = new FileOutputStream(datapath + «/tessdata/» + «eng.traineddata»); byte[] buffer = new byte[1024];
int read = in.read(buffer);
while (read != -1) <
out.write(buffer, 0, read);
read = in.read(buffer);
>
> catch (Exception e) <
Log.d(«mylog», «couldn’t copy with the following error : «+e.toString());
>
>
>

Remember to keep the traineddata file for the language that you need tesseract to recognize in the assets folder. For this example we have eng.traineddata.

Читайте также:  Андроид 10 для huawei p20 lite

Now only the final step remains to build our Android app for OCR. Now we just need to use this class and call required methods for recognition.
In the Activity in which you need to recognition, just Initialize an object of this class :

private MyTessOCR mTessOCR;
mTessOCR = new TessOCR(MainActivity.this);

Once initialized you can just call the following method and pass a bitmap as a parameter, your android app should read the text from the bitmap and return you a string now :

String temp = mTessOCR.getOCRResult(bitmap);

Temp now contains the string value of the text that is read from the bitmap that you passed as an argument in the code above! Congratulations, now you are ready to build your first OCR app for Android. If you want to learn how to recognize text region, and want an example for this code check out the following :
Text Region Detection using OpenCV.
Text Recognition app in Android Example.
Keep Coding, do comment you problems if you have any problems.

Источник

What is tesseract in android

NO LONGER MAINTAINED

This project won’t be receiving further updates or maintenance.

Please consider trying one of the forks of this project such as https://github.com/alexcohn/tess-two.

A fork of Tesseract Tools for Android (tesseract-android-tools) that adds some additional functions. Tesseract Tools for Android is a set of Android APIs and build files for the Tesseract OCR and Leptonica image processing libraries.

This project works with:

  • Tesseract 3.05
  • Leptonica 1.74.1
  • libjpeg 9b
  • libpng 1.6.25

The source code for these dependencies is included within the tess-two/jni folder.

The tess-two module contains tools for compiling the Tesseract and Leptonica libraries for use on the Android platform. It provides a Java API for accessing natively-compiled Tesseract and Leptonica APIs.

The eyes-two module contains additional image processing code copied from the eyes-free project. It includes native functions for text detection, blur detection, optical flow detection, and thresholding. Eyes-two is not needed for using the Tesseract or Leptonica APIs.

The tess-two-test module contains instrumented unit tests for tess-two.

  • Android 2.3 or higher
  • A v3.04 trained data file for a language. Data files must be copied to the Android device in a subdirectory named tessdata .

To use tess-two from your app, edit your app module’s build.gradle file to add tess-two as an external dependency:

If you want to modify the tess-two code, or you want to use the eyes-two module, you may build the project yourself locally. See BUILDING.md.

Release points are tagged with version numbers. A change to the major version number indicates an API change making that version incompatible with previous versions.

The change log shows what’s new in each version.

The Google Mobile Vision API team has made available an OCR capability that is provided to developers through the Google Play Services library: The Mobile Vision API offers a simple API for OCR that currently works for Latin-based characters.

Источник

Оцените статью