Skip to content

Instantly share code, notes, and snippets.

@jav974
Last active January 4, 2023 13:11
Show Gist options
  • Save jav974/072425f14927e6ca2c7a4439d8ac5457 to your computer and use it in GitHub Desktop.
Save jav974/072425f14927e6ca2c7a4439d8ac5457 to your computer and use it in GitHub Desktop.
This bash script compiles openalpr for android. You just need to provide the path to Android SDK and Android NDK in the first lines of this code and execute it. Tested on ubuntu 17.04 64bit
#!/bin/bash
# You should tweak this section to adapt the paths to your need
export ANDROID_HOME=/home/jeremy/Android/Sdk
export NDK_ROOT=/home/jeremy/Android/Sdk/ndk-bundle
ANDROID_PLATFORM="android-21"
# In my case, FindJNI.cmake does not find java, so i had to manually specify these
# You could try without it and remove the cmake variable specification at the bottom of this file
JAVA_HOME=/usr/lib/jvm/oracle-java8-jdk-amd64
JAVA_AWT_LIBRARY=$JAVA_HOME/jre/lib/amd64
JAVA_JVM_LIBRARY=$JAVA_HOME/jre/lib/amd64
JAVA_INCLUDE_PATH=$JAVA_HOME/include
JAVA_INCLUDE_PATH2=$JAVA_HOME/include/linux
JAVA_AWT_INCLUDE_PATH=$JAVA_HOME/include
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
####################################################################
# Prepare Tesseract and Leptonica, using rmtheis/tess-two repository
####################################################################
git clone --recursive https://github.com/rmtheis/tess-two.git tess2
cd tess2
echo "sdk.dir=$ANDROID_HOME
ndk.dir=$NDK_ROOT" > local.properties
./gradlew assemble
cd ..
####################################################################
# Download and extract OpenCV4Android
####################################################################
wget -O opencv-3.2.0-android-sdk.zip -- https://sourceforge.net/projects/opencvlibrary/files/opencv-android/3.2.0/opencv-3.2.0-android-sdk.zip/download
unzip opencv-3.2.0-android-sdk.zip
rm opencv-3.2.0-android-sdk.zip
####################################################################
# Download and configure openalpr from jav974/openalpr forked repo
####################################################################
git clone https://github.com/jav974/openalpr.git openalpr
mkdir openalpr/android-build
TESSERACT_SRC_DIR=$SCRIPTPATH/tess2/tess-two/jni/com_googlecode_tesseract_android/src
rm -rf openalpr/src/openalpr/ocr/tesseract
mkdir openalpr/src/openalpr/ocr/tesseract
shopt -s globstar
cd $TESSERACT_SRC_DIR
cp **/*.h $SCRIPTPATH/openalpr/src/openalpr/ocr/tesseract
cd $SCRIPTPATH
declare -a ANDROID_ABIS=("armeabi"
"armeabi-v7a"
"armeabi-v7a with NEON"
"arm64-v8a"
"mips"
"mips64"
"x86"
"x86_64"
)
cd openalpr/android-build
for i in "${ANDROID_ABIS[@]}"
do
if [ "$i" == "armeabi-v7a with NEON" ]; then abi="armeabi-v7a"; else abi="$i"; fi
TESSERACT_LIB_DIR=$SCRIPTPATH/tess2/tess-two/libs/$abi
if [[ "$i" == armeabi* ]];
then
arch="arm"
lib="lib"
elif [[ "$i" == arm64-v8a ]];
then
arch="arm64"
lib="lib"
elif [[ "$i" == mips ]] || [[ "$i" == x86 ]];
then
arch="$i"
lib="lib"
elif [[ "$i" == mips64 ]] || [[ "$i" == x86_64 ]];
then
arch="$i"
lib="lib64"
fi
echo "
######################################
Generating project for arch $i
######################################
"
rm -rf "$i" && mkdir "$i"
cd "$i"
cmake \
-DANDROID_TOOLCHAIN=clang \
-DCMAKE_TOOLCHAIN_FILE=$NDK_ROOT/build/cmake/android.toolchain.cmake \
-DANDROID_NDK=$NDK_ROOT \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_PLATFORM=$ANDROID_PLATFORM \
-DANDROID_ABI="$i" \
-DANDROID_STL=gnustl_static \
-DANDROID_CPP_FEATURES="rtti exceptions" \
-DTesseract_INCLUDE_BASEAPI_DIR=$TESSERACT_SRC_DIR/api \
-DTesseract_INCLUDE_CCSTRUCT_DIR=$TESSERACT_SRC_DIR/ccstruct \
-DTesseract_INCLUDE_CCMAIN_DIR=$TESSERACT_SRC_DIR/ccmain \
-DTesseract_INCLUDE_CCUTIL_DIR=$TESSERACT_SRC_DIR/ccutil \
-DTesseract_LIB=$TESSERACT_LIB_DIR/libtess.so \
-DLeptonica_LIB=$TESSERACT_LIB_DIR/liblept.so \
-DOpenCV_DIR=$SCRIPTPATH/OpenCV-android-sdk/sdk/native/jni \
-DJAVA_AWT_LIBRARY=$JAVA_AWT_LIBRARY \
-DJAVA_JVM_LIBRARY=$JAVA_JVM_LIBRARY \
-DJAVA_INCLUDE_PATH=$JAVA_INCLUDE_PATH \
-DJAVA_INCLUDE_PATH2=$JAVA_INCLUDE_PATH2 \
-DJAVA_AWT_INCLUDE_PATH=$JAVA_AWT_INCLUDE_PATH \
-DPngt_LIB=$TESSERACT_LIB_DIR/libpngt.so \
-DJpgt_LIB=$TESSERACT_LIB_DIR/libjpgt.so \
-DJnigraphics_LIB=$NDK_ROOT/platforms/$ANDROID_PLATFORM/arch-$arch/usr/$lib/libjnigraphics.so \
-DANDROID_ARM_MODE=arm \
../../src/
cmake --build . -- -j 8
cd ..
done
echo "
All done !!!"
@alaroma
Copy link

alaroma commented Jan 16, 2020

  • In android studio my eu.conf was in the directory runtime_data / config, moving it to the directory /runtime_data alpr.isLoaded() returned true.
  • I already wrote that the structure is exactly the same as yours
  • No, it is console in debug process on my device:
`
01/16 19:07:58: Launching 'app' on Sony F3111.
$ adb shell am start -n "com.example.var8/com.example.var8.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.var8.test | com.example.var8
Waiting for application to come online: com.example.var8.test | com.example.var8
Waiting for application to come online: com.example.var8.test | com.example.var8
Connecting to com.example.var8
Connected to the target VM, address: 'localhost:8601', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Late-enabling -Xcheck:jni
W/ActivityThread: Application com.example.var8 is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/art: Debugger is active
I/System.out: Debugger has connected
    waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1428)
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
I/System: FinalizerDaemon: finalize objects = 1
I/[MALI][Gralloc]: [+]r_hnd(0x7f79939320), client(35), share_fd(33)
E/GED: Failed to get GED Log Buf, err(0)
I/OpenGLRenderer: Initialized EGL, version 1.4
I/OpenGLRenderer: Get enable program binary service property (1)
    Initializing program atlas...
I/OpenGLRenderer: Program binary detail: Binary length is 147292, program map length is 128.
I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 40, and path is /dev/ashmem.
    No need to use file discriptor anymore, close fd(40).
W/libEGL: [ANDROID_RECORDABLE] format: 1
I/PerfService: PerfServiceNative api init
I/[MALI][Gralloc]: [+]r_hnd(0x7f89060d20), client(35), share_fd(42)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@770160e time:6933542
I/[MALI][Gralloc]: [+]r_hnd(0x7f89060dc0), client(35), share_fd(44)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@770160e time:6937954
I/[MALI][Gralloc]: [+]r_hnd(0x7f890606e0), client(35), share_fd(46)
I/[MALI][Gralloc]: [+]r_hnd(0x7f89060fa0), client(35), share_fd(48)
I/[MALI][Gralloc]: [-]r_hnd(0x7f89060dc0), client(35), share_fd(44)
I/[MALI][Gralloc]: [-]r_hnd(0x7f890606e0), client(35), share_fd(46)
I/[MALI][Gralloc]: [-]r_hnd(0x7f89060fa0), client(35), share_fd(48)
I/[MALI][Gralloc]: [-]r_hnd(0x7f89060d20), client(35), share_fd(42)
E/C++->AlprImpl->OpenALPR Initialization Time:: 107.559384
I/Choreographer: Skipped 310 frames!  The application may be doing too much work on its main thread.
W/libEGL: [ANDROID_RECORDABLE] format: 1
W/libEGL: [ANDROID_RECORDABLE] format: 1
I/[MALI][Gralloc]: [+]r_hnd(0x7f89060d20), client(35), share_fd(44)
I/[MALI][Gralloc]: [+]r_hnd(0x7f89060b40), client(35), share_fd(46)
I/[MALI][Gralloc]: [+]r_hnd(0x7f89060c80), client(35), share_fd(48)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@770160e time:6950058
I/[MALI][Gralloc]: [+]r_hnd(0x7f890610e0), client(35), share_fd(42)
I/[MALI][Gralloc]: [+]r_hnd(0x7f89061180), client(35), share_fd(52)
I/C++->AlprImpl->recognizeFullDetails(): Analyzing:: eu
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
`

@cefaci
Copy link

cefaci commented Jan 16, 2020

Did you read what I post?

In android studio my eu.conf was in the directory runtime_data / config, moving it to the directory /runtime_data alpr.isLoaded() returned true.

  • DON'T copy/move/touch ANYTHING in runtime_data, leave it as you have git cloned (unless you know what you do or extend it w/ other countries e.g. RU) and COPY it completly unchanged in your assets and then on your phone.
  • COPY my openaplr.conf aka openAlprConfFile, change it as needed, move it anywhere and load it in the new Alpr()
  • As I wrote 10 times country_code looks for a file in runtime_data/config/[coutry_code].xml and has nothing to do with the openaplr.conf aka openAlprConfFile! Your copied eu.xml is not a valid openAlprConfFile

I/C++->AlprImpl->recognizeFullDetails(): Analyzing:: eu
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'

openaplr.conf is not the runtime_data/config/[coutry_code].xml file. Just check the C++ code there are the default config values...

@alaroma
Copy link

alaroma commented Jan 16, 2020

Yes, I did exactly as you said. Here is my catalog.
Maybe the problem is how I call the function? (from asynk task)
1

@RobertSasak
Copy link

I started to migrate this gist into a proper repo with a Travis for building. So that we can easy reproduce building every time.

Repository https://github.com/RobertSasak/openalpr-android
Travis: https://travis-ci.com/RobertSasak/openalpr-android

Currently I have two issues:

  1. Error when building Tess-two. I believe this is issue with the latest AndroidX changes. I am quite sure that I need to downgrade something, but I am not sure what and to what version.
error: package android.support.annotation does not exist import android.support.annotation.Size;
  1. In cmake. I think this can also be resolved by downgrading NDK. What version I should use?
CMake Error at /usr/local/android-sdk/ndk-bundle/build/cmake/android.toolchain.cmake:250 (message):
  gnustl_static is no longer supported.  Please switch to either c++_shared
  or c++_static.  See
  https://developer.android.com/ndk/guides/cpp-support.html for more
  information.

We are using OpenAlpr in react-native projects https://github.com/RobertSasak/react-native-openalpr . This work very well. However Google Play requires 64bits libraries in order to publish app. So we are in desperate need to OpenAlpr 64bit libraries.

@karan-patel61
Copy link

I am having trouble generating the library files using this script but I am using:

Windows 10
Ubuntu 18.04

Which compiler do I need to use in order to generate the .so files Windows Visual Studio, Ninja, Unix Makefiles, or another compiler?

@AbhishekHirapara
Copy link

  • I mailed you the .so files
  • Use OpenCV 4.1.1 for android then you can link it in your project static
  • To check your code doesn't make sense, as everything is happening in the library and the return looks good, as it executes and takes time. I still think it can be your images or your configs are not right.
  • My build.gradle:
...
android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        ...
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.02"
        ndk {
            abiFilters "arm64-v8a","armeabi-v7a"
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
...
  • I mailed you the .so files
  • Use OpenCV 4.1.1 for android then you can link it in your project static
  • To check your code doesn't make sense, as everything is happening in the library and the return looks good, as it executes and takes time. I still think it can be your images or your configs are not right.
  • My build.gradle:
...
android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        ...
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.02"
        ndk {
            abiFilters "arm64-v8a","armeabi-v7a"
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
...

@cefaci Can you send me .so file for 64 bit? My mail is : abhijnd@gmail.com

@momodeveloper
Copy link

@AbhishekHirapara can you mail me the .so files please if you managed to get them ?, my mail is nassikmohamed7@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment