Artificial intelligence on a single board computer

With the new single board computer „Jetson Nano“ from nvidia it is possible to make this little thing really „see“ and detect things.

This 99.00 $ sbc is able to recognise the real world and process the things it sees in nearly realtime. For such a little thing it is astonishing what you can do nowadays: only with a dirty cheap usb webcam and a more dirty cheap computer running on 5V.

What you can see here are some examples from the open source Computer Vision software OpenCV version 4.1.1 written in python exectuted directly on the Jetson Nano. The usb camera is watching TV (soccer, of course :-))

On the monitor connected to the Jetson Nano via HDMI you can see the images being processed by OpenCV 4.1.1. The samples can be found in the (… surprise, surprise) opencv’s samples/python folder.

If you are interested in how to get OpenCV 4.1.1 installed on the Nano: Here we go

#prepare jetson nano
 sudo apt update
 sudo apt upgrade
 sudo apt full-upgrade --autoremove
# get rid of old installation
 sudo apt-get purge *libopencv*
# install everything we need for compile
 sudo apt install -y \
         build-essential \
         curl \
         git \
         gfortran \
         liblapacke-dev \
         libavcodec-dev \
         libavformat-dev \
         libavresample-dev \
         libcaffe-cpu-dev \
         libdc1394-22-dev \
         libgflags-dev \
         libgoogle-glog-dev \
         libgstreamer1.0-dev \
         libgstreamer-plugins-base1.0-dev \
         libgtk2.0-dev \
         libgphoto2-dev \
         libjpeg-dev \
         libpng-dev \
         libtheora-dev \
         libmp3lame-dev \
         libswscale-dev \
         libtbb-dev \
         libtbb2 \
         libtiff-dev \
         libv4l-dev \
         libavcodec-dev \
         libavformat-dev \
         libhdf5-dev \
         libxvidcore-dev \
         libx264-dev \
         libatlas-base-dev \
         libblas-dev \
         libcurl4-gnutls-dev \
         liblapack-dev \
         libeigen3-dev \
         libswscale-dev \
         libprotobuf-c-dev \
         libprotobuf-dev \
         libprotoc-dev \
         protobuf-compiler \
         pkg-config \
         python-dev \
         python-numpy \
         python-pip \
         python3-pip \
         python-tk \
         python3-tk \
         python3-numpy \
         python2.7-dev \
         python3-dev \
         qt5-qmake \
         qttools5-dev-tools \
         qt5-default \
         libcurl4-openssl-dev \
         libssl-dev \
         libeigen-stl-containers-dev \
         python-qt4-dev \
 libtesseract-dev 

 #prepare for installing a recent version of cmake
 #install cmake from source
 cd ~
 sudo apt remove  --purge cmake
 sudo apt install libcurl4-openssl-dev 
 curl -L https://github.com/Kitware/CMake/releases/download/v3.15.2/cmake-3.15.2.tar.gz -o cmake-3.15.2.tar.gz
 tar xf cmake-3.15.2.tar.gz
 cd cmake-3.15.2
 ./bootstrap --prefix=/usr --parallel=j$(nproc) 
  make -j$(nproc) 
  sudo make install
# test (optional)
 cmake --version
# cmake version 3.15.2
# CMake suite maintained and supported by Kitware (kitware.com/cmake).
 curl -L https://bootstrap.pypa.io/get-pip.py -o get-pip.py
 sudo -H pip install virtualenv virtualenvwrapper
 remove old cache
 sudo rm -rf ~/.cache/pip
# virtualenv and virtualenvwrapper
 export WORKON_HOME=$HOME/.virtualenvs
# we use python3 as a standard in virtual environment
 export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
 source /usr/local/bin/virtualenvwrapper.sh
 export VIRTUALENVWRAPPER_PYTHON3=/usr/bin/python3
 source /usr/local/bin/virtualenvwrapper.sh
# setting this permanently
 echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
 echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
 echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python" >> ~/.bashrc
 echo "export VIRTUALENVWRAPPER_PYTHON3=/usr/bin/python3" >> ~/.bashrc
 echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
# create a virtual environment
 cd ~
 mkvirtualenv cv_python3 -p python3
 mkvirtualenv cv_python2 -p python
# this should give you
# Already using interpreter /usr/bin/python3
# Using base prefix '/usr'
# New python executable in /home//.virtualenvs/cv/bin/python3
# Also creating executable in /home//.virtualenvs/cv/bin/python
# Installing setuptools, pip, wheel…
# done.
# virtualenvwrapper.user_scripts creating /home//.virtualenvs/cv/bin /predeactivate
# virtualenvwrapper.user_scripts creating /home//.virtualenvs/cv/bin/postdeactivate
# virtualenvwrapper.user_scripts creating /home//.virtualenvs/cv/bin/preactivate
# virtualenvwrapper.user_scripts creating /home/.virtualenvs/cv/bin/postactivate
# virtualenvwrapper.user_scripts creating /home//.virtualenvs/cv/bin/get_env_details
# (cv) @jetson:~/opencvComplete/opencv-4.1.1/release
# the "(cv) in fromt of @jetson is the important one. If you do not see #this yet, try it with
 workon cv
# if you want to leave the virtual environment, simple do it with
# "deactivate" + ctrl
(cv) … pip install numpy
# we need MORE MEMORY for the build
# check memory:
 sudo swapon --show
# on a jetson nano it gives you
# bNAME       TYPE        SIZE USED PRIO
# /dev/zram0 partition 494,6M   0B    5
# /dev/zram1 partition 494,6M   0B    5
# /dev/zram2 partition 494,6M   0B    5
# /dev/zram3 partition 494,6M   0B    5
# create a swap file - for building opencv4.1.1 3G additional swap were tested o.k.
# allocate the file:
 sudo fallocate -l 3G /swapfile

# only root should have access to this file:
 sudo chmod 600 /swapfile
# create the file->
 sudo mkswap /swapfile
# activate the swap file ->
 sudo swapon /swapfile 
# test if everything is o.k.->
 #
 sudo swapon --show
 # another test ->
 sudo free -h
 # adjust swapiness - first lets see what we have now ->
 cat /proc/sys/vm/swappiness
 # set swapiness to a value of  e.g. 10 -> (for me the default was o.k.)
 sudo sysctl vm.swappiness=10
 # do not forget to remove the swap file after compiling when you are 
 # working on an SD-card!
 # deactivate it with ->
 sudo swapoff -v /swapfile
  
 # remove it ->
 sudo rm /swapfile
 # protobuf -fix
 # If you have protobuf installed, then configure your build with 
 # -BUILD_PROTOBUF=OFF and -WITH_PROTOBUF=ON
 # If you do NOT have protobuf installed, just ensure you have    #-BUILD_PROTOBUF=ON
# It worked for me with OpenCV 4.1.1 on Ubuntu 18.4.3 with CUDA 10.0
#  get the sources
 cd ~
 curl -L https://github.com/opencv/opencv/archive/4.1.1.zip -o opencv-4.1.1.zip
 curl -L https://github.com/opencv/opencv_contrib/archive/4.1.1.zip -o opencv_contrib-4.1.1.zip
 unzip opencv-4.1.1.zip 
 unzip opencv_contrib-4.1.1.zip 
 cd opencv-4.1.1/
# prepare build folder
 mkdir release && cd release
# fix eigen bug (important to get eigen running)
 sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen
 cmake --system-curl -D CMAKE_BUILD_TYPE=RELEASE \
       -D CMAKE_INSTALL_PREFIX=/usr/local \
       -D CMAKE_BUILD_TYPE=RELEASE \
       -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.1/modules \
       -D OPENCV_ENABLE_NONFREE=ON \
        D ENABLE_PRECOMPILED_HEADERS=OFF \
       -D WITH_CUDA=ON \
       -D CUDA_ARCH_BIN="5.3" \
       -D CUDA_ARCH_PTX="" \
       -D CUDA_FAST_MATH=1 \
       -D BUILD_PERF_TESTS=OFF \
       -D BUILD_TESTS=OFF \
       -D BUILD_DOCS=OFF \
       -D WITH_JASPER=OFF \
       -D BUILD_EXAMPLES=ON \
       -D INSTALL_PYTHON_EXAMPLES=ON \
       -D INSTALL_C_EXAMPLES=OFF \
       -D INSTALL_PYTHON_EXAMPLES=OFF \
       -D WITH_TBB=ON \
       -D WITH_HDF=ON \
       -D WITH_OPENMP=ON \
       -D WITH_JAVA=OFF \
       -D WITH_GSTREAMER=ON \
       -D WITH_LIBV4L=ON \
       -D WITH_EIGEN3=ON \
       -D PYTHON2_INCLUDE_PATH=/usr/include/python2.7 \
       -D PYTHON2_LIBRARIES=/usr/lib/python2.7/config-aarch64-linux-gnu/libpython2.7.so  \
       -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include \
       -D PYTHON3_INCLUDE_PATH=/usr/include/python3.6 \
       -D PYTHON3_LIBRARIES=/usr/lib/python3.6/config-3.6m-aarch64-linux-gnu/libpython3.6.so \
       -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \
       -D WITH_QT=ON \
       -D BUILD_OPENCV_PYTHON2=ON \
       -D BUILD_OPENCV_PYTHON3=ON \
       -D PYTHON2_EXECUTABLE=/usr/bin/python2.7 \
       -D PYTHON_EXECUTABLE=/usr/bin/python \
       -D BUILD_WITH_DEBUG_INFO=OFF      \
       -Wno-dev \
       -Wno-undef \
       -Wno-strict-aliasing \
 ..

 make -j$(nproc)
 sudo make -j$(nproc) install
 cd /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/
 ln -s cv2.so cv2.cpython-36m-aarch64-linux-gnu.so
 sudo ldconfig
 sudo swapon --show
 sudo swapoff -v /swapfile
 sudo swapon --show
 sudo rm /swapfile
 workon cv_python3
 (cv) pip install numpy -vv

Have fun.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert