From c692f88fce7d3dd3d3d11e044206c76342016c93 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Fri, 19 Jul 2024 21:42:47 +0200 Subject: [PATCH] improve setup script and update readme accordingly --- README.md | 20 +--- build_alpr_api.sh | 2 +- build_and_setup_ultimatealvr.sh | 199 +++++++++++++++++++++++++------- plugins.xml | 20 ---- 4 files changed, 162 insertions(+), 79 deletions(-) delete mode 100644 plugins.xml diff --git a/README.md b/README.md index 13014a8..acfe7c3 100644 --- a/README.md +++ b/README.md @@ -42,24 +42,12 @@ the ``dist`` folder. ## Setup development environment -### Install ultimateALPR SDK -#### Manually build the wheel -If you want to build the wheel yourself, you can use the ``build_and_setup_ultimatealvr.sh`` script. It will create a new -directory ``tmp`` and build the wheel in there. It also includes the assets and libs folders needed when developing. +### Use automatic setup script +You can use the ``build_and_setup_ultimatealvr.sh`` script to automatically install the necessary packages and build the ultimateALPR SDK wheel, copy the assets and the libs. > [!IMPORTANT] > Make sure to install the package python3-dev (APT) python3-devel (RPM) before running the build and setup script. -#### Use already built wheel (quick and easy but not recommended for dev) -I have already built the ultimateALPR SDK for x86_64 and ARM64 and included the python3.10 wheel in the wheel folder. -You can install the wheel using : ``pip install wheel/*.whl`` -### Copy necessary files/folders -Copy the ``assets`` and ``libs`` folders to the same directory as the script. - -- If you built the wheel in the previous step, you can copy the ``assets`` and ``libs`` folders from the ``tmp`` directory. - -- *If you used the already built wheel, you can find the 'assets' and 'libs' folders on the [GitHub repository](https://github.com/DoubangoTelecom/ultimateALPR-SDK/tree/master/assets), although you will manually copy every ``*.so*`` file from ``/binaries/linux//`` into a newly created ``libs`` folder.* - -The structure should look like this: +The end structure should look like this: ```bash . ├── alpr_api.py @@ -74,7 +62,7 @@ The structure should look like this: ``` ### Important notes -When building or developing the script, make sure to set the ``LD_LIBRARY_PATH`` environment variable to the libs folder +When running, building or developing the script, make sure to set the ``LD_LIBRARY_PATH`` environment variable to the libs folder *(limitation of the ultimateALPR SDK)*. ```bash export LD_LIBRARY_PATH=libs:$LD_LIBRARY_PATH diff --git a/build_alpr_api.sh b/build_alpr_api.sh index c262c89..2c8925f 100755 --- a/build_alpr_api.sh +++ b/build_alpr_api.sh @@ -1 +1 @@ -pyinstaller --noconfirm --onefile --console --add-data libs:. --add-data assets:assets --add-data static:static --add-data templates:templates --add-binary plugins.xml:. --name easy-local-alpr-1.0.0-linux_x86_64 "alpr_api.py" +pyinstaller --noconfirm --onefile --console --add-data libs:. --add-data assets:assets --add-data static:static --add-data templates:templates --name easy-local-alpr-1.0.0-linux_x86_64 "alpr_api.py" diff --git a/build_and_setup_ultimatealvr.sh b/build_and_setup_ultimatealvr.sh index 21bf6ae..7980709 100755 --- a/build_and_setup_ultimatealvr.sh +++ b/build_and_setup_ultimatealvr.sh @@ -1,58 +1,173 @@ -# clone sdk -mkdir ./tmp -cd tmp -wget https://github.com/DoubangoTelecom/ultimateALPR-SDK/archive/8130c76140fe8edc60fe20f875796121a8d22fed.zip -O temp-sdk.zip -unzip temp-sdk.zip -rm temp-sdk.zip +#!/bin/bash -mkdir temp-sdk -mv ultimateALPR-SDK*/* ./temp-sdk -rm -R ultimateALPR-SDK* +# Function to create virtual environment, install the wheel, and copy assets and libs +install_and_setup() { + echo "Creating virtual environment at the root..." + python3.10 -m venv "$ROOT_DIR/venv" >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "Failed to create virtual environment." + exit 1 + fi -# create env to build ultimatealpr-sdk for python -python3.10 -m venv venv -source venv/bin/activate -pip install setuptools wheel Cython + echo "Activating virtual environment..." + source "$ROOT_DIR/venv/bin/activate" -cd temp-sdk + echo "Installing the wheel..." + pip install "$BUILD_DIR"/ultimateAlprSdk-*.whl >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "Failed to install the wheel." + exit 1 + fi -# move folders to simplify build -mkdir -p binaries/linux/x86_64/c++ -cp c++/* binaries/linux/x86_64/c++ -cp python/* binaries/linux/x86_64/ + echo "Deactivating virtual environment..." + deactivate -# edit setup.py to simplify build -cd binaries/linux/x86_64/ + echo "Copying assets and libs folders to the root directory..." + cp -r "$BUILD_DIR/assets" "$ROOT_DIR" + cp -r "$BUILD_DIR/libs" "$ROOT_DIR" + + if [ -f "$ROOT_DIR/requirements.txt" ]; then + echo "Installing requirements..." + source "$ROOT_DIR/venv/bin/activate" + pip install -r "$ROOT_DIR/requirements.txt" >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "Failed to install requirements." + exit 1 + fi + deactivate + fi + + rm -rf "$BUILD_DIR" + + echo "Virtual environment created and wheel installed successfully." + echo "Assets and libs folders copied to the root directory." + + echo "Setup completed." +} + +# Function to prompt user for auto setup choice +prompt_auto_setup() { + read -r -p "Do you want to automatically create a new virtual environment, install the wheel and copy the assets and libs? (y/n): " choice + case "$choice" in + y|Y ) install_and_setup;; + n|N ) echo "Setup completed.";; + * ) echo "Invalid choice. Please run the script again and choose y or n.";; + esac +} + +# Directories +ROOT_DIR=$(pwd) +BUILD_DIR="$ROOT_DIR/tmp-build-env" +SDK_ZIP_URL="https://github.com/DoubangoTelecom/ultimateALPR-SDK/archive/8130c76140fe8edc60fe20f875796121a8d22fed.zip" +SDK_ZIP="$BUILD_DIR/temp-sdk.zip" +SDK_DIR="$BUILD_DIR/temp-sdk" +BIN_DIR="$SDK_DIR/binaries/linux/x86_64" + +# Create build environment +mkdir -p "$BUILD_DIR" +cd "$BUILD_DIR" || exit + +# Clone SDK +echo "Downloading SDK..." +wget "$SDK_ZIP_URL" -O "$SDK_ZIP" >/dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "Failed to download SDK." + exit 1 +fi + +echo "Unzipping SDK..." +unzip "$SDK_ZIP" >/dev/null 2>&1 +rm "$SDK_ZIP" +mkdir "$SDK_DIR" +mv ultimateALPR-SDK*/* "$SDK_DIR" +rm -r ultimateALPR-SDK* + +# Create environment to build ultimatealpr-sdk for Python +echo "Creating virtual environment for building SDK..." +python3.10 -m venv "$BUILD_DIR/venv" >/dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "Failed to create virtual environment." + exit 1 +fi + +echo "Activating virtual environment..." +source "$BUILD_DIR/venv/bin/activate" + +echo "Installing build dependencies..." +pip install setuptools wheel Cython >/dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "Failed to install build dependencies." + exit 1 +fi + +# Move folders to simplify build +mkdir -p "$BIN_DIR/c++" +mv "$SDK_DIR/c++"/* "$BIN_DIR/c++" +mv "$SDK_DIR/python"/* "$BIN_DIR/" + +# Edit setup.py to simplify build +echo "Editing setup.py for simplified build..." +cd "$BIN_DIR" || exit sed -i "s|sources=\[os.path.abspath('../../../python/ultimateALPR-SDK-API-PUBLIC-SWIG_python.cxx')\]|sources=[os.path.abspath('ultimateALPR-SDK-API-PUBLIC-SWIG_python.cxx')]|g" setup.py sed -i "s|include_dirs=\['../../../c++'\]|include_dirs=['c++']|g" setup.py sed -i "s|library_dirs=\['.'\]|library_dirs=['libs']|g" setup.py -# move all .so files into libs folder -mkdir libs -mv *.so libs/ -mv *.so.* libs/ +# Move all .so files into libs folder +mkdir "$BIN_DIR/libs" +mv "$BIN_DIR/"*.so* "$BIN_DIR/libs" -# build the wheel -python setup.py bdist_wheel -v +# Download TensorFlow +read -r -p "Do you want TensorFlow for CPU or GPU? (cpu/gpu): " tf_choice +mkdir -p "$BIN_DIR/tensorflow" +if [ "$tf_choice" == "gpu" ]; then + echo "Downloading TensorFlow GPU..." + wget https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-2.6.0.tar.gz >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "Failed to download TensorFlow GPU." + exit 1 + fi + echo "Extracting TensorFlow GPU..." + tar -xf libtensorflow-gpu-linux-x86_64-2.6.0.tar.gz -C "$BIN_DIR/tensorflow" >/dev/null 2>&1 +else + echo "Downloading TensorFlow CPU..." + wget https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.6.0.tar.gz >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "Failed to download TensorFlow CPU." + exit 1 + fi + echo "Extracting TensorFlow CPU..." + tar -xf libtensorflow-cpu-linux-x86_64-2.6.0.tar.gz -C "$BIN_DIR/tensorflow" >/dev/null 2>&1 +fi -# move the built whl and the libs back to root dir -mv dist/* ../../../../ +mv "$BIN_DIR/tensorflow/lib/"* "$BIN_DIR/libs/" +cp "$BIN_DIR/libs/libtensorflow.so.2" "$BIN_DIR/libs/libtensorflow.so.1" -mv libs ../../../../ +# Build the wheel +echo "Building the wheel..." +python "$BIN_DIR/setup.py" bdist_wheel -v >/dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "Failed to build the wheel." + exit 1 +fi -# move the assets to root dir -cd ../../../ -mv assets ../assets +# Move the built wheel and the libs back to the root directory +mv "$BIN_DIR/dist/"*.whl "$BUILD_DIR" +mv "$BIN_DIR/libs" "$BUILD_DIR" +mv "$BIN_DIR/plugins.xml" "$BUILD_DIR/libs" -## install the whl -#cd .. -#pip install *.whl -#rm *.whl +# Move the assets to the root directory +mv "$SDK_DIR/assets" "$BUILD_DIR/assets" -cd ../ +# Deactivate and clean up the build virtual environment +echo "Deactivating and cleaning up virtual environment..." +deactivate +cd "$ROOT_DIR" || exit +rm -rf "$BUILD_DIR/venv" +rm -rf "$SDK_DIR" -# remove sdk -rm -R temp-sdk - -echo "UltimateALPR SDK built and setup successfully" -echo "You can now install the wheel using 'pip install ultimateAlprSdk-*.whl'" \ No newline at end of file +# Inform the user of the successful build +echo "UltimateALPR SDK built and setup successfully." +echo "You can now create a virtual environment, install the wheel and copy the assets and libs and start developing. Say 'y' to the next prompt to do this automatically (recommended)." +echo "Tip: Look at the assets folder as you might not need all the models depending on your platform/use case." +# Prompt user for auto setup choice +prompt_auto_setup diff --git a/plugins.xml b/plugins.xml deleted file mode 100644 index 1e8a563..0000000 --- a/plugins.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file