机器学习-CoreML结合Siri


使用AVFoundation拍摄照片,再使用Core ML处理分析照片,最后通过Siri告知拍摄的对象是什么。
  1. 前期配置

    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
    38
    39
    var captureView : UIView!
    var synthe = AVSpeechSynthesizer()
    var uttrence = AVSpeechUtterance()
    var predicte = ""

    var captureSession : AVCaptureSession!
    var cameraOutput : AVCapturePhotoOutput!
    var previewLayer : AVCaptureVideoPreviewLayer!

    override func viewDidLoad() {
    super.viewDidLoad()

    captureView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 200))
    self.view.addSubview(captureView)
    setupCamera()
    }

    func setupCamera() {
    captureSession = AVCaptureSession()
    captureSession.sessionPreset = AVCaptureSession.Preset.photo
    cameraOutput = AVCapturePhotoOutput()

    let device = AVCaptureDevice.default(for: .video)
    if let input = try? AVCaptureDeviceInput.init(device: device!) {
    if(captureSession.canAddInput(input)) {
    captureSession.addInput(input)

    if(captureSession.canAddOutput(cameraOutput)) {
    captureSession.addOutput(cameraOutput)
    }

    previewLayer = AVCaptureVideoPreviewLayer.init(session: captureSession)
    previewLayer.videoGravity = .resizeAspectFill
    previewLayer.frame = CGRect.init(x: 0, y: 0, width: 200, height: 200)
    captureView.layer.addSublayer(previewLayer)
    captureSession.startRunning()
    }
    }
    }
  2. 获取照片

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    func launchUI() {
    let setting = AVCapturePhotoSettings()
    // xcode9's Bug
    let previewPixelType = setting.__availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [ kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
    kCVPixelBufferWidthKey as String: "\(captureView.bounds.size.width)",
    kCVPixelBufferHeightKey as String: "\(captureView.bounds.size.height)"] as [String : Any]
    setting.previewPhotoFormat = previewFormat
    cameraOutput.capturePhoto(with: setting, delegate: self)
    }

    // AVCapturePhotoCaptureDelegate
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    if error != nil {
    print("error occured: \(error!.localizedDescription)")
    }

    if let imageData = photo.fileDataRepresentation(),let image = UIImage.init(data: imageData) {
    // Predict
    self.predict(image: image)
    }
    }

在Xcode9上availablePreviewPhotoPixelFormatTypes这个属性前面需要加上双下划线,更高版本则不需要

3.图片处理预测

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
 func predict(image:UIImage) {
// if let data = UIImagePNGRepresentation(image) {
// let fileName = getDocumentsDirectory().appendingPathComponent("captureImage")
// print("fleName:\(fileName)")
// try? data.write(to: fileName)
// }
let model = try! VNCoreMLModel.init(for: VGG16().model)
let request = VNCoreMLRequest.init(model: model, completionHandler: { (request:VNRequest, error:Error?) in
weak var weakSelf = self
guard let results = request.results as? [VNClassificationObservation] else {
fatalError("no result")
}
var bestPrediction = ""
var bestConfidence:VNConfidence = 0

for classfication:VNClassificationObservation in results {
if classfication.confidence > bestConfidence {
bestConfidence = classfication.confidence
bestPrediction = classfication.identifier
}
}

weakSelf!.say(string: "\(bestPrediction)")

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5, execute: {
weakSelf!.launchUI()
})
})
let handler = VNImageRequestHandler.init(cgImage: image.cgImage!)
try? handler.perform([request])
}

4.语音播报

1
2
3
4
5
6
7
8
func say(string: String) {
uttrence = AVSpeechUtterance.init(string: string)
uttrence.rate = 0.3
uttrence.voice = AVSpeechSynthesisVoice.init(language: "zh_CN")
uttrence.pitchMultiplier = 0.8
uttrence.postUtteranceDelay = 0.2
synthe.speak(uttrence)
}

这里面选择的是汉语,不管选择的是什么语音种类,英语都是支持的

5.效果演示

Demo下载


如有任何疑问或问题请联系我:fishnewsdream@gmail.com,欢迎交流,共同提高!

Objective-C/Swift技术开发交流群201556264,讨论何种技术并不受限,欢迎各位大牛百家争鸣!

微信公众号OldDriverWeekly,欢迎关注并提出宝贵意见

老司机iOS周报,欢迎关注或订阅

刚刚在线工作室,欢迎关注或提出建设性意见!

刚刚在线论坛, 欢迎踊跃提问或解答!

如有转载,请注明出处,谢谢!

本站总访问量 本文总阅读量