![](img/b89b3444-a6a9-45b4-bb02-ddf8388a6992.png)Figure 2.6 Adding utility files, model files, label file and image files
6. 将`ViewController.m`重命名为`ViewController.mm`,因为我们将在该文件中混合使用 C ++代码和 Objective-C 代码来调用 TensorFlow C ++ API 并处理图像输入和推断结果。 然后,在`@interface ViewController`之前,添加以下`#include`和函数原型:
6. 将`ViewController.m`重命名为`ViewController.mm`,因为我们将在该文件中混合使用 C++ 代码和 Objective-C 代码来调用 TensorFlow C++ API 并处理图像输入和推断结果。 然后,在`@interface ViewController`之前,添加以下`#include`和函数原型:
2. 选择 Xcode 文件 | 新增 | 文件 ...,然后选择 Objective-C 文件。 输入名称`RunInference`。 您将看到一个消息框,询问您“您是否要配置一个 Objective-C 桥接头?” 单击创建桥接标题。 将文件`RunInference.m`重命名为`RunInfence.mm`,因为我们将混合使用 C,C ++和 Objective-C 代码来进行后期录音音频处理和识别。 我们仍在 Swift 应用程序中使用 Objective-C,因为要从 Swift 调用 TensorFlow C ++代码,我们需要一个 Objective-C 类作为 C ++代码的包装。
2. 选择 Xcode 文件 | 新增 | 文件 ...,然后选择 Objective-C 文件。 输入名称`RunInference`。 您将看到一个消息框,询问您“您是否要配置一个 Objective-C 桥接头?” 单击创建桥接标题。 将文件`RunInference.m`重命名为`RunInfence.mm`,因为我们将混合使用 C,C++ 和 Objective-C 代码来进行后期录音音频处理和识别。 我们仍在 Swift 应用程序中使用 Objective-C,因为要从 Swift 调用 TensorFlow C++ 代码,我们需要一个 Objective-C 类作为 C++ 代码的包装。
3. 创建一个名为`RunInference.h`的头文件,并添加以下代码:
```py
...
...
@@ -772,7 +772,7 @@ func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully f
}
```
如果您确实想将尽可能多的代码移植到 Swift,[则可以用 Swift 替换 C 中的音频文件转换代码](https://developer.apple.com/documentation/audiotoolbox/extended_audio_file_services)。 还有一些非官方的开源项目提供了官方 TensorFlow C ++ API 的 Swift 包装器。 但是为了简单起见和达到适当的平衡,我们将保持 TensorFlow 模型的推论,在本示例中,还将保持音频文件的读取和转换,以及在 C ++和 Objective-C 中与控制 UI 和 录音,并启动呼叫以进行音频处理和识别。
如果您确实想将尽可能多的代码移植到 Swift,[则可以用 Swift 替换 C 中的音频文件转换代码](https://developer.apple.com/documentation/audiotoolbox/extended_audio_file_services)。 还有一些非官方的开源项目提供了官方 TensorFlow C++ API 的 Swift 包装器。 但是为了简单起见和达到适当的平衡,我们将保持 TensorFlow 模型的推论,在本示例中,还将保持音频文件的读取和转换,以及在 C++ 和 Objective-C 中与控制 UI 和 录音,并启动呼叫以进行音频处理和识别。
这就是构建使用语音命令识别模型的 Swift iOS 应用所需的全部内容。 现在,您可以在 iOS 模拟器或实际设备上运行它,并看到与 Objective-C 版本完全相同的结果。
3. 使用 TensorFlow Lite 转换器工具将 TensorFlow 模型转换为 TensorFlow Lite 模型。 在下一节中,您将看到一个详细的示例。
4. 在 iOS 或 Android 上部署 TensorFlow Lite 模型-对于 iOS,使用 C ++ API 加载和运行模型; 对于 Android,请使用 Java API(围绕 C ++ API 的包装器)加载和运行模型。 与我们之前在 TensorFlow Mobile 项目中使用的`Session`类不同,C ++和 Java API 均使用 TensorFlow-lite 特定的`Interpreter`类来推断模型。 在接下来的两个部分中,我们将向您展示 iOS C ++代码和 Android Java 代码以使用`Interpreter`。
4. 在 iOS 或 Android 上部署 TensorFlow Lite 模型-对于 iOS,使用 C++ API 加载和运行模型; 对于 Android,请使用 Java API(围绕 C++ API 的包装器)加载和运行模型。 与我们之前在 TensorFlow Mobile 项目中使用的`Session`类不同,C++ 和 Java API 均使用 TensorFlow-lite 特定的`Interpreter`类来推断模型。 在接下来的两个部分中,我们将向您展示 iOS C++ 代码和 Android Java 代码以使用`Interpreter`。
If you run a TensorFlow Lite model on Android, and if the Android device is Android 8.1 (API level 27) or above and supports hardware acceleration with a dedicated neural network hardware, a GPU, or some other digital signal processors, then the `Interpreter` will use the Android Neural Networks API ([https://developer.android.com/ndk/guides/neuralnetworks/index.html](https://developer.android.com/ndk/guides/neuralnetworks/index.html)) to speed up the model running. For example, Google's Pixel 2 phone has a custom chip optimized for image processing, which can be turned on with Android 8.1, and support hardware acceleration.
...
...
@@ -114,7 +114,7 @@ target 'HelloTFLite'
pod'TensorFlowLite'
```
运行`pod install`。 然后在 Xcode 中打开 `HelloTFLite.xcworkspace`,将`ViewController.m`重命名为`ViewController.mm`,并添加必要的 C ++头文件和 TensorFlow Lite 头文件。 您的 Xcode 项目应类似于以下屏幕截图:
运行`pod install`。 然后在 Xcode 中打开 `HelloTFLite.xcworkspace`,将`ViewController.m`重命名为`ViewController.mm`,并添加必要的 C++ 头文件和 TensorFlow Lite 头文件。 您的 Xcode 项目应类似于以下屏幕截图:
![](img/b4b1750b-cc97-42f8-a032-226b595d7e46.png)Figure 11.1 A new Xcode iOS project using the TensorFlow Lite podWe're only showing you how to use the TensorFlow Lite pod in your iOS apps. There's another way to add TensorFlow Lite to iOS, similar to building the custom TensorFlow Mobile iOS library that we've done many times in the previous chapters. For more information on how to build your own custom TensorFlow Lite iOS library, see the documentation at [https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/ios.md](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/ios.md).
看看 C ++源代码`tensorflow/contrib/pi_examples/label_image/label_image.cc`和`tensorflow/contrib/pi_examples/camera/camera.cc`,您会看到它们使用与前几章中的 iOS 应用类似的 C ++代码来加载模型图文件,准备输入张量,运行 模型,并获得输出张量。
看看 C++ 源代码`tensorflow/contrib/pi_examples/label_image/label_image.cc`和`tensorflow/contrib/pi_examples/camera/camera.cc`,您会看到它们使用与前几章中的 iOS 应用类似的 C++ 代码来加载模型图文件,准备输入张量,运行 模型,并获得输出张量。
在之前的章节中,我们主要使用 Python 训练和测试 TensorFlow 模型,然后再使用本机 TensorFlow C ++库的 Java 接口代码在使用 C ++或 Android 的 iOS 中运行模型。 在 Raspberry Pi 上,您可以选择直接使用 TensorFlow Python API 或 C ++ API 在 Pi 上运行 TensorFlow 模型,尽管通常仍会在功能更强大的模型上进行训练 电脑。 有关完整的 TensorFlow Python API 文档,请参见[这里](https://www.tensorflow.org/api_docs/python)。
在之前的章节中,我们主要使用 Python 训练和测试 TensorFlow 模型,然后再使用本机 TensorFlow C++ 库的 Java 接口代码在使用 C++ 或 Android 的 iOS 中运行模型。 在 Raspberry Pi 上,您可以选择直接使用 TensorFlow Python API 或 C++ API 在 Pi 上运行 TensorFlow 模型,尽管通常仍会在功能更强大的模型上进行训练 电脑。 有关完整的 TensorFlow Python API 文档,请参见[这里](https://www.tensorflow.org/api_docs/python)。
要使用 GoPiGo Python API 使机器人根据您的语音命令移动,请首先在`listen.py`中添加以下两行: