Slide 1

Slide 1 text

ML Kit 使⽤用簡介 (iOS) Elvis Lin @Cocoahead Taipei 2018-07-19

Slide 2

Slide 2 text

關於我 • Elvis Lin • iOS 與 Android 永遠的初學者 • Twitter: @elvismetaphor • Blog: https://blog.elvismetaphor.me

Slide 3

Slide 3 text

⼤大綱 • 什什麼是(我理理解的)機器學習 • 移動裝置上實作機器學習應⽤用的限制 • TensorFlow Lite 與 ML Kit • 範例例

Slide 4

Slide 4 text

機器學習的應⽤用

Slide 5

Slide 5 text

機器學習 • 從資料中歸納出有⽤用的規則 • 訓練模型 • 使⽤用模型 • Mobile Application Engineer 參參 與開發主要是在「使⽤用模型」 這個範圍

Slide 6

Slide 6 text

Data Result (Trained)
 Model

Slide 7

Slide 7 text

移動裝置上
 實作機器學習應⽤用的限制 • 記憶體有限與儲存空間有限 • 計算能⼒力力不如⼤大型伺服器 • 電池容量量有限

Slide 8

Slide 8 text

移動裝置上
 實作機器學習應⽤用的改良⽅方向 • 記憶體有限與儲存空間有限 —> 減少模型(Model)的體積 • 計算能⼒力力不如⼤大型伺服器 —> 降低演算法的複雜度 • 電池容量量有限 —> 降低演算法的複雜度

Slide 9

Slide 9 text

Google 推出的解決⽅方案 • TensorFlow Lite • ML Kit

Slide 10

Slide 10 text

Tensorflow Lite https://youtu.be/ByJnpbDd-zc

Slide 11

Slide 11 text

https://www.tensorflow.org/mobile/tflite/

Slide 12

Slide 12 text

轉換 Tensorflow 檔案的⼯工具 • Tensorflow converter • 轉成 Tensorflow Lite 格式
 • Tensorflow-CoreML converter • 轉成 CoreML 格式 • https://github.com/tf-coreml/tf-coreml

Slide 13

Slide 13 text

ML Kit https://youtu.be/Z-dqGRSsaBs

Slide 14

Slide 14 text

Neural Networks API Metal

Slide 15

Slide 15 text

ML Kit • Cloud Vision API / Mobile Vision API • Tensorflow Lite • 整合 Firebase,託管「客製化的模型」

Slide 16

Slide 16 text

ML Kit Base APIs • Image labeling • Text recognition (OCR) • Face detection • Barcode scanning • Landmark detection • others……

Slide 17

Slide 17 text

託管客製化的模型 ⽬目前只⽀支援 Tensorflow Lite 格式

Slide 18

Slide 18 text

使⽤用 ML Kit

Slide 19

Slide 19 text

建立⼀一個 Firebase 專案

Slide 20

Slide 20 text

建立⼀一個 iOS app
 然後下載設定檔 設定好 Bundle ID 下載 GoogleService-info.plist

Slide 21

Slide 21 text

新增 plist 檔案到專案 • 將 GoogleService-Info.plist 放到
 // 下

Slide 22

Slide 22 text

安裝 Firebase 函式庫 • 修改 Podfile,新增以下的內容 • cd 
 pod install • 打改 .xcworkspace pod 'Firebase/Core' pod 'Firebase/MLVision' pod 'Firebase/MLVisionTextModel' pod 'Firebase/MLVisionFaceModel' pod 'Firebase/MLVisionBarcodeModel' pod 'Firebase/MLVision' pod 'Firebase/MLVisionLabelModel'

Slide 23

Slide 23 text

掃描 Barcode (Local) let barcodeDetector: VisionBarcodeDetector = 
 Vision.vision().barcodeDetector(options: options) let visionImage = VisionImage(image: pickedImage) barcodeDetector.detect(in: visionImage) { (barcodes, error) in guard error == nil, 
 let barcodes = barcodes, !barcodes.isEmpty else { self.dismiss(animated: true, completion: nil) self.resultView.text = "No Barcode Detected" return } for barcode in barcodes { // handle the detected barcode } }

Slide 24

Slide 24 text

第1步:初始化 Detector let barcodeDetector: VisionBarcodeDetector = 
 Vision.vision().barcodeDetector(options: options) let visionImage = VisionImage(image: pickedImage)

Slide 25

Slide 25 text

第2步:取得結果 barcodeDetector.detect(in: visionImage) { (barcodes, error) in guard error == nil, 
 let barcodes = barcodes,
 !barcodes.isEmpty else { self.dismiss(animated: true, completion: nil) self.resultView.text = "No Barcode Detected" return } for barcode in barcodes { // handle the detected barcode } }

Slide 26

Slide 26 text

⽀支援的 Barcode 格式 • Code 128 (FORMAT_CODE_128) • Code 39 (FORMAT_CODE_39) • Code 93 (FORMAT_CODE_93) • Codabar (FORMAT_CODABAR) • EAN-13 (FORMAT_EAN_13) • EAN-8 (FORMAT_EAN_8) • ITF (FORMAT_ITF) • UPC-A (FORMAT_UPC_A) • UPC-E (FORMAT_UPC_E) •QR Code (FORMAT_QR_CODE) • PDF417 (FORMAT_PDF417) • Aztec (FORMAT_AZTEC) • Data Matrix (FORMAT_DATA_MATRIX)

Slide 27

Slide 27 text

辨識⽂文字 (Local) lazy var textDetector: VisionTextDetector = 
 Vision.vision().textDetector() func runTextRecognition(with image: UIImage) { let visionImage = VisionImage(image: image) textDetector.detect(in: visionImage) { (features, error) in if let error = error { print("Received error: \(error)") } self.processResult(from: features, error: error) } }

Slide 28

Slide 28 text

辨識⽂文字 (Cloud) Lazy var cloudTextDetector: VisionCloudTextDetector = 
 Vision.vision().cloudTextDetector() func runCloudTextRecognition(with image: UIImage) { let visionImage = VisionImage(image: image) cloudTextDetector.detect(in: visionImage) { (features, error) in if let error = error { print("Received error: \(error)") } self.processCloudResult(from: features, error: error) } }

Slide 29

Slide 29 text

補充資料 • ML Kit 簡介 (for Android)
 https://blog.elvismetaphor.me/ml-kit-fundamentals-for- android-6444e2db0fdb • ML Kit 簡介 (for iOS)
 https://blog.elvismetaphor.me/ml-kit-fundamentals-for- ios-cb705044e69b

Slide 30

Slide 30 text

參參考資料 • https://youtu.be/Z-dqGRSsaBs • https://codelabs.developers.google.com/codelabs/mlkit-ios/ • https://github.com/firebase/quickstart-ios/tree/master/ mlvision • https://www.appcoda.com.tw/ml-kit/

Slide 31

Slide 31 text

No content