문서개요
Jetson Nano (jetpack version 4.6.1) 에서 mediapipe를 설치하고, USB 카메라를 사용하여 Hand Tracking 예제를 실행하는 과정을 서술합니다.
절차
tensorflow (GPU사용) 설치
mediapipe를 이용하기 위해서는 tensorflow가 설치되어야 합니다.
apt update
$ sudo apt update
종속 패키지 설치
$ sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev liblapack-dev libblas-dev gfortran $ sudo apt-get install python3-pip $ sudo pip3 install -U pip testresources setuptools $ sudo ln -s /usr/include/locale.h /usr/include/xlocale.h $ pip3 install Cython==0.29.36 $ pip3 install pkgconfig $ git clone https://github.com/h5py/h5py.git $ cd h5py $ H5PY_SETUP_REQUIRES=0 pip3 install . --no-deps --no-build-isolation $ sudo pip3 install -U numpy==1.19.4 future mock keras_preprocessing keras_applications gast==0.2.1 protobuf pybind11 packaging $ sudo pip3 install --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v461 tensorflow
tensorflow gpu 사용 확인
$ python3 >>> import tensorflow as tf >>> tf.config.list_physical_devices('GPU') [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] 또는 $ python3 >>> import tensorflow as tf >>> tf.test.is_gpu_available() True
위 결과처럼 나올 경우 GPU 사용
opencv 설치
swap 공간 할당
opencv 전체 빌드에는 약 8GB 이상의 램이 필요하며, Jetson nano는 ram이 4GB 이기 때문에 swap 공간을 할당해주어야 합니다.
dphys-swapfile 설치
$ sudo apt-get install dphys-swapfile
/sbin/dphys-swapfile 수정
$ sudo vi /sbin/dphys-swapfile CONF_SWAPSIZE=4096 CONF_SWAPFACTOR=2 CONF_MAXSWAP=4096
/etc/dphys-swapfile 주석 해제 및 수정
$ sudo vi /etc/dphys-swapfile CONF_SWAPSIZE=4096 CONF_SWAPFACTOR=2 CONF_MAXSWAP=4096
reboot 후 swap 확인
$ sudo reboot $ free -m
opencv 설치
openCV 4.1.1 삭제
$ sudo apt purge libopencv-dev libopencv-python libopencv-samples libopencv*
opencv가 남아있는지 확인
$ pkg-config --modversion opencv4
패키지 업데이트 및 필요한 패키지 설치
$ sudo apt update
$ sudo apt install -y python3-pip python-dev python3-dev python-numpy python3-numpy $ sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf" $ sudo apt install -y qt5-default $ sudo apt install -y build-essential cmake git unzip pkg-config libswscale-dev $ sudo apt install -y libcanberra-gtk* libgtk2.0-dev $ sudo apt install -y libtbb2 libtbb-dev libavresample-dev libvorbis-dev libxine2-dev $ sudo apt install -y curl
사진, 비디오 포맷 설치
$ sudo apt install -y libxvidcore-dev libx264-dev libgtk-3-dev $ sudo apt install -y libjpeg-dev libpng-dev libtiff-dev $ sudo apt install -y libmp3lame-dev libtheora-dev libfaac-dev libopencore-amrnb-dev $ sudo apt install -y libopencore-amrwb-dev libopenblas-dev libatlas-base-dev $ sudo apt install -y libblas-dev liblapack-dev libeigen3-dev libgflags-dev $ sudo apt install -y protobuf-compiler libprotobuf-dev libgoogle-glog-dev $ sudo apt install -y libavcodec-dev libavformat-dev gfortran libhdf5-dev $ sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev $ sudo apt install -y libv4l-dev v4l-utils qv4l2 v4l2ucp libdc1394-22-dev
opencv & contrib modules 설치 및 압축해제
# 현재 경로 : ~ $ curl -L https://github.com/opencv/opencv/archive/4.5.1.zip -o opencv-4.5.1.zip $ curl -L https://github.com/opencv/opencv_contrib/archive/4.5.1.zip -o opencv_contrib-4.5.1.zip
$ unzip opencv-4.5.1.zip $ unzip opencv_contrib-4.5.1.zip
build 폴더 생성 및 이동
# 현재 경로 : ~/opencv-4.5.1 $ mkdir build $ cd build
opencv와 contribs modules 빌드 (시간 많이 소요됨)
$ cmake -D WITH_CUDA=ON \ -D ENABLE_PRECOMPILED_HEADERS=OFF \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.1/modules \ -D WITH_GSTREAMER=ON \ -D WITH_LIBV4L=ON \ -D BUILD_opencv_python2=ON \ -D BUILD_opencv_python3=ON \ -D BUILD_TESTS=OFF \ -D BUILD_PERF_TESTS=OFF \ -D BUILD_EXAMPLES=OFF \ -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.1/modules \ -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \ -D CUDA_ARCH_BIN="7.2" \ -D CUDA_ARCH_PTX="" \ -D WITH_CUDNN=ON \ -D WITH_CUBLAS=ON \ -D ENABLE_FAST_MATH=ON \ -D CUDA_FAST_MATH=ON \ -D OPENCV_DNN_CUDA=ON \ -D ENABLE_NEON=ON \ -D WITH_QT=OFF \ -D WITH_OPENMP=ON \ -D WITH_OPENGL=ON \ -D BUILD_TIFF=ON \ -D WITH_FFMPEG=ON \ -D WITH_TBB=ON \ -D BUILD_TBB=ON \ -D WITH_EIGEN=ON \ -D WITH_V4L=ON \ -D OPENCV_ENABLE_NONFREE=ON \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D BUILD_NEW_PYTHON_SUPPORT=ON \ -D BUILD_opencv_python3=TRUE \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D BUILD_EXAMPLES=OFF ..
# 코어 개수에 따라 옵션을 주세요 # 예) make -j4 $ make $ sudo make install $ sudo ldconfig
swap 제거
swap 을 사용한 경우에만 이 과정을 따라해주세요.
$ sudo /etc/init.d/dphys-swapfile stop $ sudo apt-get remove --purge dphys-swapfile
mediapipe 설치
https://drive.google.com/file/d/1lHr9Krznst1ugLF_ElWGCNi_Y4AmEexx/view?usp=sharing 다운로드
$ sudo apt install unzip
$ unzip mediapipe-bin.zip $ cd mediapipe-bin $ sudo pip3 install numpy-1.19.4-cp36-none-manylinux2014_aarch64.whl mediapipe-0.8.5_cuda102-cp36-none-linux_aarch64.whl $ pip3 install dataclasses
mediapipe (HandTracking) 예제 실행
$ git clone https://github.com/Melvinsajith/How-to-Install-Mediapipe-in-Jetson-Nano.git $ cd How-to-Install-Mediapipe-in-Jetson-Nano
$ python3 Hand_counter.py 또는 $ python3 HandTrackingModule.py
참고사이트
https://github.com/Melvinsajith/How-to-Install-Mediapipe-in-Jetson-Nano