TensorFlow Lite开发之——iOS图像分类识别(5)

一 概述

  • 图像模型介绍
  • iOS开发流程
  • 运行数据识别结果

二 图像模型介绍

2.1 模型位置

2.2 模型说明

  • 每个模型可以识别1000多种物体特征
  • 应用启动后,通过Model选项可以选择Model

三 iOS开发流程

3.1 将模型放到TFLite文件夹下

3.2 添加到Bundle Resources

3.3 在Podfile中添加TensorFlow Lite依赖

1
2
3
4
5
target 'ImageClassification' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'TensorFlowLiteTaskVision'
end

3.4 界面布局

3.5 定义ImageClassificationHelper用于对图形解析

1
2
3
4
5
6
let options = ImageClassifierOptions(modelPath: modelPath)
options.baseOptions.computeSettings.cpuSettings.numThreads = Int(Int32(threadCount))
options.classificationOptions.maxResults = resultCount
options.classificationOptions.scoreThreshold = scoreThreshold

classifier = try ImageClassifier.classifier(options: options)

3.6 ViewController定义模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
enum ModelType: CaseIterable {
case efficientnetLite0
case efficientnetLite1
case efficientnetLite2
case efficientnetLite3
case efficientnetLite4

var modelFileInfo: FileInfo {
switch self {
case .efficientnetLite0:
return FileInfo("efficientnet_lite0", "tflite")
case .efficientnetLite1:
return FileInfo("efficientnet_lite1", "tflite")
case .efficientnetLite2:
return FileInfo("efficientnet_lite2", "tflite")
case .efficientnetLite3:
return FileInfo("efficientnet_lite3", "tflite")
case .efficientnetLite4:
return FileInfo("efficientnet_lite4", "tflite")
}
}

var title: String {
switch self {
case .efficientnetLite0:
return "EfficientNet-Lite0"
case .efficientnetLite1:
return "EfficientNet-Lite1"
case .efficientnetLite2:
return "EfficientNet-Lite2"
case .efficientnetLite3:
return "EfficientNet-Lite3"
case .efficientnetLite4:
return "EfficientNet-Lite4"
}
}
}

3.7 识别结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
extension ViewController: CameraFeedManagerDelegate {

func didOutput(pixelBuffer: CVPixelBuffer) {
let currentTimeMs = Date().timeIntervalSince1970 * 1000
guard (currentTimeMs - previousInferenceTimeMs) >= delayBetweenInferencesMs else { return }
previousInferenceTimeMs = currentTimeMs
guard !isInferenceQueueBusy else { return }
inferenceQueue.async { [weak self] in
guard let self = self else { return }

self.isInferenceQueueBusy = true

// Pass the pixel buffer to TensorFlow Lite to perform inference.
let result = self.imageClassificationHelper?.classify(frame: pixelBuffer)

self.isInferenceQueueBusy = false

// Display results by handing off to the InferenceViewController.
DispatchQueue.main.async {
let resolution = CGSize(
width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer))
self.inferenceViewController?.inferenceResult = result
self.inferenceViewController?.resolution = resolution
self.inferenceViewController?.tableView.reloadData()
}
}
}

四 运行数据识别结果

识别结果1 识别结果1 识别结果1

五 参考

  • TensorFlow-ios快速入门
  • TensorFlow Lite示例应用