//
Search
▶️

Desktop에 TFLite와 OpenCV 설치하기

본 문서에서는 데스크탑에 TFLite와 OpenCV를 설치하는 방법을 안내합니다. Bazel과 Cmake를 이용하고 소스코드로 부터 직접 빌드합니다.

사전 준비

bazel

구글에서 만든 make, maven, gradle과 같은 오픈소스 빌드 및 테스트 툴
Java, C++ 뿐 만 아니라 Android, iOS 어플리케이션 개발에도 사용이 가능함

cmake

빌드파일을 생성해주는 프로그램(파일 빌드가 아님)
CMakeLists.txt에 필요한 부분을 작성해주면 간단하게 빌드파일을 생성 할 수 있음
주요함수
set
include_directories
message
find_package
link_directories
add_executable
target_link_libraries

Tensorflow Lite 설치하기

bazel을 활용해 tensorflow github에서 직접 소스를 빌드하여 설치합니다. configure에 대한 값은 아래 참고 링크를 보고 입력해주세요.
TF_VERSION="2.9.0" git clone -b v${TF_VERSION} --single-branch https://github.com/tensorflow/tensorflow.git && cd tensorflow ./configure
Bash
복사
./configure 를 입력하면 아래와 같이 나옵니다. 아래 옵션을 참고해서 설정을 완료해주세요. 파이썬 경로와 라이브러리 경로는 디폴트 값과 다를 수 있으니 필히 확인 후 입력해주시고 한글이름의 폴더의 경우에 잘 안될 수 있으니 해당 부분도 참고해주세요.
You have bazel 5.0.0 installed. Please specify the location of python. [Default is /usr/local/opt/python@3.9/bin/python3.9]: /usr/local/bin/python3 Found possible Python library paths: /usr/local/Cellar/python@3.9/3.9.1_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages Please input the desired Python library path to use. Default is [/usr/local/Cellar/python@3.9/3.9.1_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages] /usr/local/lib/python3.9/site-packages Do you wish to build TensorFlow with ROCm support? [y/N]: N No ROCm support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: N No CUDA support will be enabled for TensorFlow. Do you wish to download a fresh release of clang? (Experimental) [y/N]: N Clang will not be downloaded. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]: -D_GLIBCXX_USE_CXX11_ABI=0 -O3 Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: N Not configuring the WORKSPACE for Android builds. Do you wish to build TensorFlow with iOS support? [y/N]: N No iOS support will be enabled for TensorFlow. Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL). --config=monolithic # Config for mostly static monolithic build. --config=numa # Build with NUMA support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. --config=v1 # Build with TensorFlow 1 API instead of TF 2 API. Preconfigured Bazel build configs to DISABLE default on features: --config=nogcp # Disable GCP support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished
C++
복사
# 빌드하기 (CPU 버전) bazel build -c opt //tensorflow/lite/c:tensorflowlite_c
Bash
복사
빌드가 완료되면 만들어진 라이브러리 파일(예시: tensorflowlite_c.dylib)을 적당한 위치(lib)에 이동하여 사용합니다. 라이브러리 파일은 OS 별로 확장자가 다르니 참고!
특히 경로를 입력할때는 해당 경로가 맞는지 꼭 확인 후 입력합니다.

OpenCV 설치하기

cmake를 활용해 opencv github에서 직접 소스를 빌드하여 설치합니다.
OPENCV_VERSION="4.5.0" git clone https://github.com/opencv/opencv.git && cd opencv git checkout tags/${OPENCV_VERSION} cd .. mkdir install build_opencv cd build_opencv cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=../install \ -D INSTALL_C_EXAMPLES=ON \ -D BUILD_EXAMPLES=ON ../opencv export CPUS=$(sysctl -n hw.physicalcpu) make -j $CPUS make install
Bash
복사
빌드가 완료되면 마찬가지로 만들어진 라이브러리 파일을 적당한 위치(lib)에 이동하여 사용합니다. 라이브러리 파일은 OS 별로 확장자가 다르니 참고!

CMakeLists.txt

빌드 후 필요한 라이브러리 파일과 헤더파일을 이동하고 CMakeLists.txt 파일을 작성합니다. 예시는 아래와 같습니다.
set은 변수에 값을 넣는 함수입니다. 프로그래밍 언어의 =와 유사합니다.
include_directories는 헤더파일을 연결하는 함수입니다.
message는 콘솔창에 출력하는 함수입니다. 프로그래밍 언어의 print함수와 유사합니다.
link_directories는 라이브러리 파일을 연결하는 함수 입니다.
add_library는 라이브러리를 등록하는 함수입니다.
add_executable는 실행파일 빌드를 위한 함수입니다. 실행파일 이름과 소스파일을 작성합니다.
target_link_libraries는 포함할 라이브러리 목록을 넣는 함수입니다.
cmake_minimum_required(VERSION 3.20) project(TFLiteInferenceExample) set(CMAKE_CXX_STANDARD 14) set(LIB_ROOT "${CMAKE_SOURCE_DIR}/lib") set(TFLITE_ROOT "${LIB_ROOT}/tflite") set(OPENCV_ROOT "${LIB_ROOT}/opencv") set(TFLITE_INCLUDE_DIR "${TFLITE_ROOT}/include") set(OPENCV_INCLUDE_DIR "${OPENCV_ROOT}/include/opencv4") set(OPENCV_LIBRARY_DIR "${OPENCV_ROOT}/lib") include_directories(${CMAKE_SOURCE_DIR}/include) include_directories(${TFLITE_INCLUDE_DIR}) include_directories(${OPENCV_INCLUDE_DIR}) message(STATUS " # ----------------------------------------------- CMAKE_SOURCE_DIR : ${CMAKE_SOURCE_DIR} TFLITE_INCLUDE_DIR : ${TFLITE_INCLUDE_DIR} # ----------------------------------------------- ") find_package( OpenCV REQUIRED ) link_directories(${OPENCV_LIBRARY_DIR}) add_library(tensorflowlite_c SHARED IMPORTED) set_property(TARGET tensorflowlite_c PROPERTY IMPORTED_LOCATION ${TFLITE_ROOT}/lib/libtensorflowlite_c.dylib) add_executable( TFLiteInferenceExample tests/main.cpp src/engine_tflite.cpp src/transform.cpp src/utils.cpp ) target_link_libraries( TFLiteInferenceExample tensorflowlite_c ${OpenCV_LIBS} )
Plain Text
복사
최종 디렉토리 예시

참고