This contains the code for Image Classification with OpenCV for Android. For more information - visit Image Classification with OpenCV for Android
Our code is tested using Python 3.7.5, but it should also work with any other python3.x. If you'd like to check your version run:
python3 -V
Let's create a new virtual environment. You'll need to install virtualenv package if you don't have it:
pip install virtualenv
Now we can create a new virtualenv variable and call it env
:
python3 -m venv ~/env
The last thing we have to do is to activate it:
source ~/env/bin/activate
To install the required python dependencies run:
pip3 install -r requirements.txt
In this blog post we are using OpenCV 4.3.0 unavailable via pip
. The first step is building the OpenCV library. To do so:
- Check the list of the below libraries. Install the missed dependencies:
sudo apt-get update
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev
For OpenCV Java installation we used default Java Runtime Environment and Java Development Kit:
sudo apt-get install default-jre
sudo apt-get install default-jdk
sudo apt-get install ant
- Download the latest OpenCV version from the official repository:
cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.3.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.3.0.zip
- Unzip the downloaded archives:
unzip opencv.zip
unzip opencv_contrib.zip
- Rename the directories to match CMake paths:
mv opencv-4.3.0 opencv
mv opencv_contrib-4.3.0 opencv_contrib
- Compile OpenCV. Create and enter a build directory:
cd ~/opencv
mkdir build && cd build
- Run CMake to configure the OpenCV build. Don't forget to set the right pass to the
PYTHON_EXECUTABLE
:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON_EXECUTABLE=~/env/bin/python3 \
-D ANT_EXECUTABLE=/usr/bin/ant \
-D BUILD_SHARED_LIBRARY=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_EXAMPLES=ON ..
If you want to configure the build with some specific Java version, please, add the following fields, verifying the paths:
-D JAVA_AWT_INCLUDE_PATH=/usr/lib/jvm/java-1.x.x-openjdk-amd64/include \
-D JAVA_AWT_LIBRARY=/usr/lib/jvm/java-1.x.x-openjdk-amd64/lib/libawt.so \
-D JAVA_INCLUDE_PATH=/usr/lib/jvm/java-1.x.x-openjdk-amd64/include \
-D JAVA_INCLUDE_PATH2=/usr/lib/jvm/java-1.x.x-openjdk-amd64/include/linux \
-D JAVA_JVM_LIBRARY=/usr/lib/jvm/java-1.x.x-openjdk-amd64/include/jni.h \
- Check the output and make sure that everything is set correctly. After that we're ready to build it with:
make -j8
Make sure, you didn't get any errors. In case of successful completion you will find the following files in the build
directory lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so
.
Then run the following command:
sudo ldconfig
which creates the necessary links and cache to our freshly built shared library.
The last step is to move lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so
into the virtual environment installed packages:
cp lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so ~/env/lib/python3.7/site-packages/cv2.so
For Android application development we will need OpenCV for Android:
wget https://github.com/opencv/opencv/releases/download/4.3.0/opencv-4.3.0-android-sdk.zip -O opencv-4.3.0-android-sdk.zip
unzip opencv-4.3.0-android-sdk.zip
rm opencv-4.3.0-android-sdk.zip
The proposed for the experiments MobileNetV2ToOnnx.py
script supports the --input_image
key to customize the model conversion pipeline. It defines the full input image path, including its name - "test_img_cup.jpg"
by default.
To run MobileNetV2 conversion case, please, choose one of the described below scenarios:
- for the custom input image and running evaluation of the converted model:
python3 MobileNetV2Conversion.py --input_image <image_name>
- for the default input image:
python3 MobileNetV2Conversion.py
Want to become an expert in AI? AI Courses by OpenCV is a great place to start.