在 Windows 上为 Android 设置 Dlib

Setting up Dlib for Android on Windows

我正尝试按照本教程 https://github.com/Luca96/dlib-for-android 在 windows 的 android 工作室中设置 dlib。我已经更改了各种变量的路径,但得到了同样的错误。我不需要 opencv 只需要 dlib。 Opencv 已经设置好并正常工作。

我用 -DCMAKE_MAKE_PROGRAM=C:\Users\computer\AppData\Local\Android\Sdk\cmake.10.2.4988404\bin\ninja.exe 替换了 -GNinja,但仍然出现错误。

使用 android studio 设置 dlib 的任何替代过程也将有效。

我根据提供的示例自定义 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.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.0.6113669'
}

$TOOLCHAIN = "C:\Users\computer\AppData\Local\Android\Sdk\ndk.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.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.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.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.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.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.

EDIT: Use DLIB master from Github instead of 19.19 from their website to avoid errors with OpenCV. Change the paths accordingly, set dlib-19.19 to dlib-master where applicable.

我能够通过对路径和标志进行一些更改来编译 dlib。一个很大的变化是包括两个标志,

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

我将最新的 dlib(19.19) 提取到桌面并设置了 cmakeNDKlldb。仅为 x86_64 编译,

C:\Users\computer\AppData\Local\Android\Sdk\cmake.10.2.4988404\bin\cmake.exe -DBUILD_SHARED_LIBS=1 -DCMAKE_TOOLCHAIN_FILE="C:\Users\computer\AppData\Local\Android\Sdk\ndk.0.6113669\build\cmake\android.toolchain.cmake" -DANDROID_NDK="C:\Users\computer\AppData\Local\Android\Sdk\ndk.0.6113669" -GNinja -DCMAKE_MAKE_PROGRAM="C:\Users\computer\AppData\Local\Android\Sdk\cmake.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 中。它将在 dlib 文件夹中生成 libdlib.so

编译脚本:

# Luca Anzalone

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

# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$AndroidCmake = 'C:\Users\computer\AppData\Local\Android\Sdk\cmake.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.0.6113669'
}

$TOOLCHAIN = "C:\Users\computer\AppData\Local\Android\Sdk\ndk.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.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."

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

将上述代码保存到扩展名为 setup1.ps1 的强大 shell 文件中。上面的代码应该在每个平台文件夹的 dlib-19.19/dlib/build 文件夹中生成 libdlib.so 文件。

为了运行代码先以管理员身份打开电源shell。接下来,使用 set-location C:\Users\computer\Desktop\dlib-19.19。这里使用 Set-ExecutionPolicy RemoteSigned 并按 A 可以执行 powershell 代码。完成后(libdlib.so 生成并复制)使用 Set-ExecutionPolicy Restricted.

通过将 & "C:\Users\computer\Desktop\setup1.ps1" 粘贴到 shell 中,

运行 幂 shell 代码。编译需要一些时间,编译后它会将不同平台的 headers 和 libdlib.so 复制到 android studio 项目文件夹,如上面脚本中的变量 $PROJECT_PATH 所示。

@B200011011 解决方案对我不起作用。我必须更正一些输出目录才能使其在 windows 10 PowerShell 上运行。

这是更新后的代码:

# Luca Anzalone

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

# Android-cmake path: REPLACE WITH YOUR CMAKE PATH!
$AndroidCmake = 'C:\Users\Aeon\AppData\Local\Android\Sdk\cmake.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\Aeon\AppData\Local\Android\Sdk\ndk.3.6528147'
}

$TOOLCHAIN = "$NDK\build\cmake\android.toolchain.cmake"

# Supported Android ABI: TAKE ONLY WHAT YOU NEED!
$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 = 16

# Android project path: REPLACE WITH YOUR PROJECT PATH!
$PROJECT_PATH = 'C:\Users\Aeon\Desktop\AeonLabs\Software\Android\IOYO\code'

# 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:\dlibAndroid\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_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_PATH/build/$abi/libdlib.so"
        )

        & $AndroidCmake $cmakeArguments
        sleep 0.5

        Write-Host "=> Generating the 'dlib/build/$abi/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
# -----------------------------------------------------------------------------

# OpenCV library path: REPLACE WITH YOUR OPENCV PATH!
$OPENCV_PATH='C:\dlibAndroid\OpenCV-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."

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

当您 运行 .ps1 PowerShell 脚本时,您可能会收到消息说“.ps1 未经过数字签名。该脚本不会在系统上执行。”

要修复它,您必须 运行 下面的命令 运行 Set-ExecutionPolicy 并更改执行策略设置。

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass