I get SIGSEGV error when I call TessBaseAPI.Init().
OCRUtil.java:
public class OCRUtil
{
private TessBaseAPI mTess; //Tess API reference
String datapath = "" ; //path to data
private boolean isInitiated=false;
private String TAG="BTOPP OCRHELPER";
public void Init()
{
Log.v(TAG,"OCR INIT");
if(isInitiated)
return;
//init image
//image = BitmapFactory.decodeFile(""); //sample file
//train data path
datapath = WorkerService.getContext().getFilesDir()+"/tesseract/";
//Check if the training data has been copied
checkFile(new File(datapath + "tessdata/"));
Log.d(TAG,"CheckFile done");
//Tesseract API
String lang = "eng";
mTess = new TessBaseAPI();
Log.d(TAG,"NEW tessApi done");
mTess.setDebug(true);
System.gc();
try
{
mTess.init(datapath, lang);
isInitiated=true;
}catch(Exception e)
{
Log.e(TAG,"init error",e);
}
Log.d(TAG,"TESSINIT DONE");
return;
}
//Process an Image
public String processImage(Bitmap img)
{
String OCRresult;
mTess.setImage(img);
OCRresult = mTess.getUTF8Text();
//TextView OCRTextView = (TextView) findViewById(R.id.OCRTextView);
//OCRTextView.setText(OCRresult);
return OCRresult;
}
public String processImage(String path)
{
return processImage(BitmapFactory.decodeFile(path));
}
//copy file to device
private void copyFiles()
{
try
{
String filepath = datapath + "/tessdata/eng.traineddata";
AssetManager assetManager = WorkerService.getContext().getAssets();
InputStream instream = assetManager.open("tessdata/eng.traineddata");
OutputStream outstream = new FileOutputStream(filepath);
byte[] buffer = new byte[1024];
int read;
while ((read = instream.read(buffer)) != -1)
{
outstream.write(buffer, 0, read);
}
outstream.flush();
outstream.close();
instream.close();
}
catch (FileNotFoundException e)
{
Log.e(TAG,"Copy Files",e);
e.printStackTrace();
}
catch (IOException e)
{
Log.e(TAG,"Copy Files",e);
e.printStackTrace();
}
return ;
}
//check file on the device
private void checkFile(File dir)
{
//if the dir not exist then create dir and copy
if (!dir.exists() && dir.mkdirs())
{
copyFiles();
}
//if dir exists but the file doesn't then copy file
if (dir.exists())
{
String datafilepath = datapath + "/tessdata/eng.traineddata";
File datafile = new File(datafilepath);
if (!datafile.exists())
{
copyFiles();
}
}
return ;
}
}
Util.java that uses OCRUtil:
static OCRUtil ocrUtil=new OCRUtil();
public static String getStringFromImageFile(String path)
{
Log.d(TAG,"GetStrfromImg");
ocrUtil.Init();
Log.d(TAG,"Init done");
return ocrUtil.processImage(path);
}
Logcat:
03-18 12:39:08.220 D/BTOPP Utility(5258): GetStrfromImg
03-18 12:39:08.220 V/BTOPP OCRHELPER(5258): OCR INIT
03-18 12:39:08.220 D/BTOPP OCRHELPER(5258): CheckFile done
03-18 12:39:08.230 D/dalvikvm(5258): Trying to load lib /data/app-lib/com.kyunggi.worker2-1/liblept.so 0x41a656b8
03-18 12:39:08.230 D/dalvikvm(5258): Added shared lib /data/app-lib/com.kyunggi.worker2-1/liblept.so 0x41a656b8
03-18 12:39:08.230 D/dalvikvm(5258): Trying to load lib /data/app-lib/com.kyunggi.worker2-1/libtess.so 0x41a656b8
03-18 12:39:08.250 D/dalvikvm(5258): Added shared lib /data/app-lib/com.kyunggi.worker2-1/libtess.so 0x41a656b8
03-18 12:39:08.250 D/BTOPP OCRHELPER(5258): NEW tessApi done
03-18 12:39:08.271 D/dalvikvm(5258): GC_EXPLICIT freed 205K, 58% free 5019K/11744K, paused 3ms+2ms, total 20ms
03-18 12:39:08.271 F/libc (5258): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 7896 (Thread-410)
03-18 12:39:08.331 I/DEBUG (262): pid: 5258, tid: 7896, name: Thread-410 >>> com.kyunggi.worker2 <<<
03-18 12:39:09.432 I/ActivityManager(982): Process com.kyunggi.worker2 (pid 5258) has died.
03-18 12:39:09.602 D/Zygote (266): Process 5258 terminated by signal (11)
Project directory hierarchy:
app:
build/
jni/
jniLibs/
libs/
obj/
src/
build.gradle
proguard.rules.pro
app/jniLibs:
armeabi-v7a/
x86/
app/jniLibs/armeabi-v7a:
liblept.so (0.95MB)
libtess.so (1.9MB)
app/jniLibs/x86:
liblept.so (1.8MB)
libtess.so (3.3MB)
app/src/main/:
assets/
bin/
gen/
java/
res/
resources/
AndroidManifest.xml
device_admin_simple.xml
app/src/main/assets/tessdata/:
eng.traineddata(30MB)
...
Also I added android:largeheap="true"
to AndroidManifest.xml
.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:resizeableActivity = "true"
android:largeHeap="true" >
I checked if the data file is copied correctly but it was perfect. And I added read/write permissions to my manifest.
What can I do more to make tess-two api work?
I finally got the answer. The cause was that I was using Traineddata with mismatched versions.
User contributions licensed under CC BY-SA 3.0