Настройка Dlib для Android studio - PullRequest
1 голос
/ 05 марта 2020

Я пытаюсь настроить dlib в android студии в windows, следуя этому уроку, https://github.com/Luca96/dlib-for-android. Я изменил путь к различным переменным, но получил ту же ошибку. Мне не нужен opencv просто dlib. Opencv уже настроен и работает правильно.

Я заменил -GNinja на -DCMAKE_MAKE_PROGRAM=C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe и все еще получаю ошибку.

Любая альтернативная процедура для установки dlib с android studio будет также работает.

Мой пользовательский сценарий Power Shell на основе предоставленного образца:

# Luca Anzalone

# -----------------------------------------------------------------------------
# -- DLIB FOR ANDROID: setup script
# -----------------------------------------------------------------------------

# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$AndroidCmake = 'C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe'

# Android-ndk path: REPLACE WITH YOUR NDK PATH!
if (Get-Variable 'ANDROID_NDK' -Scope Global -ErrorAction 'Ignore') {
    $NDK = $ANDROID_NDK
} else {
    $NDK = 'C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669'
}

$TOOLCHAIN = "C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669\build\cmake\android.toolchain.cmake"

# Supported Android ABI: TAKE ONLY WHAT YOU NEED!
# $ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
$ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'

# path to stripper tool: REPLACE WITH YOURS, ACCORDING TO OS!!
$STRIPPER_PATH = "$NDK\toolchains\llvm\prebuilt\windows-x86_64\bin"
$STRIPPERS = @{
    'armeabi-v7a' = "$STRIPPER_PATH\arm-linux-androideabi-strip.exe";
    'arm64-v8a'   = "$STRIPPER_PATH\aarch64-linux-android-strip.exe";
    'x86'         = "$STRIPPER_PATH\x86_64-linux-android-strip.exe" ;
    'x86_64'      = "$STRIPPER_PATH\x86_64-linux-android-strip.exe" ;    
} 

# Minimum supported sdk: SHOULD BE GREATER THAN 16
$MIN_SDK = 24

# Android project path: REPLACE WITH YOUR PROJECT PATH!
$PROJECT_PATH = 'C:\Users\computer\AndroidStudioProjects\OpencvPose'

# Directory for storing native libraries
$NATIVE_DIR = "$PROJECT_PATH\app\src\main\cppLibs"

# -----------------------------------------------------------------------------
# -- Utils
# -----------------------------------------------------------------------------
function Make-Dir {
    if (-Not (Test-Path $args[0])) {
        New-Item -ItemType directory -Path $args[0] > $null
    }
}

function Copy-Directory ($from, $to, $tab) {
    $Items = Get-ChildItem $from

    ForEach ($item in $Items) {
        if ((Get-Item $item.FullName) -is [System.IO.DirectoryInfo]) {
            Write-Host "$tab > $item"
            Make-Dir "$to\$item"
            Copy-Directory "$from\$item" "$to\$item" "$tab  "
        } else {
            Copy-Item -Path $item.FullName -Destination $to
            Write-Host "$tab | $item copied." 
        }
    }
}

# -----------------------------------------------------------------------------
# -- Dlib
# ----------------------------------------------------------------------------- 

# Dlib library path:
$DLIB_PATH = 'C:\Users\computer\Desktop\dlib-19.19'

function Compile-Dlib {
    Set-Location $DLIB_PATH
    Make-Dir 'build'

    Write-Host '=> Compiling Dlib...'
    sleep 0.5

    ForEach ($abi in $ABIs) {
        Write-Host
        Write-Host "=> Compiling Dlib for ABI: '$abi'..."
        sleep 0.5

        Make-Dir "build\$abi"
        Set-Location "build\$abi"

        $cmakeArguments = @(
                "-DBUILD_SHARED_LIBS=1",
                "-DANDROID_NDK=$NDK",
                "-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN",
                "-DCMAKE_MAKE_PROGRAM=C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe",
                "-DCMAKE_BUILD_TYPE=Release",
                "-DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions",
                "-DCMAKE_C_FLAGS=-O3",
                "-DANDROID_ABI=$abi",
                "-DANDROID_PLATFORM=android-$MIN_SDK",
                "-DANDROID_TOOLCHAIN=clang",
                "-DANDROID_STL=c++_shared",
                "-DANDROID_CPP_FEATURES=rtti exceptions",
                "-DCMAKE_PREFIX_PATH=..\..\",
                "..\..\"
        )

        $stripArguments = @(
                "--strip-unneeded",
                "dlib/libdlib.so"
        )

        & $AndroidCmake $cmakeArguments
        sleep 0.5

        Write-Host "=> Generating the 'dlib/libdlib.so' for ABI: '$abi'..."
        & $AndroidCmake --build .
        sleep 0.5

        Write-Host "=> Stripping libdlib.so for ABI: '$abi'to reduce space..."
        & $STRIPPERS[$abi] $stripArguments
        sleep 0.5

        Write-Host '=> done.'
        Set-Location ..\..\
        sleep 0.5
    }    
}

function Dlib-Setup {
    Write-Host '=> Making directories for Dlib...'
    Make-Dir "$NATIVE_DIR\dlib"
    Write-Host "=> '$NATIVE_DIR\dlib' created."
    Make-Dir "$NATIVE_DIR\dlib\lib"
    Write-Host "=> '$NATIVE_DIR\dlib\lib' created."
    Make-Dir "$NATIVE_DIR\dlib\include"
    Write-Host "=> '$NATIVE_DIR\dlib\include' created."
    Make-Dir "$NATIVE_DIR\dlib\include\dlib"
    Write-Host "=> '$NATIVE_DIR\dlib\include\dlib' created."

    Write-Host "=> Copying Dlib headers..."
    Copy-Directory "$DLIB_PATH\dlib" "$NATIVE_DIR\dlib\include\dlib" ''

    Write-Host "=> Copying 'libdlib.so' for each ABI..."
    ForEach ($abi in $ABIs) {
        Make-Dir "$NATIVE_DIR\dlib\lib\$abi"
        Copy-Item -Path "$DLIB_PATH\build\$abi\dlib\libdlib.so" -Destination "$NATIVE_DIR\dlib\lib\$abi"
        Write-Host " > Copied libdlib.so for $abi"
    }
}

# COMMENT TO DISABLE COMPILATION
Compile-Dlib

# -----------------------------------------------------------------------------
# -- OpenCV
# -----------------------------------------------------------------------------

# OpenCV library path: REPLACE WITH YOUR OPENCV PATH!
$OPENCV_PATH='path-to-your\opencv-4.0.1-android-sdk\sdk\native'

function Opencv-Setup {
    Make-Dir "$NATIVE_DIR\opencv"

    Write-Host "=> Copying 'libopencv_java4.so' for each ABI..."
    ForEach ($abi in $ABIs) {
        Make-Dir "$NATIVE_DIR\opencv\$abi"
        Copy-Item -Path "$OPENCV_PATH\libs\$abi\libopencv_java4.so" -Destination "$NATIVE_DIR\opencv\$abi"
        Write-Host " > Copied libopencv_java4.so for $abi"
    }
}

# -----------------------------------------------------------------------------
# -- Project setup
# -----------------------------------------------------------------------------

Make-Dir $NATIVE_DIR

# COMMENT TO NOT COPY DLIB '.so' FILES
Dlib-Setup

# COMMENT TO NOT COPY OPENCV '.so' FILES
Opencv-Setup

Write-Host "=> Project configuration completed."

# -----------------------------------------------------------------------------

Журнал частичных ошибок:

PS C:\Users\computer\Desktop\dlib-19.19\build> & "C:\Users\computer\Desktop\setup1.ps1"
=> Compiling Dlib...

=> Compiling Dlib for ABI: 'armeabi-v7a'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/armeabi-v7a/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'armeabi-v7a'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'armeabi-v7a'to reduce space...
C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\arm-linux-androideabi-strip.exe: 'dlib/libdlib.so': No such file
=> done.

=> Compiling Dlib for ABI: 'arm64-v8a'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/arm64-v8a/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'arm64-v8a'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'arm64-v8a'to reduce space...
C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android-strip.exe: 'dlib/libdlib.so': No such file
=> done.

=> Compiling Dlib for ABI: 'x86'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/x86/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'x86'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'x86'to reduce space...
C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\x86_64-linux-android-strip.exe: 'dlib/libdlib.so': No such file
=> done.

=> Compiling Dlib for ABI: 'x86_64'...
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
  CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio
  Edition' is not installed.


-- Configuring incomplete, errors occurred!
See also "C:/Users/computer/Desktop/dlib-19.19/build/x86_64/CMakeFiles/CMakeOutput.log".
=> Generating the 'dlib/libdlib.so' for ABI: 'x86_64'...
ninja: error: loading 'build.ninja': The system cannot find the file specified.

=> Stripping libdlib.so for ABI: 'x86_64'to reduce space...
C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\x86_64-linux-android-strip.exe: 'dlib/libdlib.so': No such file
=> done.
=> Making directories for Dlib...
=> 'C:\Users\computer\AndroidStudioProjects\OpencvPose\app\src\main\cppLibs\dlib' created.
=> 'C:\Users\computer\AndroidStudioProjects\OpencvPose\app\src\main\cppLibs\dlib\lib' created.
=> 'C:\Users\computer\AndroidStudioProjects\OpencvPose\app\src\main\cppLibs\dlib\include' created.
=> 'C:\Users\computer\AndroidStudioProjects\OpencvPose\app\src\main\cppLibs\dlib\include\dlib' created.

