halkar / Tesseract.Xamarin

Tesseract OCR wrapper for Xamarin
MIT License
121 stars 40 forks source link

Init function with custom traineddata #17

Closed vdurif closed 8 years ago

vdurif commented 8 years ago

Hello,

I'm trying to use custom trained data for OCR-B with Xamarin.Tesseract. I build my own ocrb.traineddata following https://reachsri.wordpress.com/2014/01/28/process-to-train-tesseract-ocr-3/

I put this traineddata in the assets folder and then I call "tesseractApi.Init ("ocrb");" but initialization fail. Have someone an idea about what happens ? I can share my trained data if required.

vdurif commented 8 years ago

Ok sorry, I've a problem with assets folder in Xamarin Studio. My files are note populated to my app I don't know why. But it explains why init function did not work in my case.

halkar commented 8 years ago

Just check if your tessdata folder is setup correctly. For Android tessdata folder should sit under the Assets folder and all files should have Build action set to AndroidAsset. For iOS folder with files marked as BundleResource should be in the Resources folder.

vdurif commented 8 years ago

Yes I've done that. I got it work with "eng" language last week but now when I add files to my assets folder it is not deployed to my phone. So I try to remove all files and add them again in Assets/tessdata and now i don't have any files in Assets deployed to my phone (not even the tessdata folder).

halkar commented 8 years ago

Have you tried removing the app completely? I probably know about one issue that could cause problem like yours.

vdurif commented 8 years ago

You mean removing the app from my phone ? I tried uninstall it, removing the "com.myapp" folder and reinstall it but I still got nothing in Assets folder. I really don't know where it comes from.

EDIT: I tried modifying a layout file in order to see if its modification was deployed after build and it was.

halkar commented 8 years ago

Which OS are you testing on?

vdurif commented 8 years ago

Android 5.1.1 (on a Nexus 4 and Nexus 5)

halkar commented 8 years ago

Can you share your logs?

vdurif commented 8 years ago

Do you mean my build logs ?

halkar commented 8 years ago

No, application logs (logcat).

vdurif commented 8 years ago

[OpenCV] Trying to get library list [OpenCV] Library list: "" [OpenCV] First attempt to load libs [OpenCV] Trying to init OpenCV libs [OpenCV] Trying to load library opencv_java [OpenCV] Library opencv_java loaded [OpenCV] First attempt to load libs is OK [OpenCV] General configuration for OpenCV 2.4.9 ===================================== [OpenCV] Version control: 2.4.9 [OpenCV] Platform: [OpenCV] Host: Linux 3.2.0-56-generic x86_64 [OpenCV] Target: Linux 1 armv7-a [OpenCV] CMake: 2.8.12.1 [OpenCV] CMake generator: Ninja [OpenCV] CMake build tool: /usr/local/bin/ninja [OpenCV] Configuration: Release [OpenCV] C/C++: [OpenCV] Built as dynamic libs?: NO [OpenCV] C++ Compiler: /opt/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-g++ (ver 4.6) [OpenCV] C++ flags (Release): -fexceptions -frtti -Wno-psabi --sysroot=/opt/android-ndk-r8c/platforms/android-8/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -mthumb -fomit-frame-pointer -fno-strict-aliasing -O3 -DNDEBUG -DNDEBUG [OpenCV] C++ flags (Debug): -fexceptions -frtti -Wno-psabi --sysroot=/opt/android-ndk-r8c/platforms/android-8/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -marm -fno-omit-frame-pointer -fno-strict-aliasing -O0 -g -DDEBUG -D_DEBUG -O0 -DDEBUG -D_DEBUG [OpenCV] C Compiler: /opt/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc [OpenCV] C flags (Release): -fexceptions -Wno-psabi --sysroot=/opt/android-ndk-r8c/platforms/android-8/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -mthumb -fomit-frame-pointer -fno-strict-aliasing -O3 -DNDEBUG -DNDEBUG [OpenCV] C flags (Debug): -fexceptions -Wno-psabi --sysroot=/opt/android-ndk-r8c/platforms/android-8/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fdata-sections -ffunction-sections -Wa,--noexecstack -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -marm -fno-omit-frame-pointer -fno-strict-aliasing -O0 -g -DDEBUG -D_DEBUG -O0 -DDEBUG -D_DEBUG [OpenCV] Linker flags (Release): -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -fuse-ld=gold -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now [OpenCV] Linker flags (Debug): -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-sections -fuse-ld=gold -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now [OpenCV] Precompiled headers: YES [OpenCV] OpenCV modules: [OpenCV] To be built: core androidcamera flann imgproc highgui features2d calib3d ml objdetect video contrib photo java legacy ocl stitching superres ts videostab [OpenCV] Disabled: gpu world [OpenCV] Disabled by dependency: - [OpenCV] Unavailable: dynamicuda nonfree python viz [OpenCV] Android: [OpenCV] Android ABI: armeabi-v7a [OpenCV] STL type: gnustl_static [OpenCV] Native API level: android-8 [OpenCV] SDK target: android-11 [OpenCV] Android NDK: /opt/android-ndk-r8c (toolchain: arm-linux-androideabi-4.6) [OpenCV] android tool: /opt/android-sdk-linux/tools/android (Android SDK Tools, revision 22.6.) [OpenCV] Google Play package: YES [OpenCV] Android examples: YES [OpenCV] GUI: [OpenCV] GTK+ 2.x: NO [OpenCV] GThread : NO [OpenCV] GtkGlExt: NO [OpenCV] OpenGL support: NO [OpenCV] VTK support: NO [OpenCV] Media I/O: [OpenCV] ZLib: z (ver 1.2.3) [OpenCV] JPEG: build (ver 62) [OpenCV] PNG: build (ver 1.5.12) [OpenCV] TIFF: build (ver 42 - 4.0.2) [OpenCV] JPEG 2000: build (ver 1.900.1) [OpenCV] OpenEXR: build (ver 1.7.1) [OpenCV] Video I/O: [OpenCV] AndroidNativeCamera: YES, use prebuilt libraries [OpenCV] Other third-party libraries: [OpenCV] Use IPP: NO [OpenCV] Use Eigen: YES (ver 3.1.2) [OpenCV] Use TBB: YES (ver 4.1 interface 6102) [OpenCV] Use OpenMP: NO [OpenCV] Use GCD NO [OpenCV] Use Concurrency NO [OpenCV] Use C=: NO [OpenCV] Use Cuda: NO [OpenCV] Use OpenCL: YES [OpenCV] OpenCL: [OpenCV] Version: dynamic [OpenCV] Include path: /home/reports/ci/slave_desktop/50-SDK/opencv/3rdparty/include/opencl/1.2 [OpenCV] Use AMD FFT: NO [OpenCV] Use AMD BLAS: NO [OpenCV] Python: [OpenCV] Interpreter: /usr/bin/python2 (ver 2.7.3) [OpenCV] Java: [OpenCV] ant: /usr/bin/ant (ver 1.8.2) [OpenCV] Java tests: NO [OpenCV] Documentation: [OpenCV] Build Documentation: YES [OpenCV] Sphinx: /usr/local/bin/sphinx-build (ver 1.1.3) [OpenCV] PdfLaTeX compiler: /usr/bin/pdflatex [OpenCV] Tests and samples: [OpenCV] Tests: NO [OpenCV] Performance tests: NO [OpenCV] C/C++ Examples: NO [OpenCV] Install path: /home/reports/ci/slave_desktop/50-SDK/build/builds/o4a/install [OpenCV] cvconfig.h is in: /home/reports/ci/slave_desktop/50-SDK/build/builds/o4a [OpenCV] -----------------------------------------------------------------

[[TesseractApi]] Copy assets to /storage/emulated/0/Android/data/com.mycompany.myapp/files Thread started: #2 Thread started: #3 [Mono] DllImport attempting to load: '/system/lib/liblog.so'. [Mono] DllImport loaded library '/system/lib/liblog.so'. [Mono] DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so'). [Mono] Searching for 'android_log_print'. [Mono] Probing 'android_log_print'. [Mono] Found as '__android_log_print'. [Mono] DllImport attempting to load: '/system/lib/libc.so'. [Mono] DllImport loaded library '/system/lib/libc.so'. [Mono] DllImport searching in: '/system/lib/libc.so' ('/system/lib/libc.so'). [Mono] Searching for 'gettid'. [Mono] Probing 'gettid'. [Mono] Found as 'gettid'. [MonoDroid] UNHANDLED EXCEPTION: [MonoDroid] System.InvalidOperationException: Call Init first [MonoDroid] at Tesseract.Droid.TesseractApi.CheckIfInitialized () <IL 0x00015, 0x00094> [MonoDroid] at Tesseract.Droid.TesseractApi.SetWhitelist (string) <IL 0x00001, 0x00047> [MonoDroid] at myapp.Droid.CameraActivity..ctor () [0x00054] in c:\Users\myname\Documents\Projects\myapp\Droid\CameraActivity.cs:66 [MonoDroid] at (wrapper dynamic-method) object.fd016a2a-adf7-43a5-a458-e908eaefd27b (intptr,object[]) <IL 0x00018, 0x0004b> [MonoDroid] at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) [0x000c2] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:156 [MonoDroid] at (wrapper dynamic-method) object.3882b8f7-7a8b-49a3-a391-8d03b975cc88 (intptr,intptr,intptr,intptr,intptr,intptr) <IL 0x00029, 0x0007b> [art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable [AndroidRuntime] Shutting down VM

EDIT : Did not know which part of logs you wanted , I just put part of code throwing exception after init failed :D

halkar commented 8 years ago

Thanks, this is the log, that I need. Can you share your initailization code? Do you await TesseractApi.Init method? Result is true?

vdurif commented 8 years ago

No I did not. I still don't know much .NET but when I use await it asks me to put an async on my function and I can't on my constructor (I suppose i could use a Task.run() or something like this but I don't really looked at it for now).

I add it to an async function and used await: it works. I used an explorer on my phone and tessdata is now fully deployed on my phone. Does it comes from await ? I thought that Assets folder will be filled during installation.

Anyway, thank you halkar for helping me :)

halkar commented 8 years ago

If you could share your code I will be able to suggest something :)

vdurif commented 8 years ago

What I change : tesseractApi = new TesseractApi (Application.Context); bool success = await tesseractApi.Init ("ocrb"); if (success) { Log.Debug (TAG, "Tesseract success init"); tesseractApi.SetWhitelist ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ<"); tesseractApi.SetVariable ("load_system_dawg", "F"); tesseractApi.SetVariable ("load_freq_dawg", "F"); tesseractApi.SetVariable ("load_unambig_dawg", "F"); tesseractApi.SetVariable ("load_punc_dawg", "F"); tesseractApi.SetVariable ("load_number_dawg", "F"); tesseractApi.SetVariable ("load_fixed_length_dawgs", "F"); tesseractApi.SetVariable ("load_bigram_dawg", "F"); tesseractApi.SetVariable ("wordrec_enable_assoc", "F"); } else { Log.Debug (TAG, "Tesseract failed init"); }

How it was before :

tesseractApi = new TesseractApi (Application.Context);
        tesseractApi.Init ("ocrb");tesseractApi.SetWhitelist ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ<");
            tesseractApi.SetVariable ("load_system_dawg", "F");
            tesseractApi.SetVariable ("load_freq_dawg", "F");
            tesseractApi.SetVariable ("load_unambig_dawg", "F");
            tesseractApi.SetVariable ("load_punc_dawg", "F");
            tesseractApi.SetVariable ("load_number_dawg", "F");
            tesseractApi.SetVariable ("load_fixed_length_dawgs", "F");
            tesseractApi.SetVariable ("load_bigram_dawg", "F");
            tesseractApi.SetVariable ("wordrec_enable_assoc", "F");
halkar commented 8 years ago

Your code looks fine. I wonder why not all files are deployed. Try uninstalling the app and deploing it again ;)

vdurif commented 8 years ago

I wil ltry deploying it on another phone. I'll edit with result.

halkar commented 8 years ago

Thanks!

vdurif commented 8 years ago

Ok I tried depolying it on a Nexus 5. I had to uninstall old app first if not it doesn't update my Assets folder but after that it works great. I think of Xamarin Studio issue :).

halkar commented 8 years ago

Cool!

RashidGit-Hub commented 1 year ago

Hello,

I'm trying to use custom trained data for OCR-B with Xamarin.Tesseract. I build my own ocrb.traineddata following https://reachsri.wordpress.com/2014/01/28/process-to-train-tesseract-ocr-3/

I put this traineddata in the assets folder and then I call "tesseractApi.Init ("ocrb");" but initialization fail. Have someone an idea about what happens ? I can share my trained data if required.

Hello Sir,

I am new to xamarin android development. I have eng.traineddata file and i am using this in xamarin android but scan always gives me very bad result. Can you please share with me your ocrb trained data.

Thanks and best regards