ios - 使用Vision&AVFoundation Framework从实时摄像头(不是从静态图像)进行实时面部检测

标签 ios swift real-time face-detection vision

我需要从iPhone前置摄像头检测真实面孔。因此,我已经使用视觉框架来实现它。但是它也可以从静态图像(人像)中检测到面部,这也是不需要的。这是我的代码段。

 class ViewController {

 func sessionPrepare() {
        session = AVCaptureSession()
        guard let session = session, let captureDevice = frontCamera else { return }

        do {
            let deviceInput = try AVCaptureDeviceInput(device: captureDevice)
            session.beginConfiguration()

            if session.canAddInput(deviceInput) {
                session.addInput(deviceInput)
            }

            let output = AVCaptureVideoDataOutput()
            output.videoSettings = [
                String(kCVPixelBufferPixelFormatTypeKey) : Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
            ]

            output.alwaysDiscardsLateVideoFrames = true

            if session.canAddOutput(output) {
                session.addOutput(output)

            }

            session.commitConfiguration()
            let queue = DispatchQueue(label: "output.queue")
            output.setSampleBufferDelegate(self, queue: queue)
            print("setup delegate")
        } catch {
            print("can't setup session")
        }
    }
}


}

如果将其放在相机前面,它也会从静态图像中检测到脸部。
 extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate {

        func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

            let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)

            let attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate)

            let ciImage = CIImage(cvImageBuffer: pixelBuffer!, options: attachments as! [String : Any]?)

            let ciImageWithOrientation = ciImage.applyingOrientation(Int32(UIImageOrientation.leftMirrored.rawValue))

            detectFace(on: ciImageWithOrientation)
        }

    }

    func detectFace(on image: CIImage) {
            try? faceDetectionRequest.perform([faceDetection], on: image)
            if let results = faceDetection.results as? [VNFaceObservation] {
                if !results.isEmpty {
                    faceLandmarks.inputFaceObservations = results
                    detectLandmarks(on: image)

                    DispatchQueue.main.async {
                        self.shapeLayer.sublayers?.removeAll()
                    }
                }
            }
        }

        func detectLandmarks(on image: CIImage) {
            try? faceLandmarksDetectionRequest.perform([faceLandmarks], on: image)
            if let landmarksResults = faceLandmarks.results as? [VNFaceObservation] {
                for observation in landmarksResults {
                    DispatchQueue.main.async {
                        if let boundingBox = self.faceLandmarks.inputFaceObservations?.first?.boundingBox {
                            let faceBoundingBox = boundingBox.scaled(to: self.view.bounds.size)
                            //different types of landmarks
                            let faceContour = observation.landmarks?.faceContour
                            let leftEye = observation.landmarks?.leftEye
                            let rightEye = observation.landmarks?.rightEye
                            let nose = observation.landmarks?.nose
                            let lips = observation.landmarks?.innerLips
                            let leftEyebrow = observation.landmarks?.leftEyebrow
                            let rightEyebrow = observation.landmarks?.rightEyebrow
                            let noseCrest = observation.landmarks?.noseCrest
                            let outerLips = observation.landmarks?.outerLips
                        }
                    }
                }
            }
        }

那么,有什么方法可以仅通过实时摄像机检测来完成?非常感谢您的帮助和建议

最佳答案

我需要做同样的事情,最后经过大量实验,我发现了
https://github.com/syaringan357/iOS-MobileFaceNet-MTCNN-FaceAntiSpoofing
它仅检测实时摄像机的面部。但是它没有使用Vision框架。

关于ios - 使用Vision&AVFoundation Framework从实时摄像头(不是从静态图像)进行实时面部检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55503503/

相关文章:

swift - 访问结构属性

performance - Haskell 对于游戏等软实时应用程序的 GC 性能如何?

ios - 核心情节实时图

Android Canvas 路径实时性能

iphone - Segue 传递 uiimage 并在 UIwebview 中打开

ios - Swift 无法识别的选择器发送到实例错误

ios - Swift 中的 NSSortDescriptor - 按距离下一个生日剩余的天数排序

android - 在 macOS sierra 10.12.3 上安装 appium

ios - 如何使用 Parse TableView 传输图像

ios - 为什么注册 Swinject 模型类时没有 ".inObjectScope( .Container )"生成单例?