Skip to content

failure-to-thrive/wheat-grains-detector

Repository files navigation

Wheat Grains Detector

For Celiac Disease sufferers

App in action

Android + OpenCV + Tensorflow Object Detection API

Please note: this is a sidenotes for my own personal use rather than a detailed step-by-step instruction on how to build the project. However an experienced developer should get what's going on.

Preparing dataset

LabelImg/VoTT/LabelBox for segmentation and annotation are all fine. LabelImg is finest.

Generate TFRecord

python create_pascal_tf_record_ex.py --annotations_dir dataset/train --label_map_path dataset/label_map.pbtxt --output_path train.record
python create_pascal_tf_record_ex.py --annotations_dir dataset/val --label_map_path dataset/label_map.pbtxt --output_path val.record

Training model

Connect to Colaboratory and upload colab/Wheat_Grains_Detector.ipynb

To upload:

  • colab/ssd_mobilenet_v2_coco.config
  • dataset/label_map.pbtxt
  • train.record
  • val.record

Will be downloaded:

  • frozen.zip

Building Android app

Install JDK

sudo apt-get install openjdk-8-jdk-headless

or

Download Java SE Development Kit 8 and unpack to <JDK_DIR>. jdk-8u181-linux-x64.tar.gz was used.

export PATH=$PATH:<JDK_DIR>/bin/

Install Android SDK

Download SDK tools and unpack to <ANDROID_SDK_DIR>. sdk-tools-linux-4333796.zip was used.

export ANDROID_SDK_HOME=<ANDROID_SDK_DIR>

export PATH=$PATH:$ANDROID_SDK_HOME/tools/bin/

sdkmanager "build-tools;28.0.2"
export PATH=$PATH:$ANDROID_SDK_HOME/build-tools/28.0.2/

sdkmanager "platforms;android-21"

sudo apt-get install adb

Connect adb to a device.

Build OpenCV library

Download Android pack and unpack to <OPENCV_SDK_DIR>. opencv-3.4.3-android-sdk.zip was used.

export OPENCV_SDK_JAVA=<OPENCV_SDK_DIR>/sdk/java/
pushd $OPENCV_SDK_JAVA
mkdir -p build/gen/ build/obj/
aapt package -m -J build/gen/ -M AndroidManifest.xml -S res/ -I $ANDROID_SDK_HOME/platforms/android-21/android.jar
aidl -obuild/gen/ src/org/opencv/engine/OpenCVEngineInterface.aidl

create build/gen/BuildConfig.java with the following content:

package org.opencv;

public final class BuildConfig {
    public static final boolean DEBUG = Boolean.parseBoolean("false");
}
javac -d build/obj/ -bootclasspath $ANDROID_SDK_HOME/platforms/android-21/android.jar build/gen/BuildConfig.java build/gen/org/opencv/R.java build/gen/org/opencv/*/*.java src/org/opencv/*/*.java
aapt package -F build/opencv.jar -M AndroidManifest.xml -S res/ -I $ANDROID_SDK_HOME/platforms/android-21/android.jar build/obj/
popd

Build app

pushd android
mkdir -p build/gen/ build/obj/ build/bin/lib/ build/bin/assets/
aapt package -m -J build/gen/ -M AndroidManifest.xml -S $OPENCV_SDK_JAVA/res/ -S res/ -I $ANDROID_SDK_HOME/platforms/android-21/android.jar
javac -d build/obj/ -bootclasspath $ANDROID_SDK_HOME/platforms/android-21/android.jar -classpath $OPENCV_SDK_JAVA/build/opencv.jar build/gen/com/github/failure_to_thrive/wheat_grains_detector/R.java src/com/github/failure_to_thrive/wheat_grains_detector/MainActivity.java
dx --dex --output=build/bin/classes.dex $OPENCV_SDK_JAVA/build/opencv.jar build/obj/

copy <OPENCV_SDK_DIR>/sdk/native/libs/armeabi-v7a/ to build/bin/lib/
copy <OPENCV_SDK_DIR>/sdk/native/libs/arm64-v8a/ to build/bin/lib/
unpack frozen.zip to build/bin/assets/

aapt package -F build/WheatGrainsDetector.unaligned.apk -M AndroidManifest.xml -S $OPENCV_SDK_JAVA/res/ -S res/ -I $ANDROID_SDK_HOME/platforms/android-21/android.jar build/bin/
apksigner sign --key <key> --cert <cert> build/WheatGrainsDetector.unaligned.apk
zipalign -p 4 build/WheatGrainsDetector.unaligned.apk build/WheatGrainsDetector.apk
adb install build/WheatGrainsDetector.apk
popd
adb logcat WGD:* *:S

That's all!