ShiqiYu / libfacedetection

An open source library for face detection in images. The face detection speed can reach 1000FPS.
Other
12.26k stars 3.05k forks source link

how to build on armv7l raspberry pi 4b raspbian buster - code share #355

Open zoldaten opened 1 year ago

zoldaten commented 1 year ago

i`ve just successfully built library and examples on raspberry pi. so i would like to share instructions to reproduce:

 git clone https://github.com/ShiqiYu/libfacedetection.git

cd libfacedetection
mkdir build && cd build

cmake -DENABLE_NEON=ON -DCMAKE_BUILD_TYPE=RELEASE -DUSE_OPENMP=OFF -DENABLE_AVX2=OFF -DDEMO=OFF -DCMAKE_CXX_FLAGS:STRING="-march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a72 -Wno-psabi" -DCMAKE_C_FLAGS:STRING="-march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a72 -Wno-psabi" -DCMAKE_ASM_FLAGS:STRING="-march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a72 -g" ..

*Replace -mtune=cortex-a15 with -mtune=cortex-a53 for RPi3 and -mtune=cortex-a72 for RPi4.

make
sudo make install

cd ..
cd example

nano CMakeLists.txt
cmake_minimum_required( VERSION 2.8 )
project( example )
find_package( OpenCV REQUIRED )
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( detect-image detect-image.cpp )
add_executable( detect-camera detect-camera.cpp )
target_link_libraries( detect-image ${OpenCV_LIBS} )
#target_link_libraries( detect-image /libfacedetection/build/install/lib/libfacedetection.so )
target_link_libraries( detect-image /home/al/Desktop/projects/libfacedetection/build/install/lib/libfacedetection.so )
#target_link_libraries( detect-image /opencv/build/lib/libopencv_highgui.a )
target_link_libraries( detect-image /usr/local/lib/libopencv_highgui.so )
target_link_libraries( detect-image /usr/local/lib/libopencv_imgproc.so )
target_link_libraries( detect-image /usr/local/lib/libopencv_core.so )
target_link_libraries( detect-image /usr/local/lib/libopencv_imgcodecs.so )
target_link_libraries( detect-camera ${OpenCV_LIBS} )
#target_link_libraries( detect-camera /libfacedetection/build/install/lib/libfacedetection.so )
target_link_libraries( detect-camera /home/al/Desktop/projects/libfacedetection/build/install/lib/libfacedetection.a )
target_link_libraries( detect-camera /usr/local/lib/libopencv_highgui.so )
target_link_libraries( detect-camera /usr/local/lib/libopencv_video.so )
target_link_libraries( detect-camera /usr/local/lib/libopencv_imgproc.so )
target_link_libraries( detect-camera /usr/local/lib/libopencv_core.so )
target_link_libraries( detect-camera /usr/local/lib/libopencv_videoio.so )

nano detect-camera.cpp
    /home/pi/libfacedetection/src/facedetectcnn.h
nano detect-image.cpp
    /home/pi/libfacedetection/src/facedetectcnn.h

nano /home/pi/libfacedetection/src/facedetectcnn.h
    /home/pi/libfacedetection/build/facedetection_export.h
mkdir build
cd  build
cmake ..
make

./detect-image image.jpg

it works but i dont think fast enough:

./detect-image img_640x480.jpg 
time = 607.191ms
2 faces detected.
face 0: confidence=91, [171, 265, 35, 48] (179,285) (195,285) (186,295) (181,301) (193,302)
face 1: confidence=34, [446, 207, 56, 67] (460,230) (481,230) (467,241) (461,254) (477,252)

is it good result for edge device ?

fengyuentau commented 1 year ago

Please take a look at the table we have in readme: https://github.com/ShiqiYu/libfacedetection#cnn-based-face-detection-on-arm-linux-raspberry-pi-4-b.


time = 607.191ms

Your data is close to what we have (404.63ms) in single-threaded mode.


-DUSE_OPENMP=OFF

You turned off openmp for some reason, which damaged the performance.

zoldaten commented 1 year ago

if i use -DUSE_OPENMP=ON i cant compile examples:

 pi@raspberrypi:~/libfacedetection/example/build $ make
[ 25%] Linking CXX executable detect-image
/usr/bin/ld: /usr/local/lib/libfacedetection.a(facedetectcnn.cpp.o): undefined reference to symbol 'omp_get_num_threads@@OMP_1.0'
/usr/bin/ld: //usr/lib/arm-linux-gnueabihf/libgomp.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/detect-image.dir/build.make:137: detect-image] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/detect-image.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
zoldaten commented 1 year ago

ok. fixed that. correct CMakeLists.txt for examples:

cmake_minimum_required( VERSION 2.8 )
project( example )
find_package( OpenCV REQUIRED )
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
include_directories( ${OpenCV_INCLUDE_DIRS} )
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
add_executable( detect-image detect-image.cpp )
add_executable( detect-camera detect-camera.cpp )
target_link_libraries( detect-image ${OpenCV_LIBS} )
#target_link_libraries( detect-image /libfacedetection/build/install/lib/libfacedetection.so )
target_link_libraries( detect-image /usr/local/lib/aarch64-linux-gnu/libfacedetection.a )
#target_link_libraries( detect-image /opencv/build/lib/libopencv_highgui.a )
target_link_libraries( detect-image /usr/local/lib/aarch64-linux-gnu/libopencv_highgui.so )
target_link_libraries( detect-image /usr/local/lib/aarch64-linux-gnu/libopencv_imgproc.so )
target_link_libraries( detect-image /usr/local/lib/aarch64-linux-gnu/libopencv_core.so )
target_link_libraries( detect-image /usr/local/lib/aarch64-linux-gnu/libopencv_imgcodecs.so )
target_link_libraries( detect-camera ${OpenCV_LIBS} )
#target_link_libraries( detect-camera /libfacedetection/build/install/lib/libfacedetection.so )
target_link_libraries( detect-camera /usr/local/lib/aarch64-linux-gnu/libfacedetection.a )
target_link_libraries( detect-camera /usr/local/lib/aarch64-linux-gnu/libopencv_highgui.so )
target_link_libraries( detect-camera /usr/local/lib/aarch64-linux-gnu/libopencv_video.so )
target_link_libraries( detect-camera /usr/local/lib/aarch64-linux-gnu/libopencv_imgproc.so )
target_link_libraries( detect-camera /usr/local/lib/aarch64-linux-gnu/libopencv_core.so )
target_link_libraries( detect-camera /usr/local/lib/aarch64-linux-gnu/libopencv_videoio.so )

*i also tested on aarch64 (Raspbian bullseye). here is the line to build the main library (not examples): cmake -DENABLE_NEON=ON -DCMAKE_BUILD_TYPE=RELEASE -DUSE_OPENMP=ON -DENABLE_AVX2=OFF -DDEMO=OFF -DCMAKE_CXX_FLAGS:STRING="-march=armv8-a -mtune=cortex-a72 -Wno-psabi" -DCMAKE_C_FLAGS:STRING="-march=armv8-a -mtune=cortex-a72 -Wno-psabi" -DCMAKE_ASM_FLAGS:STRING="-march=armv8-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a72 -g" ../

so. with OPENMP enabled i got time = 249.999ms for 640x480 image.

have no idea how you got better results...