0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ncnnでsegment-anythingを動かしてみる

Last updated at Posted at 2023-12-20

概要

ncnnで、領域分割を行う、segment-anythingを動かしてみる。

Screenshot from 2023-11-19 11-04-06.png

環境構築

構築手順に入る前に、まずPCに以下のバージョンのUbuntuをインストールした環境で、構築しました。

M5UnitV2のクロスコンパイル環境を構築する手順

Ubuntu 22.04.3 LTS(x64)
OpenCV ver 0.4.4
ncnn ver 231027

segment-anything-ncnnのダウンロード

GitHubからsegment-anything-ncnnのコードをダウンロードします。

$ git clone https://github.com/FeiGeChuanShu/segment-anything-ncnn.git

$ cd segment-anything-ncnn

CMakeLists.txt ファイルの作成

cmakeでコンパイルするために、CMakeLists.txtを作成します。

gedit CMakeLists.txt
CMakeLists.txt
SET(CMAKE_BUILD_TYPE "Release")

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

if(TARGET_COMPILER STREQUAL "arm")
    message(STATUS "TARGET_COMPILE STREQUAL arm")
    SET(CMAKE_CXX_COMPILER arm-none-linux-gnueabihf-g++)
    SET(CMAKE_C_COMPILER arm-none-linux-gnueabihf-gcc)
    SET(NCNN_INSTALL_DIR /opt/external/ncnn/install/arm/)
    SET(ZBAR_INSTALL_DIR /opt/external/zbar-0.10/build/arm)
    SET(OpenCV_DIR /opt/external/opencv/build/arm)
else()
    message(STATUS "TARGET_COMPILE STREQUAL X64")
    SET(NCNN_INSTALL_DIR /opt/external/ncnn/install/x64/)
    SET(ZBAR_INSTALL_DIR /opt/external/zbar-0.10/build/x64/)
    SET(OpenCV_DIR /opt/external/opencv/build/x64) 
endif()


PROJECT(ncnn_sam)
cmake_minimum_required(VERSION 3.5)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

if(OpenCV_FOUND)
 message(OPENCV_VERSION)
 message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")
 message(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
endif(OpenCV_FOUND)

find_package(OpenCV REQUIRED)

link_directories(${OpenCV_LIBRARY_DIRS})

include_directories(${OpenCV_INCLUDE_DIRS} )
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${NCNN_INSTALL_DIR}/include/ncnn)

add_executable(ncnn_sam src/main.cpp src/segment_anything.cpp src/pipeline.cpp)
target_link_libraries(ncnn_sam ${OpenCV_LIBRARIES})
target_link_libraries(ncnn_sam ${NCNN_INSTALL_DIR}/lib/libncnn.a -fopenmp)

学習モデルのダウンロード

segment-anything-ncnnのGitHubのリンクから、学習モデルをダウンロードします。

Screenshot from 2023-11-19 11-08-36.png

modelsフォルダに学習モデルを格納します。

$ ls models/
decoder.bin    encoder-einsum.bin    encoder-matmul.bin
decoder.param  encoder-einsum.param  encoder-matmul.param

ビルド

プログラムをコンパイルして実行します。

$ cmake .
$ make
$ cd bin
$ ./ncnn_sam
start image encoder...
finish image encoder...
automatic mask
processing: 0/32
processing: 1/32
processing: 2/32
processing: 3/32
~~~
processing: 31/32
processing: 32/32

参考資料

この記事を作成するにあたり、以下のウェブサイトを参考にしました。

FeiGeChuanShu/segment-anything-ncnn

facebookresearch/segment-anything

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?