5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS11 Vision.frameworkで顔トラッキング

Posted at

はじめに

リアルタイムテキスト検出を行っている、こちらの有難いソースを一部修正すればできます。
元記事はこちらをご参照ください。

ソース(変更する部分のみ)

    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        let uiImage = sampleBufferToUIImage(sampleBuffer: sampleBuffer, with: UIApplication.shared.statusBarOrientation)
        let orientation = CGImagePropertyOrientation(uiImage.imageOrientation)
        let ciImage = CIImage(image: uiImage)!

        // UI の変更はメインスレッドで実行
        DispatchQueue.main.async { [weak self] in
            if let frame = self?.imageFrameOnViewController(uiImage: uiImage) {
                self?.imageView.image = uiImage
                self?.visualizeRectanglesView.frame = frame
            }
        }

        // orientation を必ず設定すること
//        let handler = VNImageRequestHandler(ciImage: ciImage, orientation: Int32(orientation.rawValue))
        let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation)

//        let request = VNDetectTextRectanglesRequest() { request, error in
        let request = VNDetectFaceRectanglesRequest() { request, error in
            // 顔ブロックの矩形を取得
            let rects = request.results?.flatMap { result -> [CGRect] in
//                guard let observation = result as? VNTextObservation else { return [] }
                guard let observation = result as? VNFaceObservation else { return [] }
                return [observation.boundingBox]
            } ?? []

//            // 文字ごとの矩形を取得
//            let characterRects = request.results?.flatMap { result -> [VNRectangleObservation] in
//                //guard let observation = result as? VNTextObservation else { return [] }
//                guard let observation = result as? VNFaceObservation else { return [] }
//                return observation.characterBoxes ?? []
//            } ?? []

            // UI の変更はメインスレッドで実行
            DispatchQueue.main.async { [weak self] in
                self?.visualizeRectanglesView.rectangles = rects
//                self?.visualizeRectanglesView.characterRectangles = characterRects
            }
        }

//        request.reportCharacterBoxes = true

        try! handler.perform([request])
    }
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?