本文運(yùn)行環(huán)境為XCode9 beta2, iOS 11 beta2
1. CoreML
CoreML是蘋果在WWDC2017 新發(fā)布的
Framework
侍芝,方便了Machine Learning
在蘋果自家平臺(tái)的接接入與使用枕磁,同時(shí)蘋果提供了Python
的coremltools
,方便將各大開源模型訓(xùn)練工具的現(xiàn)有模型轉(zhuǎn)化為MLModel
艇拍。-
模型訓(xùn)練
2. vision
vision
是一個(gè)新的,強(qiáng)大的,易于使用的框架扫尖,是蘋果于WWDC 2017
上針對(duì)CoreML
使用所提出的新Framework
,能快速有效的用于面部檢測(cè)掠廓、面部特征點(diǎn)换怖、文字、矩形蟀瞧、條形碼和物體沉颂。
3. 集成機(jī)器學(xué)習(xí)
我們將構(gòu)建一個(gè)通過AVCaptureSession
捕獲到當(dāng)前圖像,并通過MLModel
分析悦污,獲取到與圖像最匹配的物品名字铸屉。
界面大概是
那么我們就正式開始
1. 創(chuàng)建Single
工程
2. 可以從蘋果的“機(jī)器學(xué)習(xí)”頁(yè)面下載Inception v3
。
3. 在Info.plist
中添加Privacy - Camera Usage Description
4. 代碼編寫
-
首先我們創(chuàng)建一個(gè)
AVCaptureSession
用來獲取攝像頭的圖像lazy var avSession: AVCaptureSession = AVCaptureSession() lazy var preViewLayer: AVCaptureVideoPreviewLayer = { return AVCaptureVideoPreviewLayer(session: self.avSession) }() override func viewDidLoad() { super.viewDidLoad() setupAVSession() preViewLayer.frame = view.bounds self.view.layer.insertSublayer(preViewLayer, at: 0) avSession.startRunning() } fileprivate func setupAVSession() { guard let device = AVCaptureDevice.default(for: .video) else { fatalError("this application cannot be run on simulator") } do { let input = try AVCaptureDeviceInput(device: device) avSession.addInput(input) let output = AVCaptureVideoDataOutput() avSession.addOutput(output) let queue = DispatchQueue(label: "video queue", qos: .userInteractive) output.setSampleBufferDelegate(self, queue: queue) } catch let error { print(error) } }
-
實(shí)現(xiàn)
AVCaptureVideoDataOutputSampleBufferDelegate
代理extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate { func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { //FIXME: 添加CoreML相關(guān)邏輯 } }
-
為工程引入
MLModel
直接拖拽切端,點(diǎn)擊Inceptionv3
可以看到模型的詳細(xì)信息 -
添加模型處理代碼
lazy var inceptionv3ClassificationRequest: VNCoreMLRequest = { // Load the ML model through its generated class and create a Vision request for it. do { let model = try VNCoreMLModel(for: Inceptionv3().model) return VNCoreMLRequest(model: model, completionHandler: self.inceptionv3ClassificationHandler) } catch { fatalError("can't load Vision ML model: \(error)") } }() extension ViewController { func inceptionv3ClassificationHandler(request: VNRequest, error: Error?) { guard let observations = request.results as? [VNClassificationObservation] else { fatalError("unexpected result type from VNCoreMLRequest") } guard let best = observations.first else { fatalError("can't get best result") } DispatchQueue.main.async { print("Classification: \"\(best.identifier)\" Confidence: \(best.confidence)") self.classifyLabel.text = best.identifier } } }
-
傳入
MLModel
參數(shù)func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]) do { try handler.perform([inceptionv3ClassificationRequest]) } catch _ { } }
4. 效果展示
至此已完成了機(jī)器學(xué)習(xí)的集成彻坛,代碼已上傳到git