Skip to content

Latest commit

 

History

History
351 lines (236 loc) · 10.6 KB

build-sherpa-ncnn-swift.rst

File metadata and controls

351 lines (236 loc) · 10.6 KB

Build sherpa-ncnn for iOS

This section describes how to build sherpa-ncnn for iPhone and iPad.

Requirement

Warning

The minimum deployment requires the iOS version >= 13.0.

Before we continue, please make sure the following requirements are satisfied:

  • macOS. It won't work on Windows or Linux.
  • Xcode. The version 14.2 (14C18) is known to work. Other versions may also work.
  • CMake. CMake 3.25.1 is known to work. Other versions may also work.
  • (Optional) iPhone or iPad. This is for testing the app on your device. If you don't have a device, you can still run the app within a simulator on your Mac.

Caution

If you get the following error:

CMake Error at toolchains/ios.toolchain.cmake:544 (get_filename_component):
  get_filename_component called with incorrect number of arguments
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.29.0/share/cmake/Modules/CMakeDetermineSystem.cmake:146 (include)
  CMakeLists.txt:2 (project)

please run:

sudo xcode-select --install
sudo xcodebuild -license

And then delete the build directory ./build-ios and re-build.

Please see also k2-fsa/sherpa-onnx#702.

Download sherpa-ncnn

First, let us download the source code of sherpa-ncnn.

Note

In the following, I will download sherpa-ncnn to $HOME/open-source, i.e., /Users/fangjun/open-source, on my Mac.

You can put it anywhere as you like.

mkdir -p $HOME/open-source
cd $HOME/open-source
git clone https://github.com/k2-fsa/sherpa-ncnn

Build sherpa-ncnn (in commandline, C++ Part)

After downloading sherpa-ncnn, let us build the C++ part of sherpa-ncnn.

cd $HOME/open-source/sherpa-ncnn/
./build-ios.sh

It will generate a directory $HOME/open-source/sherpa-ncnn/build-ios, which we have already pre-configured for you in Xcode.

Hint

You don't have to look at the generated files in $HOME/open-source/sherpa-ncnn/build-ios to build an app. We have pre-configured it for you.

If you are eager to learn more about the generated files or want to use sherpa-ncnn in your own iOS project, please have a look at sherpa_ncnn_ios_swift_for_the_more_curious.

Build sherpa-ncnn (in Xcode)

Use the following command to open sherpa-ncnn in Xcode:

cd $HOME/open-source/sherpa-ncnn/ios-swift/SherpaNcnn
open SherpaNcnn.xcodeproj

It will start Xcode and you will see the following screenshot:

Screenshot after running the command open SherpaNcnn.xcodeprojScreenshot after running the command open SherpaNcnn.xcodeproj

Please select Product -> Build to build the project. See the screenshot below:

Screenshot for selecting Product -> BuildScreenshot for selecting Product -> Build

After finishing the build, you should see the following screenshot:

Screenshot after finishing the build.Screenshot after finishing the build.

Congratulations! You have successfully built the project. Let us run the project by selecting Product -> Run, which is shown in the following screenshot:

Screenshot for Product -> Run.Screenshot for Product -> Run.

Please wait for a few seconds before Xcode starts the simulator.

Unfortunately, it will throw the following error:

Screenshot for the errorScreenshot for the error

The reason for the above error is that we have not provided the pre-trained model yet.

The file ViewController.swift pre-selects the pre-trained model to be sherpa-ncnn-mixed-english-chinese-conv-emformer-model, shown in the screenshot below:

Screenshot for the pre-selected pre-trained modelScreenshot for the pre-selected pre-trained model

Let us add the pre-trained model sherpa-ncnn-mixed-english-chinese-conv-emformer-model to Xcode. Please follow sherpa-ncnn-mixed-english-chinese-conv-emformer-model to download it from huggingface. You can download it to any directory as you like.

Please right click the project SherpaNcnn and select Add Files to "SherpaNcnn"... in the popup menu, as is shown in the screenshot below:

Screenshot for adding files to SherpaNcnnScreenshot for adding files to SherpaNcnn

In the popup dialog, switch to the folder where you just downloaded the pre-trained model.

In the screenshot below, it is the folder /Users/fangjun/open-source/icefall-models/sherpa-ncnn-conv-emformer-transducer-2022-12-06:

Screenshot for navigating to the folder containing the downloaded pre-trainedScreenshot for navigating to the folder containing the downloaded pre-trained

Select required files and click the button Add:

Screenshot for selecting required filesScreenshot for selecting required files

After adding pre-trained model files to Xcode, you should see the following screenshot:

Screenshot after add pre-trained model filesScreenshot after add pre-trained model files

At this point, you should be able to select the menu Product -> Run to run the project and you should finally see the following screenshot:

Screenshot for a successful run.Screenshot for a successful run.

Click the button to start recording! A screenshot is given below:

Screenshot for recording and recognition.Screenshot for recording and recognition.

Congratulations! You have finally succeeded in running sherpa-ncnn with iOS, though it is in a simulator.

Please read below if you want to run sherpa-ncnn on your iPhone or iPad.

Run sherpa-ncnn on your iPhone/iPad

First, please make sure the iOS version of your iPhone/iPad is >= 13.0.

Click the menu Xcode -> Settings..., as is shown in the following screenshot:

Screenshot for Xcode -> Settings...Screenshot for Xcode -> Settings...

In the popup dialog, please select Account and click + to add your Apple ID, as is shown in the following screenshots.

Screenshot for selecting Account and click +.Screenshot for selecting Account and click +. Screenshot for selecting Apple ID and click ContinueScreenshot for selecting Apple ID and click Continue Screenshot for adding your Apple ID and click NextScreenshot for adding your Apple ID and click Next Screenshot for entering your password and click NextScreenshot for entering your password and click Next Screenshot after adding your Apple IDScreenshot after adding your Apple ID

After adding your Apple ID, please connect your iPhone or iPad to your Mac and select your device in Xcode. The following screenshot is an example to select my iPhone.

Screenshot for selecting your deviceScreenshot for selecting your device

Now your Xcode should look like below after selecting a device:

Screenshot after selecting your deviceScreenshot after selecting your device

Please select Product -> Run again to run sherpa-ncnn on your selected device, as is shown in the following screenshot:

Screenshot for selecting Product -> RunScreenshot for selecting Product -> Run

After a successful build, check your iPhone/iPad and you should see the following screenshot:

Screenshot for running sherpa-ncnn on your deviceScreenshot for running sherpa-ncnn on your device

To fix that, please select Settings -> General -> Device Management on your device

Screenshot for selecting Settings -> General -> Device Management on your deviceScreenshot for selecting Settings -> General -> Device Management on your device

Please click Apple Development: csukuangfj... and click Trust "Apple Development: csukuangfj@g..." in the subsequent dialog, as is shown below:

Screenshot for "Trust "Apple Development: csukuangfj@g...""Screenshot for "Trust "Apple Development: csukuangfj@g...""

At this point, you should be able to run the app on your device. The following is a screenshot about running it on my iPhone:

Screenshot for running sherpa-ncnn on iPhoneScreenshot for running sherpa-ncnn on iPhone

Congratulations! You have successfully run sherpa-ncnn on your device!