1 Ответ

2 голосов
/ 05 марта 2020

РЕДАКТИРОВАТЬ: Используйте мастер DLIB из Github вместо 19.19 с их веб-сайта, чтобы избежать ошибок с OpenCV. Измените пути соответственно, установите dlib-19.19 на dlib-master, где это применимо.

Я смог скомпилировать dlib, внеся некоторые изменения в путь и флаги. Большим изменением стало включение обоих флагов:

-GNinja -DCMAKE_MAKE_PROGRAM="C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe"

Я извлек последнюю dlib(19.19) на рабочий стол, а также настроил cmake, NDK, lldb. Компиляция только для x86_64,

C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe -DBUILD_SHARED_LIBS=1 -DCMAKE_TOOLCHAIN_FILE="C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669\build\cmake\android.toolchain.cmake" -DANDROID_NDK="C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669" -GNinja -DCMAKE_MAKE_PROGRAM="C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions -DANDROID_ABI=x86_64 -DANDROID_PLATFORM=android-24 -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_shared -DANDROID_CPP_FEATURES=rtti exceptions "C:\Users\computer\Desktop\dlib-19.19\CMakeLists.txt"

Запустите указанный выше код, вставив его в Power Shell. Он сгенерирует libdlib.so в папке dlib.

Сценарий компиляции:

# Luca Anzalone

# -----------------------------------------------------------------------------
# -- DLIB FOR ANDROID: setup script
# -----------------------------------------------------------------------------

# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$AndroidCmake = 'C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe'

# Android-ndk path: REPLACE WITH YOUR NDK PATH!
if (Get-Variable 'ANDROID_NDK' -Scope Global -ErrorAction 'Ignore') {
    $NDK = $ANDROID_NDK
} else {
    $NDK = 'C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669'
}

$TOOLCHAIN = "C:\Users\computer\AppData\Local\Android\Sdk\ndk\21.0.6113669\build\cmake\android.toolchain.cmake"

# Supported Android ABI: TAKE ONLY WHAT YOU NEED!
# $ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
$ABIs = 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'

# path to stripper tool: REPLACE WITH YOURS, ACCORDING TO OS!!
$STRIPPER_PATH = "$NDK\toolchains\llvm\prebuilt\windows-x86_64\bin"
$STRIPPERS = @{
    'armeabi-v7a' = "$STRIPPER_PATH\arm-linux-androideabi-strip.exe";
    'arm64-v8a'   = "$STRIPPER_PATH\aarch64-linux-android-strip.exe";
    'x86'         = "$STRIPPER_PATH\x86_64-linux-android-strip.exe" ;
    'x86_64'      = "$STRIPPER_PATH\x86_64-linux-android-strip.exe" ;    
} 

# Minimum supported sdk: SHOULD BE GREATER THAN 16
$MIN_SDK = 24

# Android project path: REPLACE WITH YOUR PROJECT PATH!
$PROJECT_PATH = 'C:\Users\computer\AndroidStudioProjects\OpencvPose'

# Directory for storing native libraries
$NATIVE_DIR = "$PROJECT_PATH\app\src\main\cppLibs"

# -----------------------------------------------------------------------------
# -- Utils
# -----------------------------------------------------------------------------
function Make-Dir {
    if (-Not (Test-Path $args[0])) {
        New-Item -ItemType directory -Path $args[0] > $null
    }
}

function Copy-Directory ($from, $to, $tab) {
    $Items = Get-ChildItem $from

    ForEach ($item in $Items) {
        if ((Get-Item $item.FullName) -is [System.IO.DirectoryInfo]) {
            Write-Host "$tab > $item"
            Make-Dir "$to\$item"
            Copy-Directory "$from\$item" "$to\$item" "$tab  "
        } else {
            Copy-Item -Path $item.FullName -Destination $to
            Write-Host "$tab | $item copied." 
        }
    }
}

# -----------------------------------------------------------------------------
# -- Dlib
# ----------------------------------------------------------------------------- 

# Dlib library path:
$DLIB_PATH = 'C:\Users\computer\Desktop\dlib-19.19\dlib'

function Compile-Dlib {
    Set-Location $DLIB_PATH
    Make-Dir 'build'

    Write-Host '=> Compiling Dlib...'
    sleep 0.5

    ForEach ($abi in $ABIs) {
        Write-Host
        Write-Host "=> Compiling Dlib for ABI: '$abi'..."
        sleep 0.5

        Make-Dir "build\$abi"
        Set-Location "build\$abi"

        $cmakeArguments = @(
                "-DBUILD_SHARED_LIBS=1",
                "-DANDROID_NDK=$NDK",
                "-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN",
                "-GNinja"
                "-DCMAKE_MAKE_PROGRAM=C:\Users\computer\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe",
                "-DCMAKE_BUILD_TYPE=Release",
                "-DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions",
                "-DCMAKE_C_FLAGS=-O3",
                "-DANDROID_ABI=$abi",
                "-DANDROID_PLATFORM=android-$MIN_SDK",
                "-DANDROID_TOOLCHAIN=clang",
                "-DANDROID_STL=c++_shared",
                "-DANDROID_CPP_FEATURES=rtti exceptions",
                "-DCMAKE_PREFIX_PATH=..\..\",
                "..\..\"
        )

        $stripArguments = @(
                "--strip-unneeded",
                "dlib/libdlib.so"
        )

        & $AndroidCmake $cmakeArguments
        sleep 0.5

        Write-Host "=> Generating the 'dlib/libdlib.so' for ABI: '$abi'..."
        & $AndroidCmake --build .
        sleep 0.5

        Write-Host "=> Stripping libdlib.so for ABI: '$abi'to reduce space..."
        & $STRIPPERS[$abi] $stripArguments
        sleep 0.5

        Write-Host '=> done.'
        Set-Location ..\..\
        sleep 0.5
    }    
}

function Dlib-Setup {
    Write-Host '=> Making directories for Dlib...'
    Make-Dir "$NATIVE_DIR\dlib"
    Write-Host "=> '$NATIVE_DIR\dlib' created."
    Make-Dir "$NATIVE_DIR\dlib\lib"
    Write-Host "=> '$NATIVE_DIR\dlib\lib' created."
    Make-Dir "$NATIVE_DIR\dlib\include"
    Write-Host "=> '$NATIVE_DIR\dlib\include' created."
    Make-Dir "$NATIVE_DIR\dlib\include\dlib"
    Write-Host "=> '$NATIVE_DIR\dlib\include\dlib' created."

    Write-Host "=> Copying Dlib headers..."
    Copy-Directory "$DLIB_PATH" "$NATIVE_DIR\dlib\include\dlib" ''

    Write-Host "=> Copying 'libdlib.so' for each ABI..."
    ForEach ($abi in $ABIs) {
        Make-Dir "$NATIVE_DIR\dlib\lib\$abi"
        Copy-Item -Path "$DLIB_PATH\build\$abi\libdlib.so" -Destination "$NATIVE_DIR\dlib\lib\$abi"
        Write-Host " > Copied libdlib.so for $abi"
    }
}

# COMMENT TO DISABLE COMPILATION
Compile-Dlib

# -----------------------------------------------------------------------------
# -- OpenCV: Not used in this
# -----------------------------------------------------------------------------

# OpenCV library path: REPLACE WITH YOUR OPENCV PATH!
#$OPENCV_PATH='path-to-your\opencv-4.0.1-android-sdk\sdk\native'

#function Opencv-Setup {
#    Make-Dir "$NATIVE_DIR\opencv"
#
#    Write-Host "=> Copying 'libopencv_java4.so' for each ABI..."
#    ForEach ($abi in $ABIs) {
#        Make-Dir "$NATIVE_DIR\opencv\$abi"
#        Copy-Item -Path "$OPENCV_PATH\libs\$abi\libopencv_java4.so" -Destination "$NATIVE_DIR\opencv\$abi"
#        Write-Host " > Copied libopencv_java4.so for $abi"
#    }
#}


# -----------------------------------------------------------------------------
# -- Project setup
# -----------------------------------------------------------------------------

Make-Dir $NATIVE_DIR

# COMMENT TO NOT COPY DLIB '.so' FILES
Dlib-Setup

# COMMENT TO NOT COPY OPENCV '.so' FILES
#Opencv-Setup

Write-Host "=> Project configuration completed."

# -----------------------------------------------------------------------------

Сохраните приведенный выше код в файл Power Shell с расширением setup1.ps1. Приведенный выше код должен генерировать файл libdlib.so внутри папки dlib-19.19/dlib/build для каждой папки платформы.

Для запуска кода сначала откройте Power Shell от имени администратора. Далее используйте set-location C:\Users\computer\Desktop\dlib-19.19. Здесь используйте Set-ExecutionPolicy RemoteSigned и нажмите A, чтобы иметь возможность выполнять код powershell. После того, как все сделано (libdlib.so сгенерировано и скопировано), используйте Set-ExecutionPolicy Restricted.

Запустите код Power Shell, вставив & "C:\Users\computer\Desktop\setup1.ps1" в оболочку. Компиляция займет некоторое время и после компиляции скопирует заголовки и libdlib.so для другой платформы в папку android студийного проекта, как в переменной $PROJECT_PATH в приведенном выше сценарии.

